Skip to content

Commit 8501ab0

Browse files
committed
refactor: refactor initialization and option handling for install script
- Move all environment variable initialization and option parsing to later in the script, after function definitions - Add automatic fetching of the latest GitHub release version if VERSION is not specified - Add validation for semantic version format of VERSION - Centralize and improve handling for the CURL_INSECURE environment option - Move calls to detect_client_info and download_and_install to after all variable initializations and checks - Remove duplicate logic by extracting CURL_INSECURE parsing out of the download_and_install function Signed-off-by: appleboy <[email protected]>
1 parent f47a251 commit 8501ab0

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

install.sh

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ YELLOW='\033[1;33m'
1010
ORANGE='\033[38;2;255;140;0m'
1111
NC='\033[0m' # No Color
1212

13-
VERSION="${VERSION:-0.16.1}"
14-
RELEASE_URL="${RELEASE_URL:-https://github.com/appleboy/CodeGPT/releases/download}"
15-
INSTALL_DIR="${INSTALL_DIR:-$HOME/.codegpt/bin}"
16-
CURL_INSECURE="${CURL_INSECURE:-false}"
17-
CLIENT_PLATFORM="${CLIENT_PLATFORM:-$(uname -s | tr '[:upper:]' '[:lower:]')}"
18-
CLIENT_ARCH="${CLIENT_ARCH:-$(uname -m)}"
19-
2013
function print_message() {
2114
local level=$1
2215
local message=$2
@@ -49,23 +42,10 @@ function detect_client_info() {
4942
esac
5043
}
5144

52-
detect_client_info
53-
5445
function download_and_install() {
5546
DOWNLOAD_URL_PREFIX="${RELEASE_URL}/v${VERSION}"
5647
CLIENT_BINARY="CodeGPT-${VERSION}-${CLIENT_PLATFORM}-${CLIENT_ARCH}"
5748
print_message info "Downloading ${CLIENT_BINARY} from ${DOWNLOAD_URL_PREFIX}"
58-
59-
if [[ "${CURL_INSECURE}" != 'true' && "${CURL_INSECURE}" != 'false' ]]; then
60-
log_error "CURL_INSECURE must be either 'true' or 'false'" 4
61-
fi
62-
if [[ "${CURL_INSECURE}" == 'true' ]]; then
63-
print_message warning "CURL_INSECURE is set to true. Proceeding with insecure download."
64-
fi
65-
INSECURE_OPTION=""
66-
if [[ "${CURL_INSECURE}" == 'true' ]]; then
67-
INSECURE_OPTION="--insecure"
68-
fi
6949
mkdir -p "$INSTALL_DIR" || log_error "Failed to create directory: $INSTALL_DIR" 5
7050
TARGET="$INSTALL_DIR/${CLIENT_BINARY}"
7151

@@ -77,8 +57,6 @@ function download_and_install() {
7757
"${INSTALL_DIR}/codegpt" version
7858
}
7959

80-
download_and_install
81-
8260
function add_to_path() {
8361
local config_file=$1
8462
local command=$2
@@ -98,6 +76,52 @@ function add_to_path() {
9876
fi
9977
}
10078

79+
# Fetch latest release version from GitHub if VERSION is not set
80+
get_latest_version() {
81+
local latest
82+
if command -v jq >/dev/null 2>&1; then
83+
latest=$(curl $INSECURE_OPTION -# --retry 5 -fSL https://api.github.com/repos/appleboy/CodeGPT/releases/latest | jq -r .tag_name)
84+
else
85+
latest=$(curl $INSECURE_OPTION -# --retry 5 -fSL https://api.github.com/repos/appleboy/CodeGPT/releases/latest | grep '"tag_name":' | sed -E 's/.*"tag_name": ?"v?([^"]+)".*/\1/')
86+
fi
87+
# Remove leading 'v' if present
88+
latest="${latest#v}"
89+
echo "$latest"
90+
}
91+
92+
CURL_INSECURE="${CURL_INSECURE:-false}"
93+
if [[ "${CURL_INSECURE}" != 'true' && "${CURL_INSECURE}" != 'false' ]]; then
94+
log_error "CURL_INSECURE must be either 'true' or 'false'" 4
95+
fi
96+
if [[ "${CURL_INSECURE}" == 'true' ]]; then
97+
print_message warning "CURL_INSECURE is set to true. Proceeding with insecure download."
98+
fi
99+
INSECURE_OPTION=""
100+
if [[ "${CURL_INSECURE}" == 'true' ]]; then
101+
INSECURE_OPTION="--insecure"
102+
fi
103+
104+
if [[ -z "${VERSION:-}" ]]; then
105+
LATEST_VERSION=$(get_latest_version)
106+
if [[ -z "$LATEST_VERSION" ]]; then
107+
log_error "Failed to fetch the latest version from GitHub." 6
108+
fi
109+
VERSION="$LATEST_VERSION"
110+
fi
111+
112+
# Check if VERSION is a valid semantic version
113+
if ! [[ "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
114+
log_error "Invalid version format: $VERSION. Expected format: x.y.z" 1
115+
fi
116+
117+
RELEASE_URL="${RELEASE_URL:-https://github.com/appleboy/CodeGPT/releases/download}"
118+
INSTALL_DIR="${INSTALL_DIR:-$HOME/.codegpt/bin}"
119+
CLIENT_PLATFORM="${CLIENT_PLATFORM:-$(uname -s | tr '[:upper:]' '[:lower:]')}"
120+
CLIENT_ARCH="${CLIENT_ARCH:-$(uname -m)}"
121+
122+
detect_client_info
123+
download_and_install
124+
101125
XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
102126

103127
current_shell=$(basename "$SHELL")

0 commit comments

Comments
 (0)