-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.sh
524 lines (478 loc) · 17.3 KB
/
play.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
if [ "$program_path" == "" ] \
|| [ "$program_name" == "" ] \
|| [ "$program_bin_dir" == "" ] \
|| [ "$program_lib_dir" == "" ] \
; then
program_path=`realpath $0`
program_name=`basename "$path"`
program_bin_dir=`dirname $path`
program_lib_dir="${bin_dir/\/bin/\/lib/}"
fi
play() {(
####### Begin of scripting clockworks #########################################
program_name="$program_name"
program_arguments=(action_name)
declare -a arguments
parse_arguments(){
function_arguments="$1"
shift
# Empty the argument stack, but do not unset variable
while [ "${#arguments[@]}" -gt 0 ] ; do
arguments=("${arguments[@]:1}")
done
arg_expects_value(){(
if [[ "$1" =~ [a-zA-Z0-9_]+[?]?=[?]?$ ]]; then
echo "${1}" | tr -d '='
else
echo ""
fi
)}
arg_is_optional(){(
if [[ "$1" =~ [a-zA-Z0-9_]+[=]?\?[=]?$ ]]; then
echo ${1#--} | tr -d '?'
else
echo ""
fi
)}
arg_is_flag(){(
if [ ! "`arg_is_long_option $1`" == "" ]; then
echo ""
else
if [[ "$1" =~ ^-[a-zA-Z0-9_]+ ]]; then
local flags=""
local args="${1#-}"
local modifiers="";
if [ "$(arg_is_optional "$args")" != "" ]; then
args="$(arg_is_optional "$args")"
modifiers="${modifiers}?";
fi
if [ "$(arg_expects_value "$args")" != "" ]; then
args="$(arg_expects_value "$args")"
modifiers="${modifiers}=";
fi
for (( i=0; i<${#args}; i++ )); do
flags="$flags ${args:$i:1}${modifiers}"
done
echo "$flags"
else
echo ""
fi
fi
)}
arg_is_long_option(){(
[[ "$1" =~ ^--[a-zA-Z0-9_]+ ]] && echo ${1#--} || echo ""
)}
arg_is_value(){(
if [ "`arg_is_long_option "$1"`" == "" ] \
&& [ "`arg_is_flag "$1"`" == "" ] \
; then
echo "$1"
else
echo ""
fi
)}
arg_name(){(
output="$1"
if [ "`arg_is_value "$output"`" == "" ]; then
output="${output##-}"
output="${output##-}"
[ ! "`arg_is_optional "$output"`" == "" ] \
&& output="`arg_is_optional "$output"`"
[ ! "`arg_expects_value "$output"`" == "" ] \
&& output="`arg_expects_value "$output"`"
fi
echo "$output"
)}
arg_pending_for_val=""
declare -a possible_arguments
declare -a possible_options
declare -a possible_flags
declare -A values
required_values=0
supplied_values=0
declare -a args
for arg in "$@"; do args+=("$arg"); done
for arg in $function_arguments; do
if [ "`arg_is_value "$arg"`" != "" ]; then
possible_arguments+=("$arg")
[ "`arg_is_optional "$arg"`" == "" ] && \
required_values=$(( 1 + ${required_values:-0} ))
else
if [ `arg_is_long_option "$arg"` ]; then
arg="`arg_is_long_option "$arg"`"
possible_options+=("$arg")
else
for flag in `arg_is_flag "$arg"`; do
[ "$flag" == "=" ] && continue;
[ "$flag" == "?" ] && continue;
possible_flags+=("$flag")
done
fi
fi
done
ignore_current_argument(){
arguments+=( "${args[0]}" )
args=("${args[@]:1}")
}
assign_arg_to_pending_value(){
values+=([$arg_pending_for_val]="${args[0]}")
args=("${args[@]:1}")
arg_pending_for_val=""
}
assign_arg_to_possible_argument(){
values+=([$possible_arguments]="${args[0]}")
possible_arguments=("${possible_arguments[@]:1}")
args=("${args[@]:1}")
}
assign_optional_arg_to_possible_argument(){
possible_argument=`arg_is_optional "$possible_arguments"`
if [[ ! ${#args[@]} -gt 0 ]]; then
values+=([$possible_argument]="")
else
values+=([$possible_argument]="${args[0]}")
args=("${args[@]:1}")
fi
possible_arguments=("${possible_arguments[@]:1}")
}
assign_arg_to_long_option(){
arg="`arg_is_long_option "${args[0]}"`"
match_opt=""
match_opt_expects_value=false
for opt in "${possible_options[@]}"; do
if [ `arg_is_optional "$opt"` ]; then
opt=`arg_is_optional "$opt"`
fi
if [ `arg_expects_value "$opt"` ]; then
opt=`arg_expects_value "$opt"`
expects_value=true
else
expects_value=false
fi
opt="${opt//\?/}"
opt="${opt//=/}"
if [ "$arg" == "$opt" ]; then
match_opt="$opt"
match_opt_expects_value=$expects_value
fi
done
if [ ! "$match_opt" ]; then
arguments+=( ${args[0]} )
else
if [ $match_opt_expects_value == true ]; then
arg_pending_for_val="$match_opt"
else
values+=( [$match_opt]=$(( ${values[$match_opt]:-0} + 1 )) )
fi
fi
args=("${args[@]:1}")
}
assign_arg_to_flag(){
arg="`arg_is_flag "${args[0]}"`"
for flag in $arg; do
match_flag=""
match_flag_expects_value=false
for possible_flag in "${possible_flags[@]}"; do
if [ `arg_is_optional "$possible_flag"` ]; then
possible_flag=`arg_is_optional "$possible_flag"`
fi
if [ `arg_expects_value "$possible_flag"` ]; then
possible_flag=`arg_expects_value "$possible_flag"`
expects_value=true
else
expects_value=false
fi
possible_flag="${possible_flag//\?/}"
possible_flag="${possible_flag//=/}"
if [ "$flag" == "$possible_flag" ]; then
match_flag="$flag"
match_flag_expects_value=$expects_value
fi
done
if [ ! "$match_flag" ]; then
arguments+=( "-$flag" )
else
if [ $match_flag_expects_value == true ]; then
arg_pending_for_val="$match_flag"
else
values+=( [$match_flag]=$(( ${values[$match_flag]:-0} + 1 )) )
fi
fi
done
args=("${args[@]:1}")
}
while [ "${#args[@]}" -gt 0 ] ; do
arg="${args[0]}"
if [ ! "${#possible_arguments[@]}" -gt 0 ]; then
dont_ignore_this_arg=false
if [ ! "`arg_is_value "$arg"`" == "" ]; then
if [ "$arg_pending_for_val" ]; then
assign_arg_to_pending_value
continue
fi
fi
if [ ! "`arg_is_long_option "$arg"`" == "" ]; then
opt="`arg_is_long_option "$arg"`"
opt="${opt//\?/}"; opt="${opt//=/}"
for possible_option in "${possible_options[@]}"; do
possible_option="${possible_option//\?/}"
possible_option="${possible_option//=/}"
if [ "${opt}" == "$possible_option" ]; then
dont_ignore_this_arg=true
assign_arg_to_long_option
break;
fi
done
[ $dont_ignore_this_arg == true ] && continue
fi
if [ ! "`arg_is_flag "$arg"`" == "" ]; then
flg="`arg_is_flag "$arg"`"
flg="${flg//\?/}"; flg="${flg//=/}"
flg="`echo ${flg//\?/}`"
for possible_flag in "${possible_flags[@]}"; do
possible_flag="${possible_flag//\?/}"
possible_flag="${possible_flag//=/}"
if [ "${flg}" == "$possible_flag" ]; then
dont_ignore_this_arg=true
assign_arg_to_flag
break;
fi
done
[ $dont_ignore_this_arg == true ] && continue
fi
if [ $dont_ignore_this_arg == false ]; then
ignore_current_argument
fi
else
possible_argument="$possible_arguments"
if [ "`arg_is_long_option "$arg"`" == "" ] \
&& [ "`arg_is_flag "$arg"`" == "" ] \
; then
if [ "$arg_pending_for_val" ]; then
assign_arg_to_pending_value
continue
fi
supplied_values=$(( 1 + ${supplied_values:-0} ))
if [ ! "`arg_is_optional "$possible_argument"`" == "" ]; then
assign_optional_arg_to_possible_argument
else
assign_arg_to_possible_argument
fi
else
if [ ! "`arg_is_long_option "$arg"`" == "" ]; then
assign_arg_to_long_option
else
assign_arg_to_flag
fi
fi
fi
done
for key in ${!values[@]}; do
eval "${key}=${values[$key]}";
done
if [ ! $supplied_values -ge $required_values ]; then
[ ! ${h:-0} -ge 0 ] && echo "ERROR: wrong number of arguments"
h=$(( 1 + ${h:-0} ))
fi
}
get_config_value(){(
variable_name="$1"
default_value="$2"
regex_key="[[:space:]]*$variable_name[[:space:]]*"
regex_value='[[:space:]]*"\?\([^"]*\)"\?[[:space:]]*'
regex="^$regex_key=$regex_value\$"
value="$default_value"
for conf_file in \
"/etc/$program_name/config" \
"/etc/$program_name.conf" \
"$HOME/.$program_name/config" \
"$HOME/.$program_name.conf" \
; do
if [[ -f "$conf_file" ]]; then
line="$(
egrep "^$regex_key=" "$conf_file" || echo "" )"
if [ ! "$line" == "" ]; then
value="$( echo "$line" \
| sed "s/$regex/\1/" \
| head -n 1 )"
fi
fi
done
echo "$value"
)}
if [[ -f "$program_lib_dir/${program_name}/config.sh" ]]; then
source "$program_lib_dir/${program_name}/config.sh"
fi
declare -A help_messages
declare -A available_actions
declare -A action_arguments
set_action(){
action="$1"
shift;
args="$1"
shift;
one_liner="$1"
shift;
body=""
for line in "$@"; do
[ "$body" == "" ] && body="${line}" || body="${body}\n${line}";
done
available_actions["${action}"]="$one_liner"
action_arguments["${action}"]="$args"
options_with_arguments+=(
$(echo $args | tr " " "\n" | grep '=' | tr -d "=" | tr -d "?" )
)
set_help "$program_name/$action" "$body"
}
set_help(){
key="$1"
shift;
body=""
for line in "$@"; do
[ "$body" == "" ] && body="${line}" || body="${body}\n${line}";
done
help_messages["$key"]="$body"
}
get_help(){(
show_help \
"$program_name" \
"$action_name" \
"$project_name" \
"$instance_name" \
$@; return $?
)}
show_help(){(
target=default
key=""
for argument in $@; do
[[ "$key" == "" ]] && key="$argument" || key="$key/$argument"
if [ "${help_messages[$key]+exist}" ]; then
target="${key}"
else
if [ -d "$sources_folder/$argument" ]; then
if [ "${help_messages["${key}/project"]+exist}" ]; then
target="${key}/project"
fi
fi
fi
done
message="${help_messages["$target"]}"
while [[ "$message" =~ (.*)%=(.*)%(.*) ]]; do
pre="${BASH_REMATCH[1]}"
match="${BASH_REMATCH[2]}"
post="${BASH_REMATCH[3]}"
message="$pre$(eval ${match})$post"
done
while [[ "$message" =~ .*%([a-zA-Z0-9_]+)%.* ]]; do
match="${BASH_REMATCH[1]}"
value="${!match}"
message="$(echo "$message" | sed "s/%$match%/${value:-<$match>}/g")"
done
echo -e "$message"
return -1
)}
get_help_variable_list(){(
if [[ -f "$sources_folder/$project_name/.env" ]]; then
echo "$(
sed \
-e 's/^[\s]*//g' \
-e 's/^\(.*\)=/ --\L\1=/g' \
-e 's/=\(.*\)$/ [\1]/g' \
-e 's/ \[\]/ "<value>"/g' \
-e 's/\(--compose_project_name \)\[.*\]/\1['"$project_name-${instance_name:-<instance_name>}"']/g' \
-e 's/$/ \\\\/g' \
-e '$ s/ \\\\//g' \
"$sources_folder/$project_name/.env"
)"
else
echo " --<variable> <value> \\"
echo " --<variable> <value> \\"
echo " --<variable> <value> \\"
echo " ..."
echo " --<variable> <value> "
fi
)}
get_folder_size(){(
folder="$1"
du -csh "$folder" \
| tail -n 1 \
| sed 's/[ ]*total//g'
)}
########
# Main #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
########
set_help "$program_name" \
"Usage:" \
" $program_name <action> [arguments]" \
"" \
"Commands:" \
"%=$program_name actions -d%" \
""
main(){(
if [ "$action_name" == "" ]; then
get_help $@; return $?
else
if [ "${available_actions[$action_name]+exist}" == "exist" ]; then
parse_arguments "${action_arguments[$action_name]}" $@
if [ ${h:=0} -gt 0 ]; then
get_help $@; return $?
else
$action_name ${arguments[@]}
fi
else
echo "ERROR: unknown action “$action_name”"
get_help $@; return $?
fi
fi
)}
###########
# Actions #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
###########
set_action "actions" "-d"\
"List availible actions in this program" \
"Usage:" \
" %program_name% %action_name%" \
"" \
"Or if you want to list actions with a short description:" \
" %program_name% %action_name% -d" \
"" \
""
actions(){(
[[ "$d" -gt 0 ]] && show_description=true;
if [[ $show_description == true ]]; then
max_length=0
for action in ${!available_actions[@]}; do
if [ ${#action} -gt $max_length ]; then
max_length=${#action}
fi
done
action_column_width=$(( $max_length + 4 ))
cols="$(tput cols 2>/dev/null || echo 80)"
description_column_width=$(( $cols - $action_column_width - 2 ))
padding="$(printf '%*s' $action_column_width "")"
for action in ${!available_actions[@]}; do
head="${action}${padding::-${#action}}"
line="$(
echo "${available_actions[${action}]}" \
| fmt -w $description_column_width
)"
echo "${line}" | sed \
-e "1 s/^/ ${head::-2}/g" \
-e "1!s/^/ $padding/g"
done
else
for action in ${!available_actions[@]}; do
echo ${action}
done
fi
return 0
)}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
if [ -d "$action_definitions_folder" ]; then
for action in "$action_definitions_folder"/*.sh; do
source "$action"
done
fi
# Execute main function
# and return the exit code
parse_arguments "action_name -h?" $@
main "${arguments[@]}"; return $?)}