Skip to content

Commit f4367e8

Browse files
committed
fix: improve script safety and clarity in path and temp handling
- Remove unnecessary blank lines for improved script clarity - Replace hardcoded temp directory with mktemp for safer temporary file creation during download - Fix logic to correctly detect if INSTALL_DIR is already in PATH and prevent duplicate PATH entries - Print a message when INSTALL_DIR is already present in PATH Signed-off-by: appleboy <[email protected]>
1 parent 603a3e3 commit f4367e8

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

install

+46-48
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ RED='\033[0;31m'
66
GREEN='\033[0;32m'
77
YELLOW='\033[1;33m'
88
ORANGE='\033[38;2;255;140;0m'
9-
NC='\033[0m' # No Color
9+
NC='\033[0m'
1010

1111
requested_version=${VERSION:-}
1212

@@ -17,22 +17,21 @@ fi
1717
arch=$(uname -m)
1818

1919
if [[ "$arch" == "aarch64" ]]; then
20-
arch="arm64"
20+
arch="arm64"
2121
fi
2222

2323
filename="$APP-$os-$arch.tar.gz"
2424

25-
2625
case "$filename" in
27-
*"-linux-"*)
28-
[[ "$arch" == "x86_64" || "$arch" == "arm64" || "$arch" == "i386" ]] || exit 1
26+
*"-linux-"*)
27+
[[ "$arch" == "x86_64" || "$arch" == "arm64" || "$arch" == "i386" ]] || exit 1
2928
;;
30-
*"-mac-"*)
31-
[[ "$arch" == "x86_64" || "$arch" == "arm64" ]] || exit 1
29+
*"-mac-"*)
30+
[[ "$arch" == "x86_64" || "$arch" == "arm64" ]] || exit 1
3231
;;
33-
*)
34-
echo "${RED}Unsupported OS/Arch: $os/$arch${NC}"
35-
exit 1
32+
*)
33+
echo "${RED}Unsupported OS/Arch: $os/$arch${NC}"
34+
exit 1
3635
;;
3736
esac
3837

@@ -58,9 +57,9 @@ print_message() {
5857
local color=""
5958

6059
case $level in
61-
info) color="${GREEN}" ;;
62-
warning) color="${YELLOW}" ;;
63-
error) color="${RED}" ;;
60+
info) color="${GREEN}" ;;
61+
warning) color="${YELLOW}" ;;
62+
error) color="${RED}" ;;
6463
esac
6564

6665
echo -e "${color}${message}${NC}"
@@ -70,7 +69,6 @@ check_version() {
7069
if command -v opencode >/dev/null 2>&1; then
7170
opencode_path=$(which opencode)
7271

73-
7472
## TODO: check if version is installed
7573
# installed_version=$(opencode version)
7674
installed_version="0.0.1"
@@ -87,23 +85,22 @@ check_version() {
8785

8886
download_and_install() {
8987
print_message info "Downloading ${ORANGE}opencode ${GREEN}version: ${YELLOW}$specific_version ${GREEN}..."
90-
mkdir -p opencodetmp && cd opencodetmp
88+
temp_dir=$(mktemp -d) && cd "$temp_dir"
9189
curl -# -L $url | tar xz
9290
mv opencode $INSTALL_DIR
93-
cd .. && rm -rf opencodetmp
91+
cd .. && rm -rf opencodetmp
9492
}
9593

9694
check_version
9795
download_and_install
9896

99-
10097
add_to_path() {
10198
local config_file=$1
10299
local command=$2
103100

104101
if [[ -w $config_file ]]; then
105-
echo -e "\n# opencode" >> "$config_file"
106-
echo "$command" >> "$config_file"
102+
echo -e "\n# opencode" >>"$config_file"
103+
echo "$command" >>"$config_file"
107104
print_message info "Successfully added ${ORANGE}opencode ${GREEN}to \$PATH in $config_file"
108105
else
109106
print_message warning "Manually add the directory to $config_file (or similar):"
@@ -115,24 +112,24 @@ XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
115112

116113
current_shell=$(basename "$SHELL")
117114
case $current_shell in
118-
fish)
119-
config_files="$HOME/.config/fish/config.fish"
115+
fish)
116+
config_files="$HOME/.config/fish/config.fish"
120117
;;
121-
zsh)
122-
config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
118+
zsh)
119+
config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
123120
;;
124-
bash)
125-
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
121+
bash)
122+
config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
126123
;;
127-
ash)
128-
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
124+
ash)
125+
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
129126
;;
130-
sh)
131-
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
127+
sh)
128+
config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
132129
;;
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"
130+
*)
131+
# Default case if none of the above matches
132+
config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
136133
;;
137134
esac
138135

@@ -149,32 +146,33 @@ if [[ -z $config_file ]]; then
149146
exit 1
150147
fi
151148

152-
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
149+
if [[ ":$PATH:" == *":$INSTALL_DIR:"* ]]; then
150+
print_message info "PATH already contains install directory: $INSTALL_DIR"
151+
else
153152
case $current_shell in
154-
fish)
155-
add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
153+
fish)
154+
add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
156155
;;
157-
zsh)
158-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
156+
zsh)
157+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
159158
;;
160-
bash)
161-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
159+
bash)
160+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
162161
;;
163-
ash)
164-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
162+
ash)
163+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
165164
;;
166-
sh)
167-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
165+
sh)
166+
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
168167
;;
169-
*)
170-
print_message warning "Manually add the directory to $config_file (or similar):"
171-
print_message info " export PATH=$INSTALL_DIR:\$PATH"
168+
*)
169+
print_message warning "Manually add the directory to $config_file (or similar):"
170+
print_message info " export PATH=$INSTALL_DIR:\$PATH"
172171
;;
173172
esac
174173
fi
175174

176175
if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
177-
echo "$INSTALL_DIR" >> $GITHUB_PATH
176+
echo "$INSTALL_DIR" >>$GITHUB_PATH
178177
print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
179178
fi
180-

0 commit comments

Comments
 (0)