install 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 [[ -w $config_file ]]; then
  89. echo -e "\n# opencode" >> "$config_file"
  90. echo "$command" >> "$config_file"
  91. print_message info "Successfully added ${ORANGE}opencode ${GREEN}to \$PATH in $config_file"
  92. else
  93. print_message warning "Manually add the directory to $config_file (or similar):"
  94. print_message info " $command"
  95. fi
  96. }
  97. XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
  98. current_shell=$(basename "$SHELL")
  99. case $current_shell in
  100. fish)
  101. config_files="$HOME/.config/fish/config.fish"
  102. ;;
  103. zsh)
  104. config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
  105. ;;
  106. bash)
  107. config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  108. ;;
  109. ash)
  110. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  111. ;;
  112. sh)
  113. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  114. ;;
  115. *)
  116. # Default case if none of the above matches
  117. config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  118. ;;
  119. esac
  120. config_file=""
  121. for file in $config_files; do
  122. if [[ -f $file ]]; then
  123. config_file=$file
  124. break
  125. fi
  126. done
  127. if [[ -z $config_file ]]; then
  128. print_message error "No config file found for $current_shell. Checked files: ${config_files[@]}"
  129. exit 1
  130. fi
  131. if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
  132. case $current_shell in
  133. fish)
  134. add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
  135. ;;
  136. zsh)
  137. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  138. ;;
  139. bash)
  140. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  141. ;;
  142. ash)
  143. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  144. ;;
  145. sh)
  146. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  147. ;;
  148. *)
  149. print_message warning "Manually add the directory to $config_file (or similar):"
  150. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  151. ;;
  152. esac
  153. fi
  154. if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
  155. echo "$INSTALL_DIR" >> $GITHUB_PATH
  156. print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
  157. fi