-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.sh
More file actions
142 lines (127 loc) · 3.62 KB
/
Copy pathmenu.sh
File metadata and controls
142 lines (127 loc) · 3.62 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
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
#!/usr/bin/env bash
#DESC: Main Admin Menu
set -euo pipefail
IFS=$'\n\t'
COMMANDS_DIR="$(dirname "$0")/commands"
# Show help menu with --help
# --
show_help() {
clear
echo ""
cat <<EOF
Usage:
./menu.sh # Launch interactive menu
./menu.sh <command> [...] # Run command directly with arguments
./menu.sh --list # Show available commands with descriptions
./menu.sh --ci # Run tests, then launch menu
./menu.sh --ci-only # Run tests and exit
./menu.sh --help # Show this help message
echo ""
EOF
}
# List all scripts in commands/
# --
list_commands() {
echo ""
echo "Available commands:"
echo ""
mapfile -t scripts < <(find "$COMMANDS_DIR" -maxdepth 1 -type f -name "*.sh" -exec basename {} \; | sort)
for script in "${scripts[@]}"; do
desc=$(grep -m1 '^#DESC:' "$COMMANDS_DIR/$script" | sed 's/^#DESC:[[:space:]]*//')
printf " %-15s %s\n" "${script%.sh}" "${desc:-}"
echo ""
done
}
# Run extra options
# --
if [[ $# -gt 0 ]]; then
case "$1" in
# Show help
# --
--help) show_help; exit 0 ;;
# Show all commands
# --
--list) list_commands; exit 0 ;;
# Run test and launch menu
# --
--ci)
echo "🔍 Running CI test suite..."
if [[ -f "./test.sh" ]]; then
bash ./test.sh
echo "✅ CI complete. Launching menu..."
else
echo "⚠️ test.sh not found. Skipping tests."
fi
;;
# Run test only
# --
--ci-only)
echo "🔍 Running CI test suite only..."
if [[ -f "./test.sh" ]]; then
bash ./test.sh --markdown
exit 0
else
echo "⚠️ test.sh not found. Skipping tests."
exit 1
fi
;;
-*)
echo "Unknown option: $1"
show_help
exit 1
;;
*)
cmd="$1"
shift
script_path="$COMMANDS_DIR/$cmd.sh"
if [[ -f "$script_path" ]]; then
bash "$script_path" "$@"
exit 0
else
echo "Command '$cmd' not found."
exit 1
fi
;;
esac
fi
# Generate menu with scripts in command/
# --
while true; do
clear
echo "======================= Dynamic Menu ======================="
echo ""
echo "Available commands:"
echo ""
mapfile -t scripts < <(find "$COMMANDS_DIR" -maxdepth 1 -type f -name "*.sh" -exec basename {} \; | sort)
for i in "${!scripts[@]}"; do
script_path="$COMMANDS_DIR/${scripts[$i]}"
desc=$(grep -m1 '^#DESC:' "$script_path" | sed 's/^#DESC:[[:space:]]*//')
printf "%2d) %-20s %s\n" "$((i+1))" "${scripts[$i]%.sh}" "${desc:-}"
done
printf "%2d) Quit\n" "$(( ${#scripts[@]} + 1 ))"
echo ""
echo "============================================================"
echo ""
read -rp "Select an option: " choice
if [[ "$choice" -eq $(( ${#scripts[@]} + 1 )) ]]; then
clear
echo ""
echo "============================================================"
echo ""
echo "Goodbye!"
echo ""
echo "============================================================"
echo ""
exit 0
fi
if [[ "$choice" =~ ^[0-99]+$ ]] && (( choice >= 1 && choice <= ${#scripts[@]} )); then
script="${scripts[$((choice-1))]}"
bash "$COMMANDS_DIR/$script"
echo ""
read -rp "Press Enter to return to menu..."
echo ""
else
echo "Invalid choice."
sleep 1
fi
done