|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +APP=CodeGPT |
| 6 | + |
| 7 | +RED='\033[0;31m' |
| 8 | +GREEN='\033[0;32m' |
| 9 | +YELLOW='\033[1;33m' |
| 10 | +ORANGE='\033[38;2;255;140;0m' |
| 11 | +NC='\033[0m' # No Color |
| 12 | + |
| 13 | +RELEASE_URL="${RELEASE_URL:-https://github.com/appleboy/CodeGPT/releases/download}" |
| 14 | +VERSION="${VERSION:-0.16.1}" |
| 15 | +INSTALL_DIR="${INSTALL_DIR:-$HOME/.codegpt/bin}" |
| 16 | + |
| 17 | +function print_message() { |
| 18 | + local level=$1 |
| 19 | + local message=$2 |
| 20 | + local color="" |
| 21 | + |
| 22 | + case $level in |
| 23 | + info) color="${GREEN}" ;; |
| 24 | + warning) color="${YELLOW}" ;; |
| 25 | + error) color="${RED}" ;; |
| 26 | + esac |
| 27 | + |
| 28 | + printf "%b\n" "${color}${message}${NC}" |
| 29 | +} |
| 30 | + |
| 31 | +function log_error() { |
| 32 | + print_message error "$1" >&2 |
| 33 | + exit "$2" |
| 34 | +} |
| 35 | + |
| 36 | +function detect_client_info() { |
| 37 | + CLIENT_PLATFORM="${SSH_CLIENT_OS:-$(uname -s | tr '[:upper:]' '[:lower:]')}" |
| 38 | + CLIENT_ARCH="${SSH_CLIENT_ARCH:-$(uname -m)}" |
| 39 | + |
| 40 | + case "${CLIENT_PLATFORM}" in |
| 41 | + darwin | linux | windows) ;; |
| 42 | + *) log_error "Unknown or unsupported platform: ${CLIENT_PLATFORM}. Supported platforms are Linux, Darwin, and Windows." 2 ;; |
| 43 | + esac |
| 44 | + |
| 45 | + case "${CLIENT_ARCH}" in |
| 46 | + x86_64* | i?86_64* | amd64*) CLIENT_ARCH="amd64" ;; |
| 47 | + aarch64* | arm64*) CLIENT_ARCH="arm64" ;; |
| 48 | + *) log_error "Unknown or unsupported architecture: ${CLIENT_ARCH}. Supported architectures are x86_64, i686, and arm64." 3 ;; |
| 49 | + esac |
| 50 | +} |
| 51 | + |
| 52 | +detect_client_info |
| 53 | + |
| 54 | +function download_and_install() { |
| 55 | + DOWNLOAD_URL_PREFIX="${RELEASE_URL}/v${VERSION}" |
| 56 | + CLIENT_BINARY="CodeGPT-${VERSION}-${CLIENT_PLATFORM}-${CLIENT_ARCH}" |
| 57 | + print_message info "Downloading ${CLIENT_BINARY} from ${DOWNLOAD_URL_PREFIX}" |
| 58 | + |
| 59 | + CURL_INSECURE="${CURL_INSECURE:-false}" |
| 60 | + if [[ "${CURL_INSECURE}" != 'true' && "${CURL_INSECURE}" != 'false' ]]; then |
| 61 | + log_error "CURL_INSECURE must be either 'true' or 'false'" 4 |
| 62 | + fi |
| 63 | + if [[ "${CURL_INSECURE}" == 'true' ]]; then |
| 64 | + print_message warning "CURL_INSECURE is set to true. Proceeding with insecure download." |
| 65 | + fi |
| 66 | + INSECURE_OPTION="" |
| 67 | + if [[ "${CURL_INSECURE}" == 'true' ]]; then |
| 68 | + INSECURE_OPTION="--insecure" |
| 69 | + fi |
| 70 | + mkdir -p "$INSTALL_DIR" || log_error "Failed to create directory: $INSTALL_DIR" 5 |
| 71 | + TARGET="$INSTALL_DIR/${CLIENT_BINARY}" |
| 72 | + |
| 73 | + curl -# -fSL --retry 5 --keepalive-time 2 ${INSECURE_OPTION} "${DOWNLOAD_URL_PREFIX}/${CLIENT_BINARY}" -o "${TARGET}" |
| 74 | + chmod +x "${TARGET}" |
| 75 | + # Rename the binary to codegpt |
| 76 | + mv "${TARGET}" "${INSTALL_DIR}/codegpt" |
| 77 | + # show the version |
| 78 | + "${INSTALL_DIR}/codegpt" version |
| 79 | +} |
| 80 | + |
| 81 | +download_and_install |
| 82 | + |
| 83 | +function add_to_path() { |
| 84 | + local config_file=$1 |
| 85 | + local command=$2 |
| 86 | + |
| 87 | + if [[ -w $config_file ]]; then |
| 88 | + printf "\n# codegpt\n" >>"$config_file" |
| 89 | + echo "$command" >>"$config_file" |
| 90 | + print_message info "Successfully added ${ORANGE}codegpt ${GREEN}to \$PATH in $config_file" |
| 91 | + else |
| 92 | + print_message warning "Manually add the directory to $config_file (or similar):" |
| 93 | + print_message info " $command" |
| 94 | + fi |
| 95 | +} |
| 96 | + |
| 97 | +XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config} |
| 98 | + |
| 99 | +current_shell=$(basename "$SHELL") |
| 100 | +case $current_shell in |
| 101 | +fish) |
| 102 | + config_files="$HOME/.config/fish/config.fish" |
| 103 | + ;; |
| 104 | +zsh) |
| 105 | + config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv" |
| 106 | + ;; |
| 107 | +bash) |
| 108 | + config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile" |
| 109 | + ;; |
| 110 | +ash) |
| 111 | + config_files="$HOME/.ashrc $HOME/.profile /etc/profile" |
| 112 | + ;; |
| 113 | +sh) |
| 114 | + config_files="$HOME/.ashrc $HOME/.profile /etc/profile" |
| 115 | + ;; |
| 116 | +*) |
| 117 | + # Default case if none of the above matches |
| 118 | + config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile" |
| 119 | + ;; |
| 120 | +esac |
| 121 | + |
| 122 | +config_file="" |
| 123 | +for file in $config_files; do |
| 124 | + if [[ -f $file ]]; then |
| 125 | + config_file=$file |
| 126 | + break |
| 127 | + fi |
| 128 | +done |
| 129 | + |
| 130 | +if [[ -z $config_file ]]; then |
| 131 | + log_error "No config file found for $current_shell. Checked files: ${config_files[@]}" 1 |
| 132 | +fi |
| 133 | + |
| 134 | +if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then |
| 135 | + case $current_shell in |
| 136 | + fish) |
| 137 | + add_to_path "$config_file" "fish_add_path $INSTALL_DIR" |
| 138 | + ;; |
| 139 | + zsh) |
| 140 | + add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" |
| 141 | + ;; |
| 142 | + bash) |
| 143 | + add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" |
| 144 | + ;; |
| 145 | + ash) |
| 146 | + add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" |
| 147 | + ;; |
| 148 | + sh) |
| 149 | + add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH" |
| 150 | + ;; |
| 151 | + *) |
| 152 | + print_message warning "Manually add the directory to $config_file (or similar):" |
| 153 | + print_message info " export PATH=$INSTALL_DIR:\$PATH" |
| 154 | + ;; |
| 155 | + esac |
| 156 | +fi |
0 commit comments