-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash-tpl
executable file
·125 lines (108 loc) · 3.12 KB
/
bash-tpl
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
#!/usr/bin/env bash
#[[-------------------------------------------------------------------- Settings
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
# set -o xtrace
#]]
#[[-------------------------------------------------------------------- Script variables
readonly __script="$(basename "${BASH_SOURCE[0]}")"
readonly __dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
readonly __template_dir="${__dir}""/templates"
#]]
#[[-------------------------------------------------------------------- Utils
usage(){
printf "%s [OPTIONS] -o|--output <output-file>\n\n" "${0}"
}
help_options(){
printf "Options :\n"
printf " General options :\t\n"
printf " -h, --help Print this help text and exit\n"
printf " -o, --output FILENAME Output in the specified file\n"
printf " -p, --parse PARSE Add parsing method in main, see PARSE for all info\n"
printf " -c, --color Add util color script to the project and create a dir by default\n"
}
with_parse_option(){
case "${1}" in
getopt)
source="${__template_dir}/main-with-getopt-parsing"
;;
getopts)
source="${__template_dir}/main-with-getopts-parsing"
;;
bash)
source="${__template_dir}/main-with-bash-parsing"
;;
*)
printf "Parsing template %s unknown.\n" "${1}" 1>&2 exit 3
source=""
;;
esac
echo "${source}"
}
#]]
#[[-------------------------------------------------------------------- Main
main() {
# default values
settings_template="$(< "${__template_dir}/settings-template")"
main_template=""
if [[ "${#}" -eq 0 ]]; then
usage && exit 1
fi
# BEGIN_PARAMETERS_LOOP
while [[ $# -gt 0 ]]; do
i="${1}"
case ${i} in
-p|--parse)
parse="${2}"
main_template="$(< "$(with_parse_option "${parse}")")"
shift
shift
;;
-h|--help)
help_options && exit 0
shift
;;
-o|--output)
output="${2}"
shift
shift
;;
*)
printf "Unknown parameter passed: %s\n" "${1}" 1>&2
usage
exit 1
;;
esac
done
# END_PARAMETERS_LOOP
# check if output is set
if [[ ! -n "${output}" ]]; then
printf "The output option -o|--output <filename> must be specified\n" 1>&2
fi
# default main_template if nothing already set
if [[ ! -n "${main_template}" ]]; then
main_template="$(< "${__template_dir}/main-default")"
fi
# if file exist ask to override the file
if [[ -f "./${output}" ]]; then
printf "%s exists.\n" "${output}"
printf "Are you sure to override the file ? (y/n) "
read answer
case $answer in
y)
printf "File %s override.\n" "${output}"
;;
*)
printf "Abort.\n" && exit 2
;;
esac
fi
# generate the file
echo -e "#!/usr/bin/env bash\n" > "./${output}" && \
echo -e "${settings_template}\n" >> "./${output}" && \
echo -e "${main_template}\n" >> "./${output}" && \
echo "File generated"
}
#]]
main "${@}"