From f4367e8d86699c7970e258362852ba1419218089 Mon Sep 17 00:00:00 2001 From: appleboy Date: Fri, 2 May 2025 23:40:51 +0800 Subject: [PATCH] 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 --- install | 94 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/install b/install index b58aa14..d4d1b92 100755 --- a/install +++ b/install @@ -6,7 +6,7 @@ RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' ORANGE='\033[38;2;255;140;0m' -NC='\033[0m' # No Color +NC='\033[0m' requested_version=${VERSION:-} @@ -17,22 +17,21 @@ fi arch=$(uname -m) if [[ "$arch" == "aarch64" ]]; then - arch="arm64" + arch="arm64" fi filename="$APP-$os-$arch.tar.gz" - case "$filename" in - *"-linux-"*) - [[ "$arch" == "x86_64" || "$arch" == "arm64" || "$arch" == "i386" ]] || exit 1 +*"-linux-"*) + [[ "$arch" == "x86_64" || "$arch" == "arm64" || "$arch" == "i386" ]] || exit 1 ;; - *"-mac-"*) - [[ "$arch" == "x86_64" || "$arch" == "arm64" ]] || exit 1 +*"-mac-"*) + [[ "$arch" == "x86_64" || "$arch" == "arm64" ]] || exit 1 ;; - *) - echo "${RED}Unsupported OS/Arch: $os/$arch${NC}" - exit 1 +*) + echo "${RED}Unsupported OS/Arch: $os/$arch${NC}" + exit 1 ;; esac @@ -58,9 +57,9 @@ print_message() { local color="" case $level in - info) color="${GREEN}" ;; - warning) color="${YELLOW}" ;; - error) color="${RED}" ;; + info) color="${GREEN}" ;; + warning) color="${YELLOW}" ;; + error) color="${RED}" ;; esac echo -e "${color}${message}${NC}" @@ -70,7 +69,6 @@ check_version() { if command -v opencode >/dev/null 2>&1; then opencode_path=$(which opencode) - ## TODO: check if version is installed # installed_version=$(opencode version) installed_version="0.0.1" @@ -87,23 +85,22 @@ check_version() { download_and_install() { print_message info "Downloading ${ORANGE}opencode ${GREEN}version: ${YELLOW}$specific_version ${GREEN}..." - mkdir -p opencodetmp && cd opencodetmp + temp_dir=$(mktemp -d) && cd "$temp_dir" curl -# -L $url | tar xz mv opencode $INSTALL_DIR - cd .. && rm -rf opencodetmp + cd .. && rm -rf opencodetmp } check_version download_and_install - add_to_path() { local config_file=$1 local command=$2 if [[ -w $config_file ]]; then - echo -e "\n# opencode" >> "$config_file" - echo "$command" >> "$config_file" + echo -e "\n# opencode" >>"$config_file" + echo "$command" >>"$config_file" print_message info "Successfully added ${ORANGE}opencode ${GREEN}to \$PATH in $config_file" else print_message warning "Manually add the directory to $config_file (or similar):" @@ -115,24 +112,24 @@ XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config} current_shell=$(basename "$SHELL") case $current_shell in - fish) - config_files="$HOME/.config/fish/config.fish" +fish) + config_files="$HOME/.config/fish/config.fish" ;; - zsh) - config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv" +zsh) + config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv" ;; - bash) - config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile" +bash) + config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile" ;; - ash) - config_files="$HOME/.ashrc $HOME/.profile /etc/profile" +ash) + config_files="$HOME/.ashrc $HOME/.profile /etc/profile" ;; - sh) - config_files="$HOME/.ashrc $HOME/.profile /etc/profile" +sh) + config_files="$HOME/.ashrc $HOME/.profile /etc/profile" ;; - *) - # Default case if none of the above matches - config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile" +*) + # Default case if none of the above matches + config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile" ;; esac @@ -149,32 +146,33 @@ if [[ -z $config_file ]]; then exit 1 fi -if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then +if [[ ":$PATH:" == *":$INSTALL_DIR:"* ]]; then + print_message info "PATH already contains install directory: $INSTALL_DIR" +else case $current_shell in - fish) - add_to_path "$config_file" "fish_add_path $INSTALL_DIR" + fish) + add_to_path "$config_file" "fish_add_path $INSTALL_DIR" ;; - zsh) - add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" + zsh) + add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" ;; - bash) - add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" + bash) + add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" ;; - ash) - add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" + ash) + add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" ;; - sh) - add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" + sh) + add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" ;; - *) - print_message warning "Manually add the directory to $config_file (or similar):" - print_message info " export PATH=$INSTALL_DIR:\$PATH" + *) + print_message warning "Manually add the directory to $config_file (or similar):" + print_message info " export PATH=$INSTALL_DIR:\$PATH" ;; esac fi if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then - echo "$INSTALL_DIR" >> $GITHUB_PATH + echo "$INSTALL_DIR" >>$GITHUB_PATH print_message info "Added $INSTALL_DIR to \$GITHUB_PATH" fi -