-
Notifications
You must be signed in to change notification settings - Fork 2
/
crypt.nu
57 lines (51 loc) · 1.4 KB
/
crypt.nu
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
#crypt
export def nu-crypt [
file?
--encrypt(-e) #is has precedence over decrypt
--decrypt(-d)
--output_file(-o):string #only for -d option
--no_ui(-n) #to ask for password in cli
] {
let file = get-input $in $file -n
match [$encrypt,$decrypt] {
[true,false] => {gpg --pinentry-mode loopback --symmetric --armor --yes $file},
[false,true] => {
if ($output_file | is-empty) {
if $no_ui {
gpg --pinentry-mode loopback --decrypt --quiet $file
} else {
gpg --decrypt --quiet $file
}
} else {
if $no_ui {
gpg --pinentry-mode loopback --output $output_file --quiet --decrypt $file
} else {
gpg --output $output_file --quiet --decrypt $file
}
}
},
_ => {return-error "flag combination not allowed!!"}
}
}
#open credentials
export def open-credential [file?,--ui(-u)] {
let file = get-input $in $file -n
if $ui {
nu-crypt -d $file | from json
} else {
nu-crypt -d $file -n | from json
}
}
#save credentials
export def save-credential [content,field:string] {
if ($field | is-empty) or ($content | is-empty) {
return-error "missing arguments!"
}
let credentials_e = $env.MY_ENV_VARS.credentials | path join credentials.json.asc
let credentials = $env.MY_ENV_VARS.credentials | path join credentials.json
open-credential $credentials_e
| upsert $field $content
| save -f $credentials
nu-crypt -e $credentials
rm -f $credentials | ignore
}