-
Notifications
You must be signed in to change notification settings - Fork 3
/
wizard
executable file
·107 lines (93 loc) · 1.87 KB
/
wizard
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
#!/bin/sh
force=1
inject_secret_osx() {
until security find-generic-password -a "$(whoami)" -s "$1" > /dev/null 2>&1
do
echo "New password detected: $1"
security add-generic-password -a "$(whoami)" -s "$1" -w
done
export "$1"="$(security find-generic-password -a "$(whoami)" -s "$1" -w)"
}
inject_secret_pass() {
command -v pass >/dev/null || { echo "I require pass (https://passwordstore.org/) but it's not installed. Aborting." >&2; return 1; }
until pass list "$1" >/dev/null 2>&1
do
pass insert "$1"
done
export "$1"="$(pass show "$1")"
}
is_command() {
[ "${1%${1#?}}"x = '!x' ]
}
is_secret() {
test "${1#*=}" = "$1"
return $?
}
export_config() {
export "${1?}"
}
export_secret() {
case "$(uname -s)" in
Darwin*) inject_secret_osx "$1";;
*) inject_secret_pass "$1";;
esac
}
export_command() {
string=$(echo "$1" | awk '{ string=substr($0, 2); print string; }')
export "${string%=*}"="$(eval "${string#*=}")"
}
check_env_exists() {
if [ ! -f "./.env" ]
then
echo "There is no .env in this directory, this file is required."
return 1
fi
}
should_set() {
if [ "$force" -eq 0 ] || [ -z "$(printenv "${1%=*}")" ]
then
return 0
else
return 1
fi
}
export_envs() {
while read -r line <&3 || [ -n "$line" ]; do
case "$line" in
\#*|'') continue ;;
esac
if ! should_set "$line"
then
continue
elif is_secret "$line"
then
export_secret "$line"
elif is_command "$line"
then
export_command "$line"
else
export_config "$line"
fi
done 3<"$1"
}
read_in_flags() {
while test $# -gt 0; do
case "$1" in
-h|--help)
echo "-h, --help show brief help"
echo "-f, --force override already set environmental variables"
return 0
;;
-f|--force)
force=0
shift
;;
*)
break
;;
esac
done
}
read_in_flags "$@"
check_env_exists || return 1
export_envs "./.env"