-
Notifications
You must be signed in to change notification settings - Fork 0
/
newutil
executable file
·103 lines (93 loc) · 3.38 KB
/
newutil
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
#! /usr/bin/bash
################################################################################
# Copyright: [email protected]
# Version: 1.2
# Date: 20230329
# Description: Used to create a module template.
################################################################################
source ./lib/environments.sh
source ./lib/errno.sh
source ./lib/printer.sh
function usage() {
print_bold_green "Usage: newutil [-u|--utility-name <UtilityFileName>]"
print_bold_green " [-h|--help]"
print_bold_green "Note:"
print_green " -u <UtilityFileName>, specify the bash utility file name; must be [a-zA-Z0-9_]+"
}
function check_and_create_empty_utility_file() {
local vFileName=$1
if ! [[ ${vFileName} =~ [a-zA-Z0-9_]+ ]]; then
print_red_t "Invalid bash utility file name: ${vFileName}"
usage
exit ${EINVAL}
fi
if [[ -f ${vFileName} ]]; then
print_yellow_t "Utility file has alread existed: ${vFileName}"
exit ${EEXIST}
fi
touch ${vFileName}
local vRc=$?
if [[ ${vRc} -ne 0 ]]; then
print_red_t "[${vRc}]Failed to create a bash utility template file: ${vFileName}"
exit ${EPERM}
fi
chmod a+x ${vFileName}
vRc=$?
if [[ ${vRc} -ne 0 ]]; then
print_red_t "[${vRc}]Failed to make this bash utility template file executable: ${vFileName}"
exit ${EPERM}
fi
}
function main() {
local vRc
local vBashUtilityFileName
for((i = 1; i <= $#; ++i)); do
case ${!i} in
'-u' | '--utility-name')
i=$((i + 1))
vBashUtilityFileName=${!i}
check_and_create_empty_utility_file ${vBashUtilityFileName}
;;
'-h' | '--help')
usage
exit ${EOK}
;;
*)
usage
exit ${EINVAL}
;;
esac
done
echo '#! /usr/bin/bash' > ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo '################################################################################' >> ${vBashUtilityFileName}
echo '# Copyright: [email protected]' >> ${vBashUtilityFileName}
echo '# Version: 1.0' >> ${vBashUtilityFileName}
echo "# Date: $(date +%Y-%M-%d)" >> ${vBashUtilityFileName}
echo '# Description:' >> ${vBashUtilityFileName}
echo '################################################################################' >> ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo '####Imports of other module files####' >> ${vBashUtilityFileName}
echo "source ${ZBASH_LIB_ROOT}/environments.sh" >> ${vBashUtilityFileName}
echo "source ${ZBASH_LIB_ROOT}/errno.sh" >> ${vBashUtilityFileName}
echo "source ${ZBASH_LIB_ROOT}/printer.sh" >> ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo '####Definition of global parameters####' >> ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo '####Definition of functions####' >> ${vBashUtilityFileName}
echo "function ${vBashUtilityFileName}_main() {" >> ${vBashUtilityFileName}
echo '}' >> ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo "${vBashUtilityFileName}_main \"\$@\"" >> ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo >> ${vBashUtilityFileName}
echo '####Unit test section####' >> ${vBashUtilityFileName}
echo 'if false; then' >> ${vBashUtilityFileName}
echo ' print_bold_green_t "====Unit test begin===="' >> ${vBashUtilityFileName}
echo ' print_bold_green_t "====Unit test end===="' >> ${vBashUtilityFileName}
echo 'fi' >> ${vBashUtilityFileName}
}
main "$@"