-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.envloader
194 lines (153 loc) · 4.41 KB
/
lib.envloader
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!bin/bash
export ENVLOADER_VARIABLES=""
export ENVLOADER_VERBOSE
export ENVLOADER_PROMPT_ENABLED=0
if [ "Darwin" == $(uname -s) ]; then
export IS_DARWIN=1
fi
if [ -z "${ENVLOADER_VERBOSE}" ]; then
ENVLOADER_VERBOSE=0
fi
if [ -z "${BASE_PATH}" ]; then
export BASE_PATH=${PATH}
fi
if [ -z "${DOLLAR}" ]; then
export DOLLAR='$'
fi
function __envloader_error() {
local p_msg=$*;
echo >&2 "$(tput setaf 1)envloader error$(tput sgr0): ${p_msg}"
}
function __envloader_verbose() {
local p_msg=$*;
if [ "${ENVLOADER_VERBOSE}" == "1" ]; then
echo "$(tput setaf 3)envloader$(tput sgr0): ${p_msg}"
fi
}
function __envloader_export_file() {
local p_file=$1; shift
jq -r "to_entries[] | \"export \(.key)=\(.value | tostring | tojson);export \(.key)_origin=\(input_filename | tojson)\"" ${p_file} 2>/dev/null || {
__envloader_error "malformed json file ${p_file}, ignored"
}
# jq -r "to_entries[] | \"export \(.key)='\(.value | tostring)';export \(.key)_origin=\(input_filename | tojson)\"" ${p_file} 2>/dev/null || {
# __envloader_error "malformed json file ${p_file}, ignored"
# }
}
function __envloader_save_vars() {
local p_file=$1; shift
local c_var
for c_var in $(jq -r 'to_entries[] | "\(.key)"' < "${p_file}" 2>/dev/null ); do
strlist_add ENVLOADER_VARIABLES "${c_var}"
done
}
function __envloader_is_safe() {
local p_file=$1; shift
local l_mode=$(stat --printf "%A" $(realpath ${p_file}))
local l_ow=${l_mode:1:9}
if [ "${l_ow}" != "r--------" ]; then
echo "$(tput setaf 3)envloader$(tput sgr0): ${p_file} (${l_ow}) is not safe to load"
return 1
fi
__envloader_verbose ${p_file} is safe
return 0
}
function envloader_verbose_on() {
export ENVLOADER_VERBOSE="1"
}
function envloader_verbose_off() {
export ENVLOADER_VERBOSE="0"
}
function envloader_edit() {
local p_file=$1; shift
if [ -z "${p_file}" ]; then
p_file=".env.json"
fi
if [ ! -f "${p_file}" ]; then
echo '{}' > "${p_file}"
chmod 400 "${p_file}"
fi
local l_tmp=$(mktemp --suffix .json)
local l_edit=${EDITOR}
if [ -z "${l_edit}" ]; then
l_edit="vi"
fi
cp ${p_file} ${l_tmp}
chmod u+w ${p_file}
${l_edit} ${l_tmp}
cp ${l_tmp} ${p_file}
chmod u-w ${p_file}
rm -f ${l_tmp}
envloader_run
envloader_list
}
function envloader_count() {
local c_var
local l_res=0
for c_var in $(echo "${ENVLOADER_VARIABLES}" | tr ';' '\n' | sort -u); do
l_res=$((l_res + 1))
done
echo "${l_res}"
}
function __envloader_update_prompt_label() {
test ${ENVLOADER_PROMPT_ENABLED} -eq 0 && {
return 0
}
local l_count=$(envloader_count)
if [ ${l_count} -ne 0 ]; then
promptcmd_add_labels "env⌈${l_count}⌉|lightmagenta"
fi
}
function envloader_enable_prompt() {
ENVLOADER_PROMPT_ENABLED=1
promptcmd_push "__envloader_update_prompt_label"
}
function envloader_disable_prompt() {
ENVLOADER_PROMPT_ENABLED=0
}
function envloader_list() {
__envloader_verbose "listing all variables..."
local c_var
for c_var in $(echo "${ENVLOADER_VARIABLES}" | tr ';' '\n' | sort -u); do
local l_originName="${c_var}_origin"
local l_origin="${!l_originName}"
local l_val="${!c_var}"
printf -- "-> $(tput setaf 1)%10s$(tput sgr0) = $(tput setaf 3)%s$(tput sgr0) ($(tput bold)%s$(tput sgr0))\n" "${c_var}" "${l_val}" "${l_origin}"
done
}
function envloader_unload() {
__envloader_verbose "unloading all variables variables..."
local c_var
for c_var in $(echo "${ENVLOADER_VARIABLES}" | tr ';' ' '); do
if [ "${c_var}" != "PATH" ]; then
__envloader_verbose "-> unset ${c_var}"
local l_name="${c_var}_origin"
unset "${c_var}"
unset "${l_name}"
else
__envloader_verbose "-> reseting PATH to ${BASE_PATH}"
export PATH=${BASE_PATH}
fi
done
ENVLOADER_VARIABLES=""
}
function envloader_run() {
local c_path=$(pwd)
local l_vars=""
tty -s || return
envloader_unload
while [ "${c_path}" != "/" ]; do
local l_file="${c_path}/.env.json"
__envloader_verbose considering ${l_file}
c_path=$(dirname ${c_path})
test -f "${l_file}" || continue
__envloader_is_safe "${l_file}" || continue
local l_tmp="$(__envloader_export_file ${l_file} | tr '\n' ';')"
l_vars="${l_tmp}${l_vars}"
__envloader_save_vars "${l_file}"
done
if [ ! -z "${l_vars}" ]; then
__envloader_verbose "running command : ${l_vars}"
eval "${l_vars}";
fi
}
cdevent_push envloader_run