Skip to content

Commit a49e2b5

Browse files
committed
feat: Improved help commands by using declare
1 parent 6e229be commit a49e2b5

File tree

1 file changed

+45
-21
lines changed

1 file changed

+45
-21
lines changed

brain.sh

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,62 @@ RED='\033[0;31m'
66
GREEN='\033[0;32m'
77
NC='\033[0m'
88

9-
readonly brain_dir="$HOME/.brain"
9+
readonly BRAIN_DIR="$HOME/.brain"
1010
readonly VERSION_FILE="$HOME/.brain/version"
11-
readonly _brain_bin="/usr/local/bin/brain"
12-
readonly neurons_git="https://raw.githubusercontent.com/neobrains/brain/main/neurons"
13-
readonly brain_url="https://raw.githubusercontent.com/neobrains/brain/main/brain.sh"
11+
readonly _BRAIN_BIN="/usr/local/bin/brain"
12+
readonly NEURONS_GIT="https://raw.githubusercontent.com/neobrains/brain/main/neurons"
13+
readonly RAW_URI="https://raw.githubusercontent.com/neobrains/brain/main"
1414

1515
valid_commands=("install" "uninstall" "update" "upgrade" "search" "list" "info")
1616

1717
usage() {
18+
declare -A options
19+
options["-h, --help"]="Show this help message and exit"
20+
options["-v, --version"]="Show version and exit"
21+
options["-u, --upgrade"]="Update brain to the latest version"
22+
options["-f, --force"]="Force update brain to the latest version"
23+
options["-r, --remove"]="Remove brain from your system"
24+
25+
declare -A commands
26+
commands["install"]="Install a package"
27+
commands["uninstall"]="Uninstall a package"
28+
commands["update"]="Update a package"
29+
commands["neurons"]="List (local / remote) / search / info about packages"
30+
1831
echo "Usage: $(basename "$0") [options] [command]"
1932
echo "brain version $(cat "$VERSION_FILE")"
2033
echo "Options:"
21-
echo " -h, --help Show this help message and exit"
22-
echo " -v, --version Show version and exit"
23-
echo " -u, --upgrade Update brain to the latest version"
24-
echo " -f, --force Force update brain to the latest version"
25-
echo " -r, --remove Remove brain from your system"
34+
35+
for opt in "${!options[@]}"; do
36+
printf " %-20s %s\n" "$opt" "${options[$opt]}"
37+
done
38+
2639
echo "Commands:"
27-
echo " install Install a package"
28-
echo " uninstall Uninstall a package"
29-
echo " update Update a package"
30-
echo " neurons List (local / remote) / search / info about packages"
40+
41+
for cmd in "${!commands[@]}"; do
42+
printf " %-20s %s\n" "$cmd" "${commands[$cmd]}"
43+
done
44+
3145
echo ""
3246
echo "For more information, visit https://github.com/neobrains/brain"
3347
}
3448

3549
neurons_usage() {
36-
echo "Usage: $(basename "$0") [options] [command]"
50+
declare -A commands
51+
52+
commands["list remote"]="List all available packages"
53+
commands["list local"]="List locally installed packages"
54+
commands["info"]="Show information about a package"
55+
56+
declare -r script_name=$(basename "$0")
57+
58+
echo "Usage: $script_name [options] [command]"
3759
echo "Commands:"
38-
echo " list remote List all available packages"
39-
echo " list local List locally installed packages"
40-
echo " info Show information about a package"
60+
61+
for cmd in "${!commands[@]}"; do
62+
printf " %-15s %s\n" "$cmd" "${commands[$cmd]}"
63+
done
64+
4165
echo ""
4266
echo "For more information, visit https://github.com/neobrains/brain"
4367
}
@@ -68,10 +92,10 @@ if [[ $1 =~ (-u|--upgrade) ]]; then
6892
update_brain() {
6993
echo "Updating brain..."
7094
check_sudo
71-
curl -fsSL -o brain $brain_url
95+
curl -fsSL -o brain "${RAW_URI}/brain.sh"
7296
chmod +x brain
73-
if [ ! -d "$brain_dir" ]; then
74-
mkdir "$brain_dir"
97+
if [ ! -d "$BRAIN_DIR" ]; then
98+
mkdir "$BRAIN_DIR"
7599
fi
76100
latest_version=$(curl -s https://api.github.com/repos/neobrains/brain/releases/latest | jq -r '.tag_name')
77101
echo "$latest_version" >"$VERSION_FILE"
@@ -167,7 +191,7 @@ if [[ $1 =~ (install|update|uninstall) ]]; then
167191
exit 1
168192
fi
169193

170-
script=$(curl -sL "$neurons_git/$2.sh")
194+
script=$(curl -sL "$NEURONS_GIT/$2.sh")
171195

172196
if [ "$script" == "404: Not Found" ]; then
173197
echo -e "${RED}Error: Package '$2' not found${NC}"

0 commit comments

Comments
 (0)