install 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. APP=opencode
  4. MUTED='\033[0;2m'
  5. RED='\033[0;31m'
  6. ORANGE='\033[38;2;255;140;0m'
  7. NC='\033[0m' # No Color
  8. requested_version=${VERSION:-}
  9. raw_os=$(uname -s)
  10. os=$(echo "$raw_os" | tr '[:upper:]' '[:lower:]')
  11. # Normalize various Unix-like identifiers
  12. case "$raw_os" in
  13. Darwin*) os="darwin" ;;
  14. Linux*) os="linux" ;;
  15. MINGW*|MSYS*|CYGWIN*) os="windows" ;;
  16. esac
  17. arch=$(uname -m)
  18. if [[ "$arch" == "aarch64" ]]; then
  19. arch="arm64"
  20. elif [[ "$arch" == "x86_64" ]]; then
  21. arch="x64"
  22. fi
  23. filename="$APP-$os-$arch.zip"
  24. case "$filename" in
  25. *"-linux-"*)
  26. [[ "$arch" == "x64" || "$arch" == "arm64" ]] || exit 1
  27. ;;
  28. *"-darwin-"*)
  29. [[ "$arch" == "x64" || "$arch" == "arm64" ]] || exit 1
  30. ;;
  31. *"-windows-"*)
  32. [[ "$arch" == "x64" ]] || exit 1
  33. ;;
  34. *)
  35. echo -e "${RED}Unsupported OS/Arch: $os/$arch${NC}"
  36. exit 1
  37. ;;
  38. esac
  39. INSTALL_DIR=$HOME/.opencode/bin
  40. mkdir -p "$INSTALL_DIR"
  41. if [ -z "$requested_version" ]; then
  42. url="https://github.com/sst/opencode/releases/latest/download/$filename"
  43. specific_version=$(curl -s https://api.github.com/repos/sst/opencode/releases/latest | sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p')
  44. if [[ $? -ne 0 || -z "$specific_version" ]]; then
  45. echo -e "${RED}Failed to fetch version information${NC}"
  46. exit 1
  47. fi
  48. else
  49. url="https://github.com/sst/opencode/releases/download/v${requested_version}/$filename"
  50. specific_version=$requested_version
  51. fi
  52. print_message() {
  53. local level=$1
  54. local message=$2
  55. local color=""
  56. case $level in
  57. info) color="${NC}" ;;
  58. warning) color="${NC}" ;;
  59. error) color="${RED}" ;;
  60. esac
  61. echo -e "${color}${message}${NC}"
  62. }
  63. check_version() {
  64. if command -v opencode >/dev/null 2>&1; then
  65. opencode_path=$(which opencode)
  66. ## TODO: check if version is installed
  67. # installed_version=$(opencode version)
  68. installed_version="0.0.1"
  69. installed_version=$(echo $installed_version | awk '{print $2}')
  70. if [[ "$installed_version" != "$specific_version" ]]; then
  71. print_message info "${MUTED}Installed version: ${NC}$installed_version."
  72. else
  73. print_message info "${MUTED}Version ${NC}$specific_version${MUTED} already installed"
  74. exit 0
  75. fi
  76. fi
  77. }
  78. unbuffered_sed() {
  79. if echo | sed -u >/dev/null 2>&1; then
  80. sed -nu "$@"
  81. elif echo | sed -l >/dev/null 2>&1; then
  82. sed -nl "$@"
  83. else
  84. local pad="$(printf "\n%512s" "")"
  85. sed -ne "s/$/\\${pad}/" "$@"
  86. fi
  87. }
  88. print_progress() {
  89. local bytes="$1"
  90. local length="$2"
  91. [ "$length" -gt 0 ] || return 0
  92. local width=50
  93. local percent=$(( bytes * 100 / length ))
  94. [ "$percent" -gt 100 ] && percent=100
  95. local on=$(( percent * width / 100 ))
  96. local off=$(( width - on ))
  97. local full_filled=$(printf "%.0s■" {1..50})
  98. local full_empty=$(printf "%.0s・" {1..50})
  99. printf "\r${ORANGE}%s%s %3d%%${NC}" "${full_filled:0:$on}" "${full_empty:0:$off}" "$percent" >&4
  100. }
  101. download_with_progress() {
  102. local url="$1"
  103. local output="$2"
  104. if [ -t 2 ]; then
  105. exec 4>&2
  106. else
  107. exec 4>/dev/null
  108. fi
  109. local tmp_dir=${TMPDIR:-/tmp}
  110. local basename="${tmp_dir}/opencode_install_$$"
  111. local tracefile="${basename}.trace"
  112. rm -f "$tracefile"
  113. mkfifo "$tracefile"
  114. # Hide cursor
  115. printf "\033[?25l" >&4
  116. trap "trap - RETURN; rm -f \"$tracefile\"; printf '\033[?25h' >&4; exec 4>&-" RETURN
  117. (
  118. curl --trace-ascii "$tracefile" -s -L -o "$output" "$url"
  119. ) &
  120. local curl_pid=$!
  121. unbuffered_sed \
  122. -e 'y/ACDEGHLNORTV/acdeghlnortv/' \
  123. -e '/^0000: content-length:/p' \
  124. -e '/^<= recv data/p' \
  125. "$tracefile" | \
  126. {
  127. local length=0
  128. local bytes=0
  129. while IFS=" " read -r -a line; do
  130. local tag="${line[0]} ${line[1]}"
  131. if [ "$tag" = "0000: content-length:" ]; then
  132. length="${line[2]}"
  133. length=$(echo "$length" | tr -d '\r')
  134. bytes=0
  135. elif [ "$tag" = "<= recv" ]; then
  136. local size="${line[3]}"
  137. bytes=$(( bytes + size ))
  138. if [ "$length" -gt 0 ]; then
  139. print_progress "$bytes" "$length"
  140. fi
  141. fi
  142. done
  143. }
  144. wait $curl_pid
  145. local ret=$?
  146. echo "" >&4
  147. return $ret
  148. }
  149. download_and_install() {
  150. print_message info "\n${MUTED}Installing ${NC}opencode ${MUTED}version: ${NC}$specific_version"
  151. mkdir -p opencodetmp && cd opencodetmp
  152. if ! download_with_progress "$url" "$filename"; then
  153. # Fallback to standard curl if custom fails for some reason
  154. curl -# -L -o "$filename" "$url"
  155. fi
  156. unzip -q "$filename"
  157. mv opencode "$INSTALL_DIR"
  158. chmod 755 "${INSTALL_DIR}/opencode"
  159. cd .. && rm -rf opencodetmp
  160. }
  161. check_version
  162. download_and_install
  163. add_to_path() {
  164. local config_file=$1
  165. local command=$2
  166. if grep -Fxq "$command" "$config_file"; then
  167. print_message info "Command already exists in $config_file, skipping write."
  168. elif [[ -w $config_file ]]; then
  169. echo -e "\n# opencode" >> "$config_file"
  170. echo "$command" >> "$config_file"
  171. print_message info "${MUTED}Successfully added ${NC}opencode ${MUTED}to \$PATH in ${NC}$config_file"
  172. else
  173. print_message warning "Manually add the directory to $config_file (or similar):"
  174. print_message info " $command"
  175. fi
  176. }
  177. XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
  178. current_shell=$(basename "$SHELL")
  179. case $current_shell in
  180. fish)
  181. config_files="$HOME/.config/fish/config.fish"
  182. ;;
  183. zsh)
  184. config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
  185. ;;
  186. bash)
  187. config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  188. ;;
  189. ash)
  190. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  191. ;;
  192. sh)
  193. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  194. ;;
  195. *)
  196. # Default case if none of the above matches
  197. config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  198. ;;
  199. esac
  200. config_file=""
  201. for file in $config_files; do
  202. if [[ -f $file ]]; then
  203. config_file=$file
  204. break
  205. fi
  206. done
  207. if [[ -z $config_file ]]; then
  208. print_message error "No config file found for $current_shell. Checked files: ${config_files[@]}"
  209. exit 1
  210. fi
  211. if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
  212. case $current_shell in
  213. fish)
  214. add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
  215. ;;
  216. zsh)
  217. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  218. ;;
  219. bash)
  220. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  221. ;;
  222. ash)
  223. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  224. ;;
  225. sh)
  226. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  227. ;;
  228. *)
  229. export PATH=$INSTALL_DIR:$PATH
  230. print_message warning "Manually add the directory to $config_file (or similar):"
  231. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  232. ;;
  233. esac
  234. fi
  235. if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
  236. echo "$INSTALL_DIR" >> $GITHUB_PATH
  237. print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
  238. fi
  239. echo -e ""
  240. echo -e "${MUTED}  ${NC} ▄ "
  241. echo -e "${MUTED}█▀▀█ █▀▀█ █▀▀█ █▀▀▄ ${NC}█▀▀▀ █▀▀█ █▀▀█ █▀▀█"
  242. echo -e "${MUTED}█░░█ █░░█ █▀▀▀ █░░█ ${NC}█░░░ █░░█ █░░█ █▀▀▀"
  243. echo -e "${MUTED}▀▀▀▀ █▀▀▀ ▀▀▀▀ ▀ ▀ ${NC}▀▀▀▀ ▀▀▀▀ ▀▀▀▀ ▀▀▀▀"
  244. echo -e ""
  245. echo -e ""
  246. echo -e "${MUTED}To get started, navigate to a project and run:${NC}"
  247. echo -e "opencode ${MUTED}Use free models${NC}"
  248. echo -e "opencode auth login ${MUTED}Add paid provider API keys${NC}"
  249. echo -e "opencode help ${MUTED}List commands and options${NC}"
  250. echo -e ""
  251. echo -e "${MUTED}For more information visit ${NC}https://opencode.ai/docs"
  252. echo -e ""
  253. echo -e ""