-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.sh
More file actions
executable file
·133 lines (113 loc) · 2.94 KB
/
cli.sh
File metadata and controls
executable file
·133 lines (113 loc) · 2.94 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
#!/usr/bin/env bash
set -u
# Simple CLI wrapper that discovers scripts in the commands/ directory
# Get the directory of this script, so it can be run from anywhere
realpath="$(realpath "$0")" || { echo "Failed to get realpath"; exit 1; }
# `CLIROOT` is the directory of the cli script.
# Commands are run from the directory the user is in,
# use `$CLIROOT` to cd to the directory of the cli script.
CLIROOT="${realpath%/*}"
# `CMDROOT` absolute path to commands directory.
CMDROOT="$CLIROOT/commands"
####################
# Helper functions #
####################
# Prints a warning message without exiting.
# Usage: warning <message>...
warning() {
local msg="$*"
printf "\e[43m\e[30m[warning]\e[0m %s\n" "$msg"
}
# Prints a error message without exiting.
# Usage: error <message>...
error() {
local msg="$*"
printf "\e[41m\e[39m[error] %s\e[0m\n" "$msg"
}
# Prints a success message without exiting.
# Usage: success <message>...
success() {
local msg="$*"
printf "\e[42m\e[30m[success]\e[0m %s\n" "$msg"
}
# Check if a command exists
# Usage: command_exists <command>
# Returns 0 if command exists, 1 otherwise.
command_exists() {
local needle="$1"
for v in "${commands[@]}"; do
[ "$v" = "$needle" ] && return 0
done
return 1
}
# Color codes
RESET="\e[0m"
# shellcheck disable=SC2034
RED="\e[31m"
GREEN="\e[32m"
YELLOW="\e[33m"
# shellcheck disable=SC2034
BLUE="\e[34m"
##################
# Command loader #
##################
# Build commands list from commands/*.sh (lowercased, without .sh)
commands=()
if [ -d "$CMDROOT" ]; then
for f in "$CMDROOT"/*.sh; do
[ -f "$f" ] || continue
name=$(basename "$f" .sh)
name_lc=${name,,}
commands+=("$name_lc")
done
fi
commands+=("help" "list")
# CMD is $1 lowercased, default to "list"
CMD=${1:-list}
CMD=${CMD,,}
# Shift all args so $1 no longer equals CMD
shift
if [ "$CMD" = "list" ]; then
echo -e "${YELLOW}Usage:${RESET}"
echo " command [options]"
echo
echo -e "${YELLOW}Commands:${RESET}"
# determine longest command name
max=0
for c in "${commands[@]}"; do
l=${#c}
[ "$l" -gt "$max" ] && max=$l
done
for c in "${commands[@]}"; do
desc=""
helpfile="$CMDROOT/${c}.help.txt"
if [ -f "$helpfile" ]; then
IFS= read -r desc < "$helpfile" || true
fi
printf " ${GREEN}%-${max}s${RESET} %s\n" "$c" "$desc"
done
exit 0
fi
if [ "$CMD" = "help" ] || [ "$CMD" = "--help" ]; then
if [ "$#" -eq 0 ]; then
echo "Display help for a command."
echo "Usage: help [command]"
exit 0
fi
target=${1,,}
helpfile="$CMDROOT/${target}.help.txt"
if command_exists "$target" && [ -f "$helpfile" ]; then
cat "$helpfile"
exit 0
fi
echo "No help available for $target"
exit 1
fi
# If CMD not in list, print error and exit 1
if ! command_exists "$CMD"; then
printf "Command \"%s\" not found. Use list for a list of commands.\n" "$CMD"
exit 1
fi
# Source the script matching CMD
# shellcheck disable=SC1090
source "$CMDROOT/$CMD.sh"