-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmadlib.sh
executable file
·500 lines (447 loc) · 16.5 KB
/
madlib.sh
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#!/usr/bin/env bash
###################################################################
# Script Name : madlib.sh
# Description : Bash cookiecutter
# Author : onescriptkid
# Email : [email protected]
# Bash versioh : GNU bash, version 5.0.17(1)-release
###################################################################
# Setup unofficial bash strictmode, script_dir, and color formatting
setup() {
set -euo pipefail
# Get script directory
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
# Get color formatting
. "$SCRIPT_DIR/utils/bash_format_text_color.sh"
}
# Print help
usage() {
print_cyan "$(cat << EOF
Usage:
$0 [-hvsd] [-udgcapwy 0|1] <script-name>
Options:
-h show help [boolean]
-v show version [boolean]
-s skip prompt. use defaults [boolean]
-d debug mode [boolean]
-u unoffical bash strictmode [number] [required]
-S include script_dir [number] [required]
-g include git_repo_dir [number] [required]
-c include hues|colors|font [number] [required]
-a include parse_args [number] [required]
-y include a yaml parser [number] [required]
Examples:
$0 -s build.sh skips prompts. generates script quickly using defaults
$0 -a0 build.sh create a script without parse_args
$0 -h shows help
$0 -u1 -S1 -g1 -c1 -a1 -y1 build.sh give me everything overzealously
CopyPasta:
$0 -s build.sh
EOF
)"
}
# Upon finish, show success or failure depending on the exit code
trap "finish" EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
finish() {
local exit_code="$?"
if [ "$exit_code" != "0" ]; then
print_info ""
usage
print_info ""
print_failure "$0 failed with code $exit_code"
else
print_info ""
helpful_tip
print_info ""
fi
exit $exit_code
}
# Read in user options and positional arguments
# - FLAG_SET_* is set if the user overrides the default
# - OPTION_* is the value the user uses to override
# - ARG_SET_* is set if the user sets a positional argument
read_args() {
# Outer loop handles positional arguments
POSITIONAL_ARGS=()
while [ $# -gt 0 ]; do
unset OPTIND
unset OPTARG
# Inner loop handles options
while getopts ":hvdsu:S:g:c:a:p:" opt; do
case $opt in
h)
usage
exit 0
;;
v)
printf "no version set\n"
exit 0
;;
s)
FLAG_SET_MADLIB_SKIP_PROMPTS=1
OPTION_MADLIB_SKIP_PROMPTS=1
;;
d)
FLAG_SET_MADLIB_DEBUG=1
OPTION_MADLIB_DEBUG=1
;;
u)
if [ "$OPTARG" != "0" ] && [ "$OPTARG" != "1" ]; then print_error "Option -${opt} should be <0|1> not $OPTARG"; exit 1; fi
OPTION_MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE="$OPTARG"
FLAG_SET_MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE=1
;;
S)
if [ "$OPTARG" != "0" ] && [ "$OPTARG" != "1" ]; then print_error "Option -${opt} should be <0|1> not $OPTARG"; exit 1; fi
OPTION_MADLIB_TOGGLE_SCRIPT_DIR="$OPTARG"
FLAG_SET_MADLIB_TOGGLE_SCRIPT_DIR=1
;;
g)
if [ "$OPTARG" != "0" ] && [ "$OPTARG" != "1" ]; then print_error "Option -${opt} should be <0|1> not $OPTARG"; exit 1; fi
OPTION_MADLIB_TOGGLE_GIT="$OPTARG"
FLAG_SET_MADLIB_TOGGLE_GIT=1
;;
c)
if [ "$OPTARG" != "0" ] && [ "$OPTARG" != "1" ]; then print_error "Option -${opt} should be <0|1> not $OPTARG"; exit 1; fi
OPTION_MADLIB_TOGGLE_FORMAT_TEXT="$OPTARG"
FLAG_SET_MADLIB_TOGGLE_FORMAT_TEXT=1
;;
a)
if [ "$OPTARG" != "0" ] && [ "$OPTARG" != "1" ]; then print_error "Option -${opt} should be <0|1> not $OPTARG"; exit 1; fi
OPTION_MADLIB_TOGGLE_PARSE_ARGS="$OPTARG"
FLAG_SET_MADLIB_TOGGLE_PARSE_ARGS=1
;;
p)
if [ "$OPTARG" != "0" ] && [ "$OPTARG" != "1" ]; then print_error "Option -${opt} should be <0|1> not $OPTARG"; exit 1; fi
OPTION_MADLIB_TOGGLE_PROGRESS_BAR="$OPTARG"
FLAG_SET_MADLIB_TOGGLE_PROGRESS_BAR=1
print_error "progress_bar to be implemented in a future version"
exit 1
;;
y)
if [ "$OPTARG" != "0" ] && [ "$OPTARG" != "1" ]; then print_error "Option -${opt} should be <0|1> not $OPTARG"; exit 1; fi
OPTION_MADLIB_TOGGLE_YAML_PARSER="$OPTARG"
FLAG_SET_MADLIB_TOGGLE_YAML_PARSER=1
print_error "yaml_parser to be implemented in a future version"
exit 1
;;
w)
if [ "$OPTARG" != "0" ] && [ "$OPTARG" != "1" ]; then print_error "Option -${opt} should be <0|1> not $OPTARG"; exit 1; fi
FLAG_SET_MADLIB_TOGGLE_WAIT_FOR_IT=1
OPTION_MADLIB_TOGGLE_WAIT_FOR_IT="$OPTARG"
print_error "wait-for-it to be implemented in a future version"
exit 1
;;
':')
print_error "Missing argument for option -- ${OPTARG:-optarg-missing}" >&2
exit 1
;;
\?)
print_error "Unknown option -- ${OPTARG:-optarg-missing}" >&2
exit 1
;;
*)
print_error "Unimplemented option -- ${opt}" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
# Gather positional arguments. Conditional prevents unset $1 variable issues
if [ -z ${1+x} ]; then
:
else
ARG_SET_MADLIB_SCRIPT_NAME=1
POSITIONAL_ARGS+=("$1")
shift
fi
done
# Handle positional args
# > 1 Error if there's more than 1 pos arg for scriptname - unittest.sh compile.sh transpile.sh
# = 0 Show warning if there's no positional argument set - Use default scriptname
# = 1 Set to the user defined positional argument
# There shouldn't be more than 1 positional argument, it's reserved for the scriptname!
if [ ${#POSITIONAL_ARGS[@]} -gt 1 ]; then
print_error "There should only be 1 positional argument. Found -- ${POSITIONAL_ARGS[*]}"
exit 1
elif [ ${#POSITIONAL_ARGS[@]} -eq 0 ]; then
MADLIB_SCRIPT_NAME=$DEFAULT_MADLIB_SCRIPT_NAME
else
MADLIB_SCRIPT_NAME=${POSITIONAL_ARGS[0]}
fi
# Set global template variables according to the incoming options set by the user
MADLIB_SKIP_PROMPTS=${OPTION_MADLIB_SKIP_PROMPTS:-$DEFAULT_MADLIB_SKIP_PROMPTS}
MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE=${OPTION_MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE:-$DEFAULT_MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE}
MADLIB_TOGGLE_SCRIPT_DIR=${OPTION_MADLIB_TOGGLE_SCRIPT_DIR:-$DEFAULT_MADLIB_TOGGLE_SCRIPT_DIR}
MADLIB_TOGGLE_GIT=${OPTION_MADLIB_TOGGLE_GIT:-$DEFAULT_MADLIB_TOGGLE_GIT}
MADLIB_TOGGLE_FORMAT_TEXT=${OPTION_MADLIB_TOGGLE_FORMAT_TEXT:-$DEFAULT_MADLIB_TOGGLE_FORMAT_TEXT}
MADLIB_TOGGLE_YAML_PARSER=${OPTION_MADLIB_TOGGLE_YAML_PARSER:-$DEFAULT_MADLIB_TOGGLE_YAML_PARSER}
MADLIB_TOGGLE_PARSE_ARGS=${OPTION_MADLIB_TOGGLE_PARSE_ARGS:-$DEFAULT_MADLIB_TOGGLE_PARSE_ARGS}
MADLIB_TOGGLE_PROGRESS_BAR=${OPTION_MADLIB_TOGGLE_PROGRESS_BAR:-$DEFAULT_MADLIB_TOGGLE_PROGRESS_BAR}
MADLIB_TOGGLE_WAIT_FOR_IT=${OPTION_MADLIB_TOGGLE_WAIT_FOR_IT:-$DEFAULT_MADLIB_TOGGLE_WAIT_FOR_IT}
DEBUG=${OPTION_MADLIB_DEBUG:-$DEFAULT_MADLIB_DEBUG}
# DEBUG - Show template variables for debugging
print_debug "Template variables set by user or using defaults as a fallback"
print_debug "MADLIB_SKIP_PROMPTS= $MADLIB_SKIP_PROMPTS"
print_debug "MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE= $MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE"
print_debug "MADLIB_TOGGLE_SCRIPT_DIR= $MADLIB_TOGGLE_SCRIPT_DIR"
print_debug "MADLIB_TOGGLE_GIT= $MADLIB_TOGGLE_GIT"
print_debug "MADLIB_TOGGLE_FORMAT_TEXT= $MADLIB_TOGGLE_FORMAT_TEXT"
print_debug "MADLIB_TOGGLE_PARSE_ARGS= $MADLIB_TOGGLE_PARSE_ARGS"
print_debug "MADLIB_TOGGLE_YAML_PARSER= $MADLIB_TOGGLE_YAML_PARSER"
print_debug "MADLIB_SCRIPT_NAME= $MADLIB_SCRIPT_NAME"
print_debug "DEBUG= $DEBUG"
print_debug ""
print_debug "Flags"
print_debug "FLAG_SET_MADLIB_SKIP_PROMPTS= $FLAG_SET_MADLIB_SKIP_PROMPTS"
print_debug "FLAG_SET_MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE= $FLAG_SET_MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE"
print_debug "FLAG_SET_MADLIB_TOGGLE_SCRIPT_DIR= $FLAG_SET_MADLIB_TOGGLE_SCRIPT_DIR"
print_debug "FLAG_SET_MADLIB_TOGGLE_GIT= $FLAG_SET_MADLIB_TOGGLE_GIT"
print_debug "FLAG_SET_MADLIB_TOGGLE_FORMAT_TEXT= $FLAG_SET_MADLIB_TOGGLE_FORMAT_TEXT"
print_debug "FLAG_SET_MADLIB_TOGGLE_PARSE_ARGS= $FLAG_SET_MADLIB_TOGGLE_PARSE_ARGS"
print_debug "FLAG_SET_MADLIB_TOGGLE_YAML_PARSER= $FLAG_SET_MADLIB_TOGGLE_YAML_PARSER"
print_debug "DEBUG= $DEBUG"
print_debug "ARG_SET_MADLIB_SCRIPT_NAME= $ARG_SET_MADLIB_SCRIPT_NAME"
}
# Set default values for template variables
set_defaults() {
DEFAULT_MADLIB_SKIP_PROMPTS=0
DEFAULT_MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE=1
DEFAULT_MADLIB_TOGGLE_SCRIPT_DIR=1
DEFAULT_MADLIB_TOGGLE_GIT=1
DEFAULT_MADLIB_TOGGLE_FORMAT_TEXT=1
DEFAULT_MADLIB_TOGGLE_PARSE_ARGS=1
DEFAULT_MADLIB_TOGGLE_PROGRESS_BAR=0
DEFAULT_MADLIB_TOGGLE_WAIT_FOR_IT=0
DEFAULT_MADLIB_TOGGLE_YAML_PARSER=0
DEFAULT_MADLIB_SCRIPT_NAME="madlib_script.sh"
DEFAULT_MADLIB_DEBUG=0
FLAG_SET_MADLIB_SKIP_PROMPTS=0
FLAG_SET_MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE=0
FLAG_SET_MADLIB_TOGGLE_SCRIPT_DIR=0
FLAG_SET_MADLIB_TOGGLE_GIT=0
FLAG_SET_MADLIB_TOGGLE_FORMAT_TEXT=0
FLAG_SET_MADLIB_TOGGLE_PARSE_ARGS=0
FLAG_SET_MADLIB_TOGGLE_YAML_PARSER=0
ARG_SET_MADLIB_SCRIPT_NAME=0
FLAG_SET_MADLIB_DEBUG=0
}
# Prompt the user for what should be included in their bash script - script_dir, git, argparsing, etc
# - If a user sets skip prompts, skip prompts
# - If a user set a flag already, skip that prompt.
prompts() {
print_debug ""
# Should we skip prompts?
if [ "$MADLIB_SKIP_PROMPTS" == 1 ]; then
print_debug "Skipping prompts ..."
return
fi
# Should we add bash unofficial strict mode?
if [ "$FLAG_SET_MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE" == 0 ] ; then
print_bold "📁 Do you want $(print_italic unoffical bash strict mode)?"
while true; do
read -r -n1 -p "Type $(print_italic y) / $(print_italic n): " input
case $input in
[nN]* )
MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE=0
break
;;
[yY]*)
MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE=1
break
;;
esac
echo ""
done
echo ""
fi
# Should we add a script_dir
if [ "$FLAG_SET_MADLIB_TOGGLE_SCRIPT_DIR" == 0 ] ; then
print_bold "🔨 Do you want $(print_italic SCRIPT_DIR)?"
while true; do
read -r -n1 -p "Type $(print_italic y) / $(print_italic n): " input
case $input in
[nN]* )
MADLIB_TOGGLE_SCRIPT_DIR=0
break
;;
[yY]*)
MADLIB_TOGGLE_SCRIPT_DIR=1
break
;;
esac
echo ""
done
echo ""
fi
# Should we add a git references - GIT_REPO, COMMIT_HASH, and BRANCH?
if [ "$FLAG_SET_MADLIB_TOGGLE_GIT" == 0 ]; then
print_bold "🎄 Is this script in a $(print_italic git repo)?"
while true; do
read -r -n1 -p "Type $(print_italic y) / $(print_italic n): " input
case $input in
[nN]* )
MADLIB_TOGGLE_GIT=0
break
;;
[yY]*)
MADLIB_TOGGLE_GIT=1
break
;;
esac
echo ""
done
echo ""
fi
# Should we add colored output?
if [ "$FLAG_SET_MADLIB_TOGGLE_FORMAT_TEXT" == 0 ]; then
print_bold "🎨 Do you want colored output?"
while true; do
read -r -n1 -p "Type $(print_italic y) / $(print_italic n): " input
case $input in
[nN]* )
MADLIB_TOGGLE_FORMAT_TEXT=0
break
;;
[yY]*)
MADLIB_TOGGLE_FORMAT_TEXT=1
break
;;
esac
echo ""
done
echo ""
fi
# Should we add argument and option parsing?
if [ "$FLAG_SET_MADLIB_TOGGLE_PARSE_ARGS" == 0 ]; then
print_bold "🤖 Do you want arg and option parsing?"
while true; do
read -r -n1 -p "Type $(print_italic y) / $(print_italic n): " input
case $input in
[nN]* )
MADLIB_TOGGLE_FORMAT_TEXT=0
break
;;
[yY]*)
MADLIB_TOGGLE_FORMAT_TEXT=1
break
;;
esac
echo ""
done
echo ""
fi
# Should we add a shell only yaml parser?
if [ "$FLAG_SET_MADLIB_TOGGLE_YAML_PARSER" == 0 ]; then
print_bold "⭐ Do you want yaml parsing?"
while true; do
read -r -n1 -p "Type $(print_italic y) / $(print_italic n): " input
case $input in
[nN]* )
MADLIB_TOGGLE_YAML_PARSER=0
break
;;
[yY]*)
MADLIB_TOGGLE_YAML_PARSER=1
break
;;
esac
echo ""
done
echo ""
fi
# DEBUG
print_debug "MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE= $MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE"
print_debug "MADLIB_TOGGLE_SCRIPT_DIR= $MADLIB_TOGGLE_SCRIPT_DIR"
print_debug "MADLIB_TOGGLE_GIT= $MADLIB_TOGGLE_GIT"
print_debug "MADLIB_TOGGLE_FORMAT_TEXT= $MADLIB_TOGGLE_FORMAT_TEXT"
print_debug "MADLIB_TOGGLE_PARSE_ARGS= $MADLIB_TOGGLE_PARSE_ARGS"
print_debug "MADLIB_TOGGLE_YAML_PARSER= $MADLIB_TOGGLE_YAML_PARSER"
}
create_scratch_directory(){
SCRATCH="$(mktemp -d -t madlib-XXXXXXXXXX)"
chmod 755 "$SCRATCH"
print_debug "Creating scratch dir $SCRATCH"
}
generate_script_mo(){
print_debug "Generating script from template (mo).."
# Save original directory to later output script to correct location
ORIGINAL_DIR=$(pwd)
pushd "$SCRATCH" > /dev/null
# Export script_name to be used in mustache template
export MADLIB_SCRIPT_NAME
# Copy bash_template to scratch directory
set -a
cp "$SCRIPT_DIR/templates/bash_template.sh.mustache" "$SCRATCH"/"$MADLIB_SCRIPT_NAME"
# Apply bash strict mode
if [ "$MADLIB_TOGGLE_UNOFFICIAL_BASH_STRICT_MODE" = "1" ]; then
MADLIB_UNOFFCIAL_BASH_STRICT_MODE_CONTENT=$(<"$SCRIPT_DIR/utils/bash_unofficial_bash_strictmode.sh")
else
MADLIB_UNOFFCIAL_BASH_STRICT_MODE_CONTENT=""
fi
# Apply script_dir
if [ "$MADLIB_TOGGLE_SCRIPT_DIR" = "1" ]; then
MADLIB_SCRIPT_DIR_CONTENT=$(<"$SCRIPT_DIR/utils/bash_script_dir.sh")
else
MADLIB_SCRIPT_DIR_CONTENT=""
fi
# Apply git
if [ "$MADLIB_TOGGLE_GIT" = "1" ]; then
MADLIB_GIT_CONTENT=$(<"$SCRIPT_DIR/utils/bash_git.sh")
else
MADLIB_GIT_CONTENT=""
fi
# Apply colors
if [ "$MADLIB_TOGGLE_FORMAT_TEXT" = "1" ]; then
MADLIB_FORMAT_TEXT_CONTENT=$(<"$SCRIPT_DIR/utils/bash_format_text_color.sh")
else
MADLIB_FORMAT_TEXT_CONTENT=$(<"$SCRIPT_DIR/utils/bash_format_text.sh")
fi
# Apply parse_args
if [ "$MADLIB_TOGGLE_PARSE_ARGS" = "1" ]; then
MADLIB_PARSE_ARGS_CONTENT=$(<"$SCRIPT_DIR/utils/bash_parse_args.sh")
else
MADLIB_PARSE_ARGS_CONTENT=""
fi
# Apply parse_args part 2
if [ "$MADLIB_TOGGLE_PARSE_ARGS" = "1" ]; then
MADLIB_NAME_PARSE_ARGS_CONTENT="read_args \"\$@\""
else
MADLIB_NAME_PARSE_ARGS_CONTENT=""
fi
# Apply yaml parser
if [ "$MADLIB_TOGGLE_YAML_PARSER" = "1" ]; then
MADLIB_TOGGLE_YAML_PARSER_CONTENT=$(<"$SCRIPT_DIR/utils/bash_yaml_parser.sh")
else
MADLIB_TOGGLE_YAML_PARSER_CONTENT=""
fi
# If a user set the scriptname, copy the script
MADLIB_OUTPUT=""
if [ "$ARG_SET_MADLIB_SCRIPT_NAME" == 1 ]; then
MADLIB_OUTPUT="$ORIGINAL_DIR"/"$MADLIB_SCRIPT_NAME"
cp -a "$SCRATCH"/"$MADLIB_SCRIPT_NAME" "$MADLIB_OUTPUT"
else
MADLIB_OUTPUT="$SCRATCH/$MADLIB_SCRIPT_NAME"
fi
# Find and replace with mustache templating
chmod 755 "$SCRIPT_DIR"/mo
"$SCRIPT_DIR"/mo "$SCRIPT_DIR/templates/bash_template.sh.mustache" > "$MADLIB_OUTPUT"
set +a
# Make the script executable
chmod 755 "$MADLIB_OUTPUT"
popd > /dev/null
}
helpful_tip() {
# Show helpful tip
print_success "Created script:"
print_body ""
print_cyan "\t$MADLIB_OUTPUT"
}
setup
set_defaults
read_args "$@"
prompts
create_scratch_directory
generate_script_mo