Repository navigation
Expand file tree
/
Copy pathenv_cmd.mbt
More file actions
143 lines (138 loc) · 3.6 KB
/
Copy pathenv_cmd.mbt
File metadata and controls
143 lines (138 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
///|
/// The `mb env` command group: manage dotenv files for the current project and
/// for the user as a whole.
///
/// - project scope (default): `./.env` (and `./.mbenv` when present);
/// - global scope (`--global`): `$HOME/.mbenv`, which also holds the key.
///
/// Values are encrypted individually, so listing a file never needs the key.
///|
/// Resolve the encryption key, prompting for one the first time it is needed.
fn env_require_key() -> String raise MbError {
match read_env_key() {
Some(k) => k
None => {
let path = global_env_file()
println("No encryption key found.")
println("Enter a key to encrypt env values (stored in " + path + "):")
let key = @utf8.decode_lossy(mb_read_line()).trim().to_owned()
if key == "" {
raise MbError("no key provided")
}
let (_, entries) = read_global_env()
write_global_env(Some(key), entries)
println("Stored key in " + path)
key
}
}
}
///|
fn env_upsert(
entries : Array[(String, String)],
key : String,
value : String,
) -> Array[(String, String)] {
let out : Array[(String, String)] = []
let mut replaced = false
for entry in entries {
let (k, v) = entry
if k == key {
out.push((k, value))
replaced = true
} else {
out.push((k, v))
}
}
if !replaced {
out.push((key, value))
}
out
}
///|
/// The `mb env set` command. Encrypts the value unless `plain` is set.
fn env_set(
key : String,
value : String,
global : Bool,
plain : Bool,
) -> Unit raise MbError {
let stored = if plain {
value
} else {
encrypt_value(value, env_require_key())
}
if global {
let (k, entries) = read_global_env()
write_global_env(k, env_upsert(entries, key, stored))
println(key + " set in " + global_env_file())
} else {
write_env_file(".env", env_upsert(read_env_file(".env"), key, stored))
println(key + " set in .env")
}
}
///|
/// The `mb env rm` command: remove one or more keys.
fn env_rm(keys : Array[String], global : Bool) -> Unit raise MbError {
let path = if global { global_env_file() } else { ".env" }
let (existing_key, entries) = if global {
read_global_env()
} else {
(None, read_env_file(path))
}
let out : Array[(String, String)] = []
let mut removed = 0
for entry in entries {
let (k, v) = entry
let mut drop = false
for want in keys {
if want == k {
drop = true
}
}
if drop {
removed = removed + 1
} else {
out.push((k, v))
}
}
if removed == 0 {
raise MbError("no such key in " + path + ": " + keys.join(", "))
}
if global {
write_global_env(existing_key, out)
} else {
write_env_file(path, out)
}
println("Removed " + removed.to_string() + " key(s) from " + path)
}
///|
/// The `mb env list` command.
///
/// Values are only revealed when stdout is a terminal, so a captured or piped
/// listing shows encrypted values as `<encrypted>` instead of their contents.
/// `mask` forces masking even on a terminal.
fn env_list(global : Bool, mask : Bool) -> Unit raise MbError {
let path = if global { global_env_file() } else { ".env" }
let reveal = !mask && is_tty()
let entries = if reveal {
load_env_file(path)
} else if global {
let (_, e) = read_global_env()
e
} else {
read_env_file(path)
}
if entries.is_empty() {
println("No env entries in " + path + ".")
return
}
println("Env entries in " + path + ":")
for entry in entries {
let (k, v) = entry
if is_encrypted_value(v) {
println(" " + k + "=<encrypted>")
} else {
println(" " + k + "=" + v)
}
}
}