install 5.1 KB

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