-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy
executable file
·700 lines (579 loc) · 23.3 KB
/
my
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#!/bin/bash
__Author="Jerren Saunders"
__Version=24.11.28
__ScriptName=$(basename "$0") # File name with extension
__AppDir=$(dirname "$0") # Path where script is stored
__AppName=${__ScriptName%.*} # File name without extension
__origArgs=$* # Capture all of the original arguments
function print_usage() {
includeAppInfo=${1}
cat <<EOT
This script will search the current directory tree for a file named '${MY_CUSTOM_FILE}', containing
a key that matches the first argument. If found, the value of that key will be used instead.
This can be used to allow local overriding of certain commands to point to a container
instead of the local installed command.
USAGE:
${__ScriptName} [OPTIONS] [help | version | set | list [-l] | KEY [args...]]
<KEY> [...args] The key of the command to run
list [-l] List the available command keys.
Includemy he the '-l' arg to list one key per line. Default is column view.
set <key> <command> Adds or updates the 'key' to the ${MY_CUSTOM_FILE} file in the current directory.
The given command will be used whenever called from this level or a descendant.
update [diff] Update the script with the latest version. If 'diff' is given, then show the changes
help Show usage
version Show version number
OPTIONS:
-v Verbose Level (Multiple may be given to increase the verbosity)
-d Dry Run.
EOT
if [[ "${includeAppInfo}" = "true" ]]; then
cat <<EOT
By: ${__Author}
Version: ${__Version}
EOT
fi
# Print options values if verbose mode
log 2 "Parsed Options:"
log 2 " DryRun: ${DRYRUN}"
log 2 " Verbose: ${VERBOSE}"
log 2 " File: ${MY_CUSTOM_FILE}"
log 2 ""
}
# Default Settings
VERBOSE=0
DRYRUN=false
MY_CUSTOM_FILE=.myCommand
# Prints log message to the terminal
# <int> lvl - Print Level: Only print this message if the VERBOSE level is greater than or equal to this
# <string> msg - Message to print
function log() {
local lvl=$1
local msg=$2
if (( lvl <= VERBOSE )); then
echo -e "$msg"
fi
}
while getopts 'dv' option; do
# echo "Option: ${option}"
case ${option} in
d ) DRYRUN=true
log 0 "---- THIS IS A DRYRUN! ----\n"
;;
v ) VERBOSE=$((VERBOSE + 1)) ;;
* ) # Do Nothing
esac
done
shift $((OPTIND -1)) # Remaining args will be left
# Either runs the command or prints the command that would be executed if DRYRUN is 'true'
# <cmd> cmd - Command to execute or print
function runCMD() {
if [[ "${DRYRUN}" == "true" ]]; then
log 0 "CMD: $*"
else
eval "$*"
fi
}
# Function to add or update a key-value pair in an INI file
# Usage: set_ini_value INI_FILE "section.key" "value"
#
# Example usage:
# set_ini_value "config.ini" "section.key" "value"
# set_ini_value "config.ini" "key" "value"
function set_ini_value() {
local ini_file=$1
local key=$2
local value=$3
local section=""
# Check if the key contains a section
if [[ "$key" == *.* ]]; then
# Extract section and key name
section="${key%%.*}"
key="${key#*.}"
log 1 "Section Key identified - Section: '${section}', Key: '${key}'"
else
log 1 "Key identified - Key: '${key}'"
fi
# If the section is not empty, handle section
if [[ -n $section ]]; then
log 1 "Adding key to section ${section}..."
# Set the awk debug prints if at verbose 2 or higher
if [[ ${VERBOSE} -ge 2 ]]; then awkLogOut=/dev/stderr; else awkLogOut=/dev/null; fi
# Add or update the key within the section
awk -v section="$section" -v key="$key" -v value="$value" -v ts="$(date)" -v user="$USER" -v logOut="$awkLogOut" '
BEGIN {
found_section=0;
found_key=0;
}
!found_key {print " Inspecting line " NR ": " $0 > logOut}
# Match the target section header
/^\['"$section"'\]/ {
print "Found Target Section at line " NR > logOut
found_section=1;
print;
next;
}
# If we are in the target section and we find the key, update its value
found_section && $1 ~ "^" key "=" {
print "Found Section Key at line " NR > logOut
print key"="value;
found_key=1;
next;
}
# If we find a new section header and we have not found the key, add it
found_section && /^\[.*\]/ && !found_key {
print "Reached section " $section " on line " NR " without detected existing key - Inserting" > logOut
print "# Inserted via set command by " user " on " ts;
print key"="value"\n";
found_key=1;
}
# Print all other lines
{print}
# If the end of the file is reached and we have not found the key, add it
END {
if (!found_section) {
print " New section for this file! Adding" > logOut
print "\n[" section "]"
}
if (!found_key) {
print "# Inserted via set command by " user " on " ts;
print key"="value
}
}
' "$ini_file" > "$ini_file.tmp"
else
# Handle keys without section
log 1 "Adding/Updating key (no section) ..."
# Set the awk debug prints if at verbose 2 or higher
if [[ ${VERBOSE} -ge 2 ]]; then awkLogOut=/dev/stderr; else awkLogOut=/dev/null; fi
# Key does not exist, add it before the first section (or the end)
awk -v key="$key" -v value="$value" -v ts="$(date)" -v user="$USER" -v logOut="$awkLogOut" '
BEGIN {
added=0
}
# Print line for debugging
!added {print " Inspecting line " NR ": " $0 > logOut}
# If new key starts with uppercase (it is a variables, not a command), then keep above the commands
!added && key ~ "^[A-Z]" && $1 ~ "^[a-z]" {
print "Appending VARIABLE before first command" NR > logOut
print "# Inserted via set command by " user " on " ts;
print key"="value"\n";
added=1
}
# If we have not reached a section header and we find the key, update its value
!added && $1 ~ "^" key "=" {
print " Found Existing Key at line " NR > logOut
print key"="value;
added=1;
next;
}
# If we reach a header before finding the key, then add it
/^\[.*\]/ && !added {
print " Reached section " $section " on line " NR " without detected existing key - Inserting" > logOut
print "# Inserted via set command by " user " on " ts;
print key"="value"\n";
added=1
}
# Print all other lines
{print}
END {
if (!added) {
print "# Inserted via set command by " user " on " ts;
print key"="value "\n"
}
}
' "$ini_file" > "$ini_file.tmp"
fi
# Either print the temp file (DryRun) or set the command file
if [[ "${DRYRUN}" == "true" ]]; then
if command -v sdiff > /dev/null 2>&1; then
log 1
log 1 "The following changes would be made (original vs new):"
log 1 "--------------------------------------------------------"
local sdiffArgs=("--minimal")
# Adjust diff verbosity
case $VERBOSE in
[0-1])
sdiffArgs+=("--suppress-common-lines");;
[2-9])
sdiffArgs+=("--left-column");;
esac
# Show user the diff
sdiff "${sdiffArgs[@]}" "$ini_file" "$ini_file.tmp"
local -r fileChanged=$?
# Print summary of diff
if [[ $fileChanged -eq 0 ]]; then
log 0 "No changes detected"
else
log 1 "--------------------------------------------------------"
log 0 "\nChanges detected"
fi
log 1
fi
# Clean up the temp file
rm "$ini_file.tmp"
else
# This is the real deal
# IDEA: We could offer an option to keep a backup of the original file
mv "$ini_file.tmp" "$ini_file"
fi
}
# Reads an INI-style file and loads the valid sections and keys into the commands
# associative array. Keys within a section will have a '.'' as a delimiter
declare -A commands
function process_ini_file() {
log 1 "Loading file: $1"
local section=""
while IFS= read -r line <&3 || [[ -n "$line" ]]; do
# echo " Line1 - $line"
# Skip empty lines and comments
if [[ "$line" =~ ^[[:space:]]*$ ]] || [[ "$line" =~ ^[[:space:]]*# ]]; then
continue
fi
# Check for section headers
if [[ "$line" =~ ^\[.*\]$ ]]; then
log 2 "Section Header detected: ${line:1:-1}"
section=${line:1:-1}
continue
fi
# Split the line into key and value
IFS='=' read -r key value <<< "$line"
# echo " 2 - $line"
# Trim whitespace from key and value
# key=$(echo "$key" | xargs)
# value=$(echo "$value" | xargs)
key="${key#"${key%%[![:space:]]*}"}" # remove leading whitespace characters
key="${key%"${key##*[![:space:]]}"}" # remove trailing whitespace characters
value="${value#"${value%%[![:space:]]*}"}" # remove leading whitespace characters
value="${value%"${value##*[![:space:]]}"}" # remove trailing whitespace characters
# echo " 3 - $key=$value"
# If section is not empty, prepend it to the key with an underscore
if [[ -n "$section" ]]; then
key="${section}.${key}"
fi
# Before adding the key-value pair to the associative array,
# add the key to the indexed array to preserve the order.
if [[ ! -v commands["$key"] ]]; then
log 3 " ADD: ${key} = ${value}"
else
log 2 " REPLACE: ${key} = ${value}"
log 2 " was '${commands[$key]}'"
fi
commands["$key"]=$value
# echo " 4 - $key=${commands["$key"]}"
# done < "$1"
done 3< <(cat "$1")
log 2 "" # Add some spacing in extra verbose mode
}
# Walks the directory tree from the current directory to root and combines all
# keys and values found in ${MY_CUSTOM_FILES} files. The values closest to the
# current directory are used
function load_my_custom_files() {
# Start in the current directory and move up the directory tree
declare -a file_paths
dir=$(pwd)
# Find all files in the directory tree
while [ "$dir" != "/" ]; do
if [[ -f "$dir/${MY_CUSTOM_FILE}" ]]; then
file_paths+=("$dir/${MY_CUSTOM_FILE}")
fi
dir=$(dirname "$dir")
done
# Always process the home dir file first (if it exists)
if [ -f "${HOME}/${MY_CUSTOM_FILE}" ]; then
file_paths+=("${HOME}/${MY_CUSTOM_FILE}")
fi
# Process the files in reverse order
for (( idx=${#file_paths[@]}-1 ; idx>=0 ; idx-- )) ; do
process_ini_file "${file_paths[idx]}"
done
if [[ -z ${file_paths[0]} ]]; then
log 1 "No ${MY_CUSTOM_FILE} files were found in the directory tree"
return 1
fi
log 1 "" # Add spacing in verbose mode
return 0
}
# Searches the commands array for the requested key
# If found, will set the 'foundKey_value'
foundKey_value=""
function findKey() {
local cmd=$1
log 1 "Searching for cmd: $cmd"
foundKey_value="" # Clear previous search result
for key in "${!commands[@]}"; do
log 2 " Inspecting '$key'"
if [[ "$key" == "$cmd" ]]; then
foundKey_value="${commands[$key]}"
log 1 " Found: $foundKey_value"
return 0 # There will only be one matching key, so stop when found successfully
fi
done
# If here, then the key was not found
return 1
}
# Adds a new key-value pair to the ${MY_CUSTOM_FILE} file in the current directory
# <string> key - The key to add
# <string> value - The value to associate with the key
function set_key_value() {
local key=$1
shift
local value="$1 " # The first element should always be a command so don't quote
shift
# Loop through each additional argument and append it to the value string
for arg in "$@"; do
# If the argument contains spaces, enclose it in double quotes
if [[ $arg == *" "* ]]; then
value+="\"${arg//\\ / }\" "
else
value+="$arg "
fi
done
# Remove the trailing space from the value string
value=${value% }
log 1 "Adding key: '${key}' with a value of '${value}'\n to '${MY_CUSTOM_FILE}'..."
# Use set_ini_value to add or update the key-value pair under the 'commands' section
set_ini_value "${MY_CUSTOM_FILE}" "$key" "$value"
log 3 "Updated ${MY_CUSTOM_FILE} content:"
log 3 "$(cat ${MY_CUSTOM_FILE})"
log 3
}
# Function to list all keys from the ${MY_CUSTOM_FILE} files in the current directory tree
# If the -l argument is given, output will not be piped to column.
# NOTE: Only keys beginning with a lower-case letter will be printed.
# Keys starting with uppercase are considered variables
function list_keys() {
local -r indent=" "
local use_column=true
# Check for -l argument
if [[ "$1" == "-l" ]]; then
use_column=false
fi
if load_my_custom_files; then
log 0 "The following commands are available:"
if [[ "$use_column" == true ]]; then
# Consider adding `-c 120` arg to `column` command
printf "%s\n" "${!commands[@]}" | grep '^[a-z]' | sort | column
else
printf "%s\n" "${!commands[@]}" | grep '^[a-z]' | sort | sed "s/^/${indent}/"
fi
else
log 1 "No ${MY_CUSTOM_FILE} file was found in the current directory tree."
return 1
fi
}
# Builds a merged version of the ${MY_CUSTOM_FILE} files in the directory tree
# then if the give key is found, will execute the defined value
function find_and_run_cmd() {
# The command to search for
cmd=$1
shift
load_my_custom_files
# Now that we have the combined keys, search for the key entered
if findKey "$cmd"; then
log 1 "Found value for '$cmd': $foundKey_value"
binary_cmd="$foundKey_value"
local max_loops=15
# Replace any variables found in binary_cmd with their values
# Handle positional args first
max_arg_num=-1
args=("$@")
log 2 "Checking for positional args: $* (${#args[@]})"
for ((i = ${#args[@]}; i > 0; i--)); do
arg=${args[$i-1]}
log 3 "i is ${i}, arg is ${arg}"
if [[ "$binary_cmd" =~ [^\\]\$\{?(${i})\}? ]]; then
# Record this max reference so we know how much to shift
if [ "${BASH_REMATCH[1]}" -gt "$max_arg_num" ]; then
max_arg_num=${BASH_REMATCH[1]}
log 3 "Highest positional arg found at $max_arg_num"
fi
log 2 "Replacing positional argument reference '\$${BASH_REMATCH[1]}' with '${arg}'"
binary_cmd=${binary_cmd//\$${BASH_REMATCH[1]}/$arg}
log 2 " New: $binary_cmd"
fi
done
# Shift args to the highest reference found
# Any remaining args will be left to pass on
if [ "$max_arg_num" -ne -1 ]; then
log 3 "Shifting args by $max_arg_num"
shift "$max_arg_num"
fi
local loopCount=0
while [[ "$binary_cmd" =~ (^|[^\\])(\$\{([a-zA-Z_][^}]*)\}|\$([a-zA-Z_][a-zA-Z_0-9]*)) ]]; do
log 3 "Variable Reference Match Results - ${BASH_REMATCH[1]}; ${BASH_REMATCH[2]}; ${BASH_REMATCH[3]}; ${BASH_REMATCH[4]}"
if [[ -n ${BASH_REMATCH[3]} ]]; then
# If the variable is surrounded by {}, it starts with [a-zA-Z_] and includes any character except for }
var_name=${BASH_REMATCH[3]}
slug="\${${var_name}}"
log 3 " Found variable inside curly brackets: ${var_name}"
else
# If the variable is not surrounded by {}, it follows the more restrictive pattern
var_name=${BASH_REMATCH[4]}
slug="\$${var_name}"
log 3 " Found variable: ${var_name}"
fi
# Get the value of the variable from the loaded commands
log 2 " Trying to replace variable: $var_name"
if findKey "$var_name"; then
log 2 " Found key: $foundKey_value"
# Substitute the variable in the binary_cmd
binary_cmd=${binary_cmd//$slug/$foundKey_value}
else
log 1 "The referenced variable ($slug) was not defined! Passing through to eval"
# Escape it to pass on to eval later
binary_cmd=${binary_cmd//$slug/\\$slug}
log 2 " New: $binary_cmd"
fi
# Avoid an infinite loop
((loopCount++))
if ((loopCount >= max_loops)); then
log 0 "There appears to be an infinite loop in your command references!"
exit 1
fi
done
# Now that we've replaced all known variables,
# Remove the escaped references to allow passing through
binary_cmd="${binary_cmd//\\\$/$}"
log 2 "Expanded command to be executed:\n $binary_cmd\n"
# Run the extracted command with the additional arguments
local args
if [ $# -gt 0 ]; then
printf -v args '%q ' "$@"
fi
runCMD "$binary_cmd" "$args"
else
# If the key was not found in any ${MY_CUSTOM_FILE} files, run the default command
log 1 "No ${MY_CUSTOM_FILE} file was found in the current directory tree containing '$cmd'\n"
runCMD "$cmd $*"
fi
}
# Update the script with the latest version
function update_script() {
local arg="$2"
github_path=https://raw.githubusercontent.com/jerrens/MyCE/refs/heads/main/my
# install_path=/usr/local/bin/my
install_path=$0
# Check for internet access
if ! ping -q -c 1 -W 1 8.8.8.8 >/dev/null; then
log 0 "No internet connection!"
exit 1
fi
# If 'diff' is requested, then download and show the diff
if [[ "$arg" == "diff" ]]; then
# Download the latest version from the github repo
local temp_file="/tmp/${__AppName}.new"
# Download the new version to a temp file
if ! runCMD curl --silent --location --output "${temp_file}" "${github_path}"; then
log 0 "Failed to download the latest version from $github_path"
exit 1
fi
if ! diff -q "${temp_file}" "${install_path}" >/dev/null; then
# Show the diff
log 0 "The following are the changes between your current version and the latest version on GitHub:\n"
sdiff -s --width=200 "${install_path}" "${temp_file}"
log 0 "\n---------------------------------------------------------------------------"
log 0 "You can run '${__ScriptName} update' to update to the latest version"
else
log 0 "You are already running the latest version!"
fi
log 0
# Remove the temp file
rm "${temp_file}"
exit 0
fi
if [ "$EUID" -ne 0 ]; then
log 0 "This should be run as root. Attempting to elevate permissions..."
# Re-run this command as sudo
if command -v dzdo >/dev/null 2>&1; then
runCMD dzdo "$0" "$__origArgs"
else
runCMD sudo "$0" "$__origArgs"
fi
exit
fi
# NOTE: Root Level if here
# Download the latest version from the github repo
log 0 "\nDownloading latest version from GitHub..."
log 0 " ${github_path}\n"
# Download the latest version of my
if ! runCMD curl --silent --location --output "${install_path}" "${github_path}"; then
log 0 "Failed to download the latest version from ${github_path}"
exit 1
fi
# Make it executable
log 0 "\nModifying permissions..."
runCMD chmod 755 "${install_path}"
log 0 "Update finished!"
exit 0
}
# Entry point of the program
# Check the first arg to determine what action to take
function main() {
# Check for command line args
case "$1" in
"")
log 0 "Missing arguments!"
log 0 " For syntax help, run the command '${__ScriptName} help'"
if [[ ${VERBOSE} -gt 0 ]]; then print_usage false; fi
exit 1
;;
help)
print_usage true
exit 0
;;
version)
log 0 "${__Version}"
exit 0
;;
set)
shift # Remove the 'set' command
if [[ -z "$1" || -z "$2" ]]; then
log 1 "Usage: ${__ScriptName} add <key> <value>"
exit 1
fi
set_key_value "$1" "${@:2}"
exit 0
;;
list)
shift
list_keys "$@"
exit 0
;;
update)
update_script "$@";;
*)
find_and_run_cmd "$@"
;;
esac
}
# Call main function and pass all (remaining) CL args
main "$@"
exit $?
######################################
### Change History
######################################
# shellcheck disable=SC2317 # Don't warn about unreachable commands in this function
/dev/null <<EOT
- 24.11.28 Added 'help' output and additional log output
- 24.11.12 Improvements to 'update' command handling.
Added 'update diff' command.
- 24.11.8 Added 'update' command
- 24.11.7 Added support for positional args ($1, $2) within the .myCommand values
Always load the .myCommand file in the user home directory (even if not in current directory tree)
- 24.10.16 BugFix: Replacing variables in array when merging causes order issues
- 24.10.15.1731 Added code to preserve command order
Partial command match no longer supported
BugFix: Infinite loops possible by variable dereferencing
- 24.10.15.1457 Unknown variable references are passed on to eval
- 24.10.15 BugFix: Commands containing \n not processed correctly
- 24.10.14 Added support to combine all .myCommand files in the directory tree
Added support for INI section grouping
Changed 'add' command to 'set'
- 24.10.10 Added support for surrounding variables with '{}'
- 24.10.9.1616 Fix to preserve quotes around arguments when passing
- 24.10.9 Added 'list' command
- 24.10.7.1312 Changed to using file ".myCommand" as default
- 24.10.7.1216 Added ability to define variables within the .myVersion file that
can be referenced and expanded in the key values
- 24.10.7: Initial Creation
EOT