-
Notifications
You must be signed in to change notification settings - Fork 0
/
.env.gen
executable file
·54 lines (45 loc) · 1.14 KB
/
.env.gen
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
#!/usr/bin/env bash
show_help () {
cat <<-HELP
usage: ${0##*/} [options] filename required_vars
options:
-h, --help Show this document
params:
filename The file name to be written
required_vars The file that list the variables checked and/or created
HELP
exit 1
}
create_file () {
[ ! -f $filename ] && touch $filename
# INFO: It needs to read in first place the file so we need to redirect the stdin to /dev/fd/3 for user input
{ cat $required_vars | while IFS= read line; do
# INFO: ${VAR%%PATTERN} removes from the end, so you can list required variables with help texts
varname=${line%% *}
if [[ ! -e "${varname}" ]] && ! grep "^${varname}" $filename; then
cat <<-hint >&2
please set $line:
hint
read -u 3 question
echo "${varname}=${question}" >> $filename
echo >&2
fi
done } 3<&0
exit 0
}
# Parse args
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
show_help
;;
*)
[ "${1##-*}" != "" ] && filename="$1" && \
[ "${2##-*}" != "" ] && [ -f $2 ] && required_vars="$2" \
&& shift && shift
;;
esac
shift
done
[[ -n ${required_vars} && -n ${filename} ]] && create_file
show_help