install 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. APP=opencode
  4. RED='\033[0;31m'
  5. GREEN='\033[0;32m'
  6. YELLOW='\033[1;33m'
  7. ORANGE='\033[38;2;255;140;0m'
  8. NC='\033[0m' # No Color
  9. requested_version=${VERSION:-}
  10. raw_os=$(uname -s)
  11. os=$(echo "$raw_os" | tr '[:upper:]' '[:lower:]')
  12. # Normalize various Unix-like identifiers
  13. case "$raw_os" in
  14. Darwin*) os="darwin" ;;
  15. Linux*) os="linux" ;;
  16. MINGW*|MSYS*|CYGWIN*) os="windows" ;;
  17. esac
  18. arch=$(uname -m)
  19. if [[ "$arch" == "aarch64" ]]; then
  20. arch="arm64"
  21. elif [[ "$arch" == "x86_64" ]]; then
  22. arch="x64"
  23. fi
  24. filename="$APP-$os-$arch.zip"
  25. case "$filename" in
  26. *"-linux-"*)
  27. [[ "$arch" == "x64" || "$arch" == "arm64" ]] || exit 1
  28. ;;
  29. *"-darwin-"*)
  30. [[ "$arch" == "x64" || "$arch" == "arm64" ]] || exit 1
  31. ;;
  32. *"-windows-"*)
  33. [[ "$arch" == "x64" ]] || exit 1
  34. ;;
  35. *)
  36. echo -e "${RED}Unsupported OS/Arch: $os/$arch${NC}"
  37. exit 1
  38. ;;
  39. esac
  40. INSTALL_DIR=$HOME/.opencode/bin
  41. mkdir -p "$INSTALL_DIR"
  42. if [ -z "$requested_version" ]; then
  43. url="https://github.com/sst/opencode/releases/latest/download/$filename"
  44. specific_version=$(curl -s https://api.github.com/repos/sst/opencode/releases/latest | sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p')
  45. if [[ $? -ne 0 || -z "$specific_version" ]]; then
  46. echo -e "${RED}Failed to fetch version information${NC}"
  47. exit 1
  48. fi
  49. else
  50. url="https://github.com/sst/opencode/releases/download/v${requested_version}/$filename"
  51. specific_version=$requested_version
  52. fi
  53. print_message() {
  54. local level=$1
  55. local message=$2
  56. local color=""
  57. case $level in
  58. info) color="${GREEN}" ;;
  59. warning) color="${YELLOW}" ;;
  60. error) color="${RED}" ;;
  61. esac
  62. echo -e "${color}${message}${NC}"
  63. }
  64. check_version() {
  65. if command -v opencode >/dev/null 2>&1; then
  66. opencode_path=$(which opencode)
  67. ## TODO: check if version is installed
  68. # installed_version=$(opencode version)
  69. installed_version="0.0.1"
  70. installed_version=$(echo $installed_version | awk '{print $2}')
  71. if [[ "$installed_version" != "$specific_version" ]]; then
  72. print_message info "Installed version: ${YELLOW}$installed_version."
  73. else
  74. print_message info "Version ${YELLOW}$specific_version${GREEN} already installed"
  75. exit 0
  76. fi
  77. fi
  78. }
  79. download_and_install() {
  80. print_message info "Downloading ${ORANGE}opencode ${GREEN}version: ${YELLOW}$specific_version ${GREEN}..."
  81. mkdir -p opencodetmp && cd opencodetmp
  82. curl -# -L -o "$filename" "$url"
  83. unzip -q "$filename"
  84. mv opencode "$INSTALL_DIR"
  85. chmod 755 "${INSTALL_DIR}/opencode"
  86. cd .. && rm -rf opencodetmp
  87. }
  88. check_version
  89. download_and_install
  90. add_to_path() {
  91. local config_file=$1
  92. local command=$2
  93. if grep -Fxq "$command" "$config_file"; then
  94. print_message info "Command already exists in $config_file, skipping write."
  95. elif [[ -w $config_file ]]; then
  96. echo -e "\n# opencode" >> "$config_file"
  97. echo "$command" >> "$config_file"
  98. print_message info "Successfully added ${ORANGE}opencode ${GREEN}to \$PATH in $config_file"
  99. else
  100. print_message warning "Manually add the directory to $config_file (or similar):"
  101. print_message info " $command"
  102. fi
  103. }
  104. XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
  105. current_shell=$(basename "$SHELL")
  106. case $current_shell in
  107. fish)
  108. config_files="$HOME/.config/fish/config.fish"
  109. ;;
  110. zsh)
  111. config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
  112. ;;
  113. bash)
  114. config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  115. ;;
  116. ash)
  117. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  118. ;;
  119. sh)
  120. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  121. ;;
  122. *)
  123. # Default case if none of the above matches
  124. config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  125. ;;
  126. esac
  127. config_file=""
  128. for file in $config_files; do
  129. if [[ -f $file ]]; then
  130. config_file=$file
  131. break
  132. fi
  133. done
  134. if [[ -z $config_file ]]; then
  135. print_message error "No config file found for $current_shell. Checked files: ${config_files[@]}"
  136. exit 1
  137. fi
  138. if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
  139. case $current_shell in
  140. fish)
  141. add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
  142. ;;
  143. zsh)
  144. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  145. ;;
  146. bash)
  147. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  148. ;;
  149. ash)
  150. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  151. ;;
  152. sh)
  153. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  154. ;;
  155. *)
  156. export PATH=$INSTALL_DIR:$PATH
  157. print_message warning "Manually add the directory to $config_file (or similar):"
  158. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  159. ;;
  160. esac
  161. fi
  162. if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
  163. echo "$INSTALL_DIR" >> $GITHUB_PATH
  164. print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
  165. fi