-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjust.bash
executable file
·432 lines (376 loc) · 8.98 KB
/
just.bash
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env bash
# Name: just (Just a UNIX Shell script Template [with bash features])
# Version: 0.0.9
# Release: 1
# License: CC-BA (Creative Commons By Attribution)
# http://creativecommons.org/licenses/by/4.0/legalcode
# Group: System
# Source: N/A
# URL: https://github.com/lateralblast/just
# Distribution: UNIX
# Vendor: UNIX
# Packager: Richard Spindler <richard@lateralblast.com.au>
# Description: A template for writing shell scripts
# Insert some shellcheck disables
# Depending on your requirements, you may want to add/remove disables
# shellcheck disable=SC2034
# shellcheck disable=SC1090
# shellcheck disable=SC2129
# Grab script args for some initial processing
script_args="$*"
script_file="$0"
script_name="just"
script_file=$( realpath "$script_file" )
script_path=$( dirname "$script_file" )
module_path="$script_path/modules"
script_bin=$( basename "$script_file" )
# Create arrays for options and actions
declare -A options
declare -A defaults
declare -a option_flags
declare -a action_flags
# Set defaults
set_defaults () {
defaults["verbose"]="false"
defaults["actions"]="false"
defaults["options"]="false"
defaults["strict"]="false"
defaults["dryrun"]="false"
defaults["debug"]="false"
defaults["force"]="false"
defaults["yes"]="false"
for default in "${!defaults[@]}"; do
value="${defaults[${default}]}"
options["$default"]="$value"
done
os_name=$( uname -s )
if [ "$os_name" = "Linux" ]; then
os_distro=$( lsb_release -i -s 2> /dev/null )
fi
}
set_defaults
# Verbose message
verbose_message () {
message="$1"
format="$2"
if [ "$format" = "verbose" ]; then
echo "$message"
else
if [ "${options['verbose']}" = "true" ]; then
if [[ "$format" =~ ing$ ]]; then
format="${format^}"
else
if [[ "$format" =~ t$ ]]; then
format="${format^}ing"
else
if [[ "$format" =~ e$ ]]; then
format="${format::-1}"
format="${format^}ing"
fi
fi
fi
echo -e "$format:\t\t$message"
fi
fi
}
# Enable verbose mode
if [[ "$script_args" =~ "verbose" ]]; then
options["verbose"]="true"
verbose_message "verbose to true" "set"
fi
# Load modules
if [ -d "$module_path" ]; then
modules=$( find "$module_path" -name "*.sh" )
for module in $modules; do
if [[ "$script_args" =~ "verbose" ]]; then
verbose_message "Module $module" "load"
fi
. "$module"
done
fi
# Reset defaults based on command line options
reset_defaults () {
if [ "${options['debug']}" = "true" ]; then
verbose_message "Enabling debug mode" "notice"
set -x
fi
if [ "${options['strict']}" = "true" ]; then
verbose_message "Enabling strict mode" "notice"
set -u
fi
if [ "${options['dryrun']}" = "true" ]; then
verbose_message "Enabling dryrun mode" "notice"
fi
}
# Selective exit (don't exit when we're running in dryrun mode)
do_exit () {
if [ "${options['dryrun']}" = "false" ]; then
exit
fi
}
# check value (make sure that command line arguments that take values have values)
check_value () {
parameter="$1"
value="$2"
if [[ "$value" =~ "--" ]]; then
verbose_message "Value '$value' for parameter '$parameter' looks like a parameter" "verbose"
echo ""
if [ "${options['force']}" = "false" ]; then
do_exit
fi
else
if [ "$value" = "" ]; then
verbose_message "No value given for parameter $parameter" "verbose"
echo ""
if [[ "$parameter" =~ "option" ]]; then
print_options
else
if [[ "$parameter" =~ "action" ]]; then
print_actions
else
print_help
fi
fi
exit
fi
fi
}
# Execute command
execute_command () {
command="$1"
privilege="$2"
if [ "$privilege" = "su" ]; then
command="sudo sh -c \"$command\""
fi
if [ "${options['verbose']}" = "true" ]; then
verbose_message "$command" "execute"
fi
if [ "${options['dryrun']}" = "false" ]; then
eval "$command"
fi
}
# Print help/usage insformation
print_help () {
script_help=$( grep -A1 "# switch" "$script_file" |sed "s/^--//g" |sed "s/# switch//g" | tr -s " " |grep -Ev "=|echo" |sed "s/#/ /g" | sed "/^\s*$/d" )
echo "Usage: $script_bin --switch [value]"
echo ""
echo "switches:"
echo "--------"
echo "$script_help"
echo ""
}
# Print actions
print_actions () {
script_actions=$( grep -A1 "# action" "$script_file" |sed "s/^--//g" |sed "s/# action//g" | tr -s " " |grep -Ev "=|echo" |sed "s/#/ /g" |sed "/^\s*$/d" )
echo "Usage: $script_bin --action(s) [value]"
echo ""
echo "actions:"
echo "-------"
echo "$script_actions"
echo ""
}
# Print options
print_options () {
script_options=$( grep -A1 "# option" "$script_file" |sed "s/^--//g" |sed "s/# option//g" | tr -s " " |grep -Ev "=|echo" |sed "s/#/ /g" |sed "/^\s*$/d" )
echo "Usage: $script_bin --option(s) [value]"
echo ""
echo "options:"
echo "-------"
echo "$script_options"
echo ""
}
# Print Usage
print_usage () {
usage="$1"
case $usage in
all|full)
print_help
print_actions
print_options
;;
help)
print_help
;;
action*)
print_actions
;;
option*)
print_options
;;
*)
print_help
shift
;;
esac
}
# Print version information
print_version () {
script_vers=$( grep '^# Version' < "$0" | awk '{print $3}' )
echo "$script_vers"
}
# Run Shellcheck
check_shellcheck () {
bin_test=$( command -v shellcheck | grep -c shellcheck )
if [ ! "$bin_test" = "0" ]; then
shellcheck "$script_file"
fi
}
# Do some early command line argument processing
if [ "$script_args" = "" ]; then
print_help
exit
fi
# Handle options
process_options () {
option_flag="$1"
if [[ "$option_flag" =~ ^no ]]; then
option_flag="${option_flag:2}"
value="false"
else
value="true"
fi
options["$option_flag"]="true"
verbose_message "$option_flag to $value" "set"
}
# Print environment
print_environment () {
echo "Environment (Options):"
for option in "${!options[@]}"; do
value="${options[${option}]}"
echo -e "Option $option\tis set to $value"
done
}
# Print defaults
print_defaults () {
echo "Defaults:"
for default in "${!defaults[@]}"; do
value="${defaults[${default}]}"
echo -e "Default $default\tis set to $value"
done
}
# Handle actions
process_actions () {
actions="$1"
case $actions in
help) # action
# Print actions help
print_actions
exit
;;
version) # action
# Print version
print_version
exit
;;
printenv*) # action
# Print environment
print_environment
exit
;;
printdefaults) # action
# Print defaults
print_defaults
exit
;;
shellcheck) # action
# Shellcheck script
check_shellcheck
exit
;;
*)
print_actions
exit
;;
esac
}
# Handle command line arguments
while test $# -gt 0; do
case $1 in
--action*) # switch
# Action to perform
check_value "$1" "$2"
action_flags+=("$2")
options["actions"]="true"
shift 2
;;
--debug) # switch
# Enable debug mode
options["debug"]="true"
shift
;;
--force) # switch
# Enable force mode
options["force"]="true"
shift
;;
--strict) # switch
# Enable strict mode
options["strict"]="true"
shift
;;
--verbose) # switch
# Enable verbos e mode
options["verbose"]="true"
shift
;;
--version|-V) # switch
# Print version information
print_version
exit
;;
--option*) # switch
# Action to perform
check_value "$1" "$2"
option_flags+=("$2")
options["options"]="true"
shift 2
;;
--usage*) # switch
# Action to perform
check_value "$1" "$2"
usage="$2"
print_usage "$usage"
shift 2
exit
;;
--help|-h) # switch
# Print help information
print_help
shift
exit
;;
*)
print_help
shift
exit
;;
esac
done
# Process options
if [ "${options['options']}" = "true" ]; then
for option_flag in "${option_flags[@]}"; do
if [[ "$option_flag" =~ "," ]]; then
IFS="," read -r -a array <<< "$option_flag"
for option in "${array[@]}"; do
process_options "$option"
done
else
process_options "$option_flag"
fi
done
fi
# Reset defaults based on switches
reset_defaults
# Process actions
if [ "${options['actions']}" = "true" ]; then
for action_flag in "${action_flags[@]}"; do
if [[ "$action_flag" =~ "," ]]; then
IFS="," read -r -a array <<< "$action_flag"
for action in "${array[@]}"; do
process_actions "$action"
done
else
process_actions "$action_flag"
fi
done
fi