-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrain.sh
executable file
·223 lines (189 loc) · 5.73 KB
/
brain.sh
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
#!/bin/bash
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # stands for No Color
readonly BRAIN_DIR="$HOME/.brain"
readonly VERSION_FILE="$HOME/.brain/version"
readonly _BRAIN_BIN="/usr/local/bin/brain"
readonly RAW_URI="https://raw.githubusercontent.com/neobrains/brain/main"
valid_commands=("install" "uninstall" "update" "upgrade" "search" "list" "info")
usage() {
declare -A options
options["-h, --help"]="Show this help message and exit"
options["-v, --version"]="Show version and exit"
options["-u, --upgrade"]="Update brain to the latest version"
options["-f, --force"]="Force update brain to the latest version"
options["-r, --remove"]="Remove brain from your system"
declare -A commands
commands["install"]="Install a package"
commands["remove"]="Remove a package"
commands["update"]="Update a package"
commands["neurons"]="List (local / remote) / search / info about packages"
echo "Usage: $(basename "$0") [options] [command]"
echo "brain version $(cat "$VERSION_FILE")"
echo "Options:"
for opt in "${!options[@]}"; do
printf " %-20s %s\n" "$opt" "${options[$opt]}"
done
echo "Commands:"
for cmd in "${!commands[@]}"; do
printf " %-20s %s\n" "$cmd" "${commands[$cmd]}"
done
echo ""
echo "For more information, visit https://github.com/neobrains/brain"
}
neurons_usage() {
declare -A commands
commands["list remote"]="List all available packages"
commands["list local"]="List locally installed packages"
commands["info"]="Show information about a package"
declare -r script_name=$(basename "$0")
echo "Usage: $script_name [options] [command]"
echo "Commands:"
for cmd in "${!commands[@]}"; do
printf " %-15s %s\n" "$cmd" "${commands[$cmd]}"
done
echo ""
echo "For more information, visit https://github.com/neobrains/brain"
}
check_sudo() {
if ! sudo -v &>/dev/null; then
printf "${RED}Error: You need to have sudo privileges to use this command${NC}\n"
exit 1
fi
}
if ! command -v curl &>/dev/null; then
printf "${RED}Error: curl is not installed. Please install curl to continue.${NC}\n"
exit 1
fi
if [[ $# -eq 0 || $1 =~ (-h|--help|help) ]]; then
usage
exit 0
fi
if [[ $1 =~ (-v|--version) ]]; then
cat "$VERSION_FILE"
exit 0
fi
if [[ $1 =~ (-u|--upgrade) ]]; then
update_brain() {
echo "Updating brain..."
check_sudo
curl -fsSL -o brain "${RAW_URI}/brain.sh"
chmod +x brain
if [ ! -d "$BRAIN_DIR" ]; then
mkdir "$BRAIN_DIR"
fi
latest_version=$(curl -s https://api.github.com/repos/neobrains/brain/releases/latest | jq -r '.tag_name')
echo "$latest_version" >"$VERSION_FILE"
sudo mv brain /usr/local/bin/
printf "${GREEN}brain has been updated.${NC}\n"
}
update_brain
exit 0
fi
if [[ $1 =~ (-r|--remove) ]]; then
read -r -p "Are you sure you want to remove brain? (y/n) " answer
if [[ $answer =~ ^([yY][eE][sS]|[yY])$ ]]; then
echo "Removing brain..."
check_sudo
sudo rm -f /usr/local/bin/brain
if [ -d ~/.brain ]; then
sudo rm -rf ~/.brain
fi
printf "${GREEN}brain has been removed.${NC}\n"
fi
fi
if [[ $1 =~ (neurons) ]]; then
if [[ $# -eq 0 || $2 =~ (-h|--help|help) ]]; then
neurons_usage
exit 0
fi
case "$2" in
"list")
if [ -z "$3" ]; then
echo -e "${RED}Error: You must provide a package name to list${NC}"
neurons_usage
exit 1
fi
case "$3" in
"remote")
echo -e "${GREEN}Available packages:${NC}\n"
printf "%-30s %s\n" "Name" "Description"
NEURONS=$(curl -s https://api.github.com/repos/neobrains/brain/contents/neurons | jq -r '.[].name')
NEURONS_DESCRIPTION=$(curl -s https://raw.githubusercontent.com/neobrains/brain/main/descriptions.json)
for package in $NEURONS; do
package_name=$(echo "$package" | sed 's/\.sh$//')
description=$(echo $NEURONS_DESCRIPTION | jq -r ".$package_name")
printf "%-30s %s\n" "$package_name" "$description"
done
;;
"local")
echo "not implemented yet"
;;
*)
echo -e "${RED}Error: Unknown command '$3'${NC}"
neurons_usage
exit 1
;;
esac
;;
"info")
if [ -z "$3" ]; then
echo -e "${RED}Error: You must provide a package name to info${NC}"
neurons_usage
exit 1
fi
echo "not implemented yet"
;;
*)
echo -e "${RED}Error: Unknown command '$2'${NC}"
neurons_usage
exit 1
;;
esac
exit 0
fi
if [[ $1 =~ (install|update|remove) ]]; then
case "$1" in
"install")
action="-install"
;;
"update")
action="-update"
;;
"remove")
action="-uninstall"
;;
*)
echo -e "${RED}Error: Unknown command '$1'${NC}"
usage
exit 1
;;
esac
if [ -z "$2" ]; then
echo -e "${RED}Error: You must provide a package name to $1${NC}"
usage
exit 1
fi
script=$(curl -sL "$RAW_URI/neurons/$2.sh")
if [ "$script" == "404: Not Found" ]; then
echo -e "${RED}Error: Package '$2' not found${NC}"
exit 1
fi
echo "$script" | sudo bash -s -- "$action"
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo -e "${RED}Error: $2 failed to $1${NC}"
exit 1
fi
exit 0
fi
case $1 in
"${valid_commands[@]}") ;;
*)
echo -en "${RED}Error: Unknown command '$1'${NC}\n"
usage
exit 1
;;
esac