-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.sh
More file actions
executable file
·99 lines (82 loc) · 2.99 KB
/
utils.sh
File metadata and controls
executable file
·99 lines (82 loc) · 2.99 KB
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
#!/usr/bin/env fish
# Configurable Paths
set BASE_PATH "$PWD"
set UTILS_DIR "./scripts/utils"
set AVAILABLE_FUNCTIONS ""
# Function to source all .fish files in the utils directory
function source_utils
for script in (find $UTILS_DIR -type f -name "*.fish" 2>/dev/null)
source $script
end
end
# Function to get all available functions from the utils directory
function get_available_functions
set available_functions ""
for script in (find $UTILS_DIR -type f -name "*.fish" 2>/dev/null)
set func_name (basename $script .fish)
set available_functions $available_functions $func_name
end
# Clean up any leading/trailing spaces from the list and return
echo (string trim $available_functions)
end
# Function to handle unknown commands
function handle_unknown_command
echo "Error: Unknown function '$argv[1]'."
echo "Available functions: (get_available_functions)"
exit 1
end
# Function to display the function details (Description & Example)
function show_function_details
set func_name $argv[1]
set func_file (find $UTILS_DIR -type f -name "$func_name.fish" 2>/dev/null)
if test -z "$func_file"
echo "Error: Function '$func_name' not found."
exit 1
end
set description (grep -m 1 "# Description:" $func_file | string replace -r "# Description: " "" | string trim)
set example (grep -m 1 "# Example:" $func_file | string replace -r "# Example: " "" | string trim)
echo "Function: $func_name"
echo "Description: " (test -n "$description"; and echo "$description"; or echo "No description available.")
echo "Example: " (test -n "$example"; and echo "$example"; or echo "No example available.")
end
# Function to call the requested function dynamically
function call_function
set FUNCTION $argv[1]
set FUNCTION_ARGS $argv[2..-1]
if functions -q $FUNCTION
$FUNCTION $FUNCTION_ARGS
else
handle_unknown_command $FUNCTION
end
end
# Source the utils functions
source_utils
# Get the list of available functions
set available_functions (get_available_functions)
# Split the string into an array of function names and remove any empty entries
set available_functions_array (string split ' ' (string trim $available_functions))
# Main logic to handle function calls based on arguments
if test (count $argv) -gt 0
# Check if -h flag is passed
if test "$argv[1]" = "-h"
if test (count $argv) -lt 2
echo "Usage: utils.sh -h <function_name>"
echo "Use '-h' to get details for a specific function."
exit 1
end
show_function_details $argv[2]
exit 0
end
# Otherwise, call the requested function
call_function $argv
else
echo "Usage: utils.sh <function> [args...]"
echo "Available functions:"
# Print the list of discovered functions (one per line)
for func in $available_functions_array
if test -n "$func" # Only print non-empty functions
echo "- $func"
end
end
exit 1
end