-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-bws.plugin.zsh
67 lines (56 loc) · 1.7 KB
/
zsh-bws.plugin.zsh
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
ZSH_BWS_DELIMITER='\t'
function _bw_ensure_token() {
if [ -z $BWS_ACCESS_TOKEN ]; then >&2 echo "BWS_ACCESS_TOKEN is not set - aborting" && return 1; else return 0; fi
}
function _bws_select() {
jq -r ".[] | [.key, .id] | join(\"$ZSH_BWS_DELIMITER\")" <(bws secret list 2>&/dev/null) | fzf -0 --with-nth 1 -d "$ZSH_BWS_DELIMITER" | awk -F "$ZSH_BWS_DELIMITER" '{print $2}'
}
function _bws_id() {
jq -r ".[] | select(.key == \"$1\") | .id" <(bws secret list 2>&/dev/null)
}
function bws_get_full() {
local arg_key arg_id arg_format
zparseopts -D -F -K -- \
k:=arg_key \
i:=arg_id \
o:=arg_format ||
return 1
_bw_ensure_token
if [[ "$?" == "1" ]]; then
return 1
fi
local secret_id
if [[ ! -z "$arg_key" && ! -z "$arg_id" ]]; then
>&2 echo "Specify only one of -i or -k"
return 1
elif [[ -z "$arg_key" && -z "$arg_id" ]]; then
secret_id=$(_bws_select)
elif [[ ! -z "$arg_id" ]]; then
secret_id=${arg_id[-1]}
elif [[ ! -z "$arg_key" ]]; then
secret_id=$(_bws_id ${arg_key[-1]})
fi
local out_format=${arg_format[-1]:-json}
bws secret get -o $out_format $secret_id
}
function bws_get() {
local arg_key arg_id
zparseopts -D -F -K -- \
k:=arg_key \
i:=arg_id ||
return 1
bws_get_full $arg_id $arg_key -o json | jq -c '.value' -r
}
function bws_copy() {
local arg_key arg_id
local copy_cmd="${ZSH_BWS_COPY_CMD:-clipcopy}"
zparseopts -D -F -K -- \
k:=arg_key \
i:=arg_id ||
return 1
jq -c '.value' -r <(bws_get_full $arg_id $arg_key -o json) | eval "$copy_cmd"
}
alias bwsc='bws_copy'
alias bwsg='bws_get'
alias bwsgf='bws_get_full'
alias bwsl='bws secret list'