Skip to content
This repository was archived by the owner on Jul 29, 2025. It is now read-only.

Commit 0d97eea

Browse files
committed
refactor: refactor installer script for maintainability and robustness
- Replace individual color variable definitions with a Bash associative array for improved maintainability - Update references to color codes in print_message and related strings to use the associative array - Use mktemp to create a secure temporary directory during installation, replacing a static directory - Improve PATH handling: notify if the install directory is already present in PATH and avoid re-adding Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent 603a3e3 commit 0d97eea

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

install

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
set -euo pipefail
33
APP=opencode
44

5-
RED='\033[0;31m'
6-
GREEN='\033[0;32m'
7-
YELLOW='\033[1;33m'
8-
ORANGE='\033[38;2;255;140;0m'
9-
NC='\033[0m' # No Color
5+
declare -A colors=(
6+
[RED]='\033[0;31m'
7+
[GREEN]='\033[0;32m'
8+
[YELLOW]='\033[1;33m'
9+
[ORANGE]='\033[38;2;255;140;0m'
10+
[NC]='\033[0m'
11+
)
1012

1113
requested_version=${VERSION:-}
1214

@@ -17,22 +19,21 @@ fi
1719
arch=$(uname -m)
1820

1921
if [[ "$arch" == "aarch64" ]]; then
20-
arch="arm64"
22+
arch="arm64"
2123
fi
2224

2325
filename="$APP-$os-$arch.tar.gz"
2426

25-
2627
case "$filename" in
27-
*"-linux-"*)
28-
[[ "$arch" == "x86_64" || "$arch" == "arm64" || "$arch" == "i386" ]] || exit 1
28+
*"-linux-"*)
29+
[[ "$arch" == "x86_64" || "$arch" == "arm64" || "$arch" == "i386" ]] || exit 1
2930
;;
30-
*"-mac-"*)
31-
[[ "$arch" == "x86_64" || "$arch" == "arm64" ]] || exit 1
31+
*"-mac-"*)
32+
[[ "$arch" == "x86_64" || "$arch" == "arm64" ]] || exit 1
3233
;;
33-
*)
34-
echo "${RED}Unsupported OS/Arch: $os/$arch${NC}"
35-
exit 1
34+
*)
35+
echo "${RED}Unsupported OS/Arch: $os/$arch${NC}"
36+
exit 1
3637
;;
3738
esac
3839

@@ -58,19 +59,18 @@ print_message() {
5859
local color=""
5960

6061
case $level in
61-
info) color="${GREEN}" ;;
62-
warning) color="${YELLOW}" ;;
63-
error) color="${RED}" ;;
62+
info) color="${colors[GREEN]}" ;;
63+
warning) color="${colors[YELLOW]}" ;;
64+
error) color="${colors[RED]}" ;;
6465
esac
6566

66-
echo -e "${color}${message}${NC}"
67+
echo -e "${color}${message}${colors[NC]}"
6768
}
6869

6970
check_version() {
7071
if command -v opencode >/dev/null 2>&1; then
7172
opencode_path=$(which opencode)
7273

73-
7474
## TODO: check if version is installed
7575
# installed_version=$(opencode version)
7676
installed_version="0.0.1"
@@ -87,24 +87,23 @@ check_version() {
8787

8888
download_and_install() {
8989
print_message info "Downloading ${ORANGE}opencode ${GREEN}version: ${YELLOW}$specific_version ${GREEN}..."
90-
mkdir -p opencodetmp && cd opencodetmp
90+
temp_dir=$(mktemp -d) && cd "$temp_dir"
9191
curl -# -L $url | tar xz
9292
mv opencode $INSTALL_DIR
93-
cd .. && rm -rf opencodetmp
93+
cd .. && rm -rf opencodetmp
9494
}
9595

9696
check_version
9797
download_and_install
9898

99-
10099
add_to_path() {
101100
local config_file=$1
102101
local command=$2
103102

104103
if [[ -w $config_file ]]; then
105-
echo -e "\n# opencode" >> "$config_file"
106-
echo "$command" >> "$config_file"
107-
print_message info "Successfully added ${ORANGE}opencode ${GREEN}to \$PATH in $config_file"
104+
echo -e "\n# opencode" >>"$config_file"
105+
echo "$command" >>"$config_file"
106+
print_message info "Successfully added ${colors[ORANGE]}opencode ${colors[GREEN]}to \$PATH in $config_file"
108107
else
109108
print_message warning "Manually add the directory to $config_file (or similar):"
110109
print_message info " $command"
@@ -115,24 +114,24 @@ XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
115114

116115
current_shell=$(basename "$SHELL")
117116
case $current_shell in
118-
fish)
119-
config_files="$HOME/.config/fish/config.fish"
117+
fish)
118+
config_files="$HOME/.config/fish/config.fish"
120119
;;
121-
zsh)
122-
config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
120+
zsh)
121+
config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
123122
;;
124-
bash)
125-
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
123+
bash)
124+
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
126125
;;
127-
ash)
128-
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
126+
ash)
127+
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
129128
;;
130-
sh)
131-
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
129+
sh)
130+
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
132131
;;
133-
*)
134-
# Default case if none of the above matches
135-
config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
132+
*)
133+
# Default case if none of the above matches
134+
config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
136135
;;
137136
esac
138137

@@ -149,32 +148,33 @@ if [[ -z $config_file ]]; then
149148
exit 1
150149
fi
151150

152-
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
151+
if [[ ":$PATH:" == *":$INSTALL_DIR:"* ]]; then
152+
print_message info "PATH already contains install directory: $INSTALL_DIR"
153+
else
153154
case $current_shell in
154-
fish)
155-
add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
155+
fish)
156+
add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
156157
;;
157-
zsh)
158-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
158+
zsh)
159+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
159160
;;
160-
bash)
161-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
161+
bash)
162+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
162163
;;
163-
ash)
164-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
164+
ash)
165+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
165166
;;
166-
sh)
167-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
167+
sh)
168+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
168169
;;
169-
*)
170-
print_message warning "Manually add the directory to $config_file (or similar):"
171-
print_message info " export PATH=$INSTALL_DIR:\$PATH"
170+
*)
171+
print_message warning "Manually add the directory to $config_file (or similar):"
172+
print_message info " export PATH=$INSTALL_DIR:\$PATH"
172173
;;
173174
esac
174175
fi
175176

176177
if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
177-
echo "$INSTALL_DIR" >> $GITHUB_PATH
178+
echo "$INSTALL_DIR" >>$GITHUB_PATH
178179
print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
179180
fi
180-

0 commit comments

Comments
 (0)