This repository was archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsecrets.sh
More file actions
executable file
·172 lines (138 loc) · 3.51 KB
/
Copy pathsecrets.sh
File metadata and controls
executable file
·172 lines (138 loc) · 3.51 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/env bash
set -euo pipefail
readonly prefix="Dotfiles/"
if [ -t 1 ]; then
readonly success=$(tput setaf 2)$(tput bold)
readonly warning=$(tput setaf 3)$(tput bold)
readonly info=$(tput setaf 4)
readonly reset=$(tput sgr0)
else
readonly success=
readonly warning=
readonly info=
readonly reset=
fi
function path_to_key {
path="${1}"
rel=$(realpath --relative-base="${HOME}" --strip "${path}")
if [[ "${rel}" =~ ^/ ]]; then
echo "\$ROOT${rel}"
else
echo "\$HOME/${rel}"
fi
}
function key_to_path {
key="${1}"
if [[ "${key}" =~ ^\$ROOT ]]; then
echo "${key/\$ROOT/}"
elif [[ "${key}" =~ ^\$HOME ]]; then
echo "${key/\$HOME/$HOME}"
else
echo "${0}: unknown key format '${key}'" >/dev/stderr
exit 1
fi
}
function get_keys {
lpass ls "${prefix}" | sed -n "s|^${prefix}\(.\+\) \[.*\]|\1|p"
}
function diff_key {
key="${1}"
path=$(key_to_path "${key}")
tmp=$(mktemp)
chmod 0600 "${tmp}"
lpass show --notes "${prefix}${key}" >"${tmp}"
diff "${tmp}" "${path}" || true
rm -f "${tmp}"
}
function show_help {
echo "Usage: ${0} ls"
echo " or: ${0} get"
echo " or: ${0} put FILE..."
echo " or: ${0} rm KEY..."
echo "Manage secrets using LastPass."
exit 0
}
function do_list {
get_keys | while read line; do
echo "${success}${line}${reset}"
done
}
function do_get {
key="${1}"
path=$(key_to_path "${key}")
if [ -e "${path}" ]; then
if [ -z "$(diff_key "${key}")" ]; then
echo "${info}Skipped '${path}' <- '${key}'${reset}"
else
echo "${warning}Skipped '${path}' <- '${key}' (local file differs)${reset}"
fi
return
fi
mkdir -p "$(dirname "${path}")"
lpass show --notes "${prefix}${key}" >"${path}"
chmod 0600 "${path}"
echo "${success}Downloaded '${path}' <- '${key}'${reset}"
}
function do_put {
path="${1}"
key=$(path_to_key "${path}")
lpass edit --non-interactive --notes "${prefix}${key}" <"${path}"
echo "${success}Uploaded '${path}' -> '${key}'${reset}"
}
function do_rm {
key="${1}"
lpass rm "${prefix}${key}"
echo "${success}Removed '${key}'${reset}"
}
case "${1:-}" in
ls)
if [[ $# -ne 1 ]]; then
echo "${0}: invalid operand" >/dev/stderr
exit 1
fi
do_list
;;
get)
if [[ $# -ne 1 ]]; then
echo "${0}: invalid operand" >/dev/stderr
exit 1
fi
IFS=$'\n'
for key in $(get_keys); do
do_get "${key}"
done
;;
put)
if [[ $# -lt 2 ]]; then
echo "${0}: missing file operand" >/dev/stderr
exit 1
fi
for path in "${@:2}"; do
if [ ! -f "${path}" ]; then
echo "${0}: cannot upload '${path}': Not a file" >/dev/stderr
exit 1
fi
done
for path in "${@:2}"; do
do_put "${path}"
done
;;
rm)
if [[ $# -lt 2 ]]; then
echo "${0}: missing file operand" >/dev/stderr
exit 1
fi
for key in "${@:2}"; do
if [ -z "$(get_keys | grep -Fx "${key}")" ]; then
echo "${0}: cannot remove '${key}': Key does not exist" >/dev/stderr
exit 1
fi
done
for key in "${@:2}"; do
do_rm "${key}"
done
;;
*)
show_help
;;
esac