WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 0dbc57e

Browse files
committed
refactor: improve shell install script robustness and error handling
- Add error handling when curl fails to download the binary, logging a clear error message - Refactor fetching the latest release version to avoid multiple curl calls and add error handling for failed requests - Simplify the process of setting the VERSION variable, removing redundant error checks - Minor adjustment in config file not found message for improved readability - Merge shell cases for path addition into a single pattern for zsh, bash, ash, and sh for cleaner code Signed-off-by: appleboy <[email protected]>
1 parent 6b27855 commit 0dbc57e

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

install.sh

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ function download_and_install() {
5555
# Use temp dir for download
5656
TARGET="${TMPDIR}/${CLIENT_BINARY}"
5757

58-
curl -# -fSL --retry 5 --keepalive-time 2 ${INSECURE_ARG} "${DOWNLOAD_URL_PREFIX}/${CLIENT_BINARY}" -o "${TARGET}"
58+
curl -# -fSL --retry 5 --keepalive-time 2 ${INSECURE_ARG} "${DOWNLOAD_URL_PREFIX}/${CLIENT_BINARY}" -o "${TARGET}" \
59+
|| log_error "Failed to download ${CLIENT_BINARY}" 6
5960
chmod +x "${TARGET}" || log_error "Failed to set executable permission on: ${TARGET}" 7
6061
# Move the binary to install dir and rename to codegpt
6162
mv "${TARGET}" "${INSTALL_DIR}/codegpt" || log_error "Failed to move ${TARGET} to ${INSTALL_DIR}/codegpt" 8
@@ -92,10 +93,14 @@ function add_to_path() {
9293
# Fetch latest release version from GitHub if VERSION is not set
9394
function get_latest_version() {
9495
local latest
96+
local response
97+
response=$(curl $INSECURE_ARG -# --retry 5 -fSL https://api.github.com/repos/appleboy/CodeGPT/releases/latest) \
98+
|| log_error "Failed to fetch the latest version from GitHub." 6
99+
95100
if command -v jq >/dev/null 2>&1; then
96-
latest=$(curl $INSECURE_ARG -# --retry 5 -fSL https://api.github.com/repos/appleboy/CodeGPT/releases/latest | jq -r .tag_name)
101+
latest=$(echo "$response" | jq -r '.tag_name // empty')
97102
else
98-
latest=$(curl $INSECURE_ARG -# --retry 5 -fSL https://api.github.com/repos/appleboy/CodeGPT/releases/latest | grep '"tag_name":' | sed -E 's/.*"tag_name": ?"v?([^"]+)".*/\1/')
103+
latest=$(echo "$response" | grep '"tag_name":' | sed -E 's/.*"tag_name": ?"v?([^"]+)".*/\1/')
99104
fi
100105
# Remove leading 'v' if present
101106
latest="${latest#v}"
@@ -127,11 +132,7 @@ if [[ -n "${INSECURE:-}" ]]; then
127132
fi
128133

129134
if [[ -z "${VERSION:-}" ]]; then
130-
LATEST_VERSION=$(get_latest_version)
131-
if [[ -z "$LATEST_VERSION" ]]; then
132-
log_error "Failed to fetch the latest version from GitHub." 6
133-
fi
134-
VERSION="$LATEST_VERSION"
135+
VERSION=$(get_latest_version)
135136
fi
136137

137138
# Check if VERSION is a valid semantic version
@@ -181,24 +182,15 @@ for file in $config_files; do
181182
done
182183

183184
if [[ -z $config_file ]]; then
184-
log_error "No config file found for $current_shell. Checked files: ${config_files[*]}" 1
185+
log_error "No config file found for $current_shell. Checked files: $config_files" 1
185186
fi
186187

187188
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
188189
case $current_shell in
189190
fish)
190191
add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
191192
;;
192-
zsh)
193-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
194-
;;
195-
bash)
196-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
197-
;;
198-
ash)
199-
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
200-
;;
201-
sh)
193+
zsh | bash | ash | sh)
202194
add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
203195
;;
204196
*)

0 commit comments

Comments
 (0)