install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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;5;214m'
  7. NC='\033[0m' # No Color
  8. usage() {
  9. cat <<EOF
  10. OpenCode Installer
  11. Usage: install.sh [options]
  12. Options:
  13. -h, --help Display this help message
  14. -v, --version <version> Install a specific version (e.g., 1.0.180)
  15. --no-modify-path Don't modify shell config files (.zshrc, .bashrc, etc.)
  16. Examples:
  17. curl -fsSL https://opencode.ai/install | bash
  18. curl -fsSL https://opencode.ai/install | bash -s -- --version 1.0.180
  19. EOF
  20. }
  21. requested_version=${VERSION:-}
  22. no_modify_path=false
  23. while [[ $# -gt 0 ]]; do
  24. case "$1" in
  25. -h|--help)
  26. usage
  27. exit 0
  28. ;;
  29. -v|--version)
  30. if [[ -n "${2:-}" ]]; then
  31. requested_version="$2"
  32. shift 2
  33. else
  34. echo -e "${RED}Error: --version requires a version argument${NC}"
  35. exit 1
  36. fi
  37. ;;
  38. --no-modify-path)
  39. no_modify_path=true
  40. shift
  41. ;;
  42. *)
  43. echo -e "${ORANGE}Warning: Unknown option '$1'${NC}" >&2
  44. shift
  45. ;;
  46. esac
  47. done
  48. raw_os=$(uname -s)
  49. os=$(echo "$raw_os" | tr '[:upper:]' '[:lower:]')
  50. case "$raw_os" in
  51. Darwin*) os="darwin" ;;
  52. Linux*) os="linux" ;;
  53. MINGW*|MSYS*|CYGWIN*) os="windows" ;;
  54. esac
  55. arch=$(uname -m)
  56. if [[ "$arch" == "aarch64" ]]; then
  57. arch="arm64"
  58. fi
  59. if [[ "$arch" == "x86_64" ]]; then
  60. arch="x64"
  61. fi
  62. if [ "$os" = "darwin" ] && [ "$arch" = "x64" ]; then
  63. rosetta_flag=$(sysctl -n sysctl.proc_translated 2>/dev/null || echo 0)
  64. if [ "$rosetta_flag" = "1" ]; then
  65. arch="arm64"
  66. fi
  67. fi
  68. combo="$os-$arch"
  69. case "$combo" in
  70. linux-x64|linux-arm64|darwin-x64|darwin-arm64|windows-x64)
  71. ;;
  72. *)
  73. echo -e "${RED}Unsupported OS/Arch: $os/$arch${NC}"
  74. exit 1
  75. ;;
  76. esac
  77. archive_ext=".zip"
  78. if [ "$os" = "linux" ]; then
  79. archive_ext=".tar.gz"
  80. fi
  81. is_musl=false
  82. if [ "$os" = "linux" ]; then
  83. if [ -f /etc/alpine-release ]; then
  84. is_musl=true
  85. fi
  86. if command -v ldd >/dev/null 2>&1; then
  87. if ldd --version 2>&1 | grep -qi musl; then
  88. is_musl=true
  89. fi
  90. fi
  91. fi
  92. needs_baseline=false
  93. if [ "$arch" = "x64" ]; then
  94. if [ "$os" = "linux" ]; then
  95. if ! grep -qi avx2 /proc/cpuinfo 2>/dev/null; then
  96. needs_baseline=true
  97. fi
  98. fi
  99. if [ "$os" = "darwin" ]; then
  100. avx2=$(sysctl -n hw.optional.avx2_0 2>/dev/null || echo 0)
  101. if [ "$avx2" != "1" ]; then
  102. needs_baseline=true
  103. fi
  104. fi
  105. fi
  106. target="$os-$arch"
  107. if [ "$needs_baseline" = "true" ]; then
  108. target="$target-baseline"
  109. fi
  110. if [ "$is_musl" = "true" ]; then
  111. target="$target-musl"
  112. fi
  113. filename="$APP-$target$archive_ext"
  114. if [ "$os" = "linux" ]; then
  115. if ! command -v tar >/dev/null 2>&1; then
  116. echo -e "${RED}Error: 'tar' is required but not installed.${NC}"
  117. exit 1
  118. fi
  119. else
  120. if ! command -v unzip >/dev/null 2>&1; then
  121. echo -e "${RED}Error: 'unzip' is required but not installed.${NC}"
  122. exit 1
  123. fi
  124. fi
  125. INSTALL_DIR=$HOME/.opencode/bin
  126. mkdir -p "$INSTALL_DIR"
  127. if [ -z "$requested_version" ]; then
  128. url="https://github.com/sst/opencode/releases/latest/download/$filename"
  129. specific_version=$(curl -s https://api.github.com/repos/sst/opencode/releases/latest | sed -n 's/.*"tag_name": *"v\([^"]*\)".*/\1/p')
  130. if [[ $? -ne 0 || -z "$specific_version" ]]; then
  131. echo -e "${RED}Failed to fetch version information${NC}"
  132. exit 1
  133. fi
  134. else
  135. url="https://github.com/sst/opencode/releases/download/v${requested_version}/$filename"
  136. specific_version=$requested_version
  137. fi
  138. print_message() {
  139. local level=$1
  140. local message=$2
  141. local color=""
  142. case $level in
  143. info) color="${NC}" ;;
  144. warning) color="${NC}" ;;
  145. error) color="${RED}" ;;
  146. esac
  147. echo -e "${color}${message}${NC}"
  148. }
  149. check_version() {
  150. if command -v opencode >/dev/null 2>&1; then
  151. opencode_path=$(which opencode)
  152. ## TODO: check if version is installed
  153. # installed_version=$(opencode version)
  154. installed_version="0.0.1"
  155. installed_version=$(echo $installed_version | awk '{print $2}')
  156. if [[ "$installed_version" != "$specific_version" ]]; then
  157. print_message info "${MUTED}Installed version: ${NC}$installed_version."
  158. else
  159. print_message info "${MUTED}Version ${NC}$specific_version${MUTED} already installed"
  160. exit 0
  161. fi
  162. fi
  163. }
  164. unbuffered_sed() {
  165. if echo | sed -u -e "" >/dev/null 2>&1; then
  166. sed -nu "$@"
  167. elif echo | sed -l -e "" >/dev/null 2>&1; then
  168. sed -nl "$@"
  169. else
  170. local pad="$(printf "\n%512s" "")"
  171. sed -ne "s/$/\\${pad}/" "$@"
  172. fi
  173. }
  174. print_progress() {
  175. local bytes="$1"
  176. local length="$2"
  177. [ "$length" -gt 0 ] || return 0
  178. local width=50
  179. local percent=$(( bytes * 100 / length ))
  180. [ "$percent" -gt 100 ] && percent=100
  181. local on=$(( percent * width / 100 ))
  182. local off=$(( width - on ))
  183. local filled=$(printf "%*s" "$on" "")
  184. filled=${filled// /■}
  185. local empty=$(printf "%*s" "$off" "")
  186. empty=${empty// /・}
  187. printf "\r${ORANGE}%s%s %3d%%${NC}" "$filled" "$empty" "$percent" >&4
  188. }
  189. download_with_progress() {
  190. local url="$1"
  191. local output="$2"
  192. if [ -t 2 ]; then
  193. exec 4>&2
  194. else
  195. exec 4>/dev/null
  196. fi
  197. local tmp_dir=${TMPDIR:-/tmp}
  198. local basename="${tmp_dir}/opencode_install_$$"
  199. local tracefile="${basename}.trace"
  200. rm -f "$tracefile"
  201. mkfifo "$tracefile"
  202. # Hide cursor
  203. printf "\033[?25l" >&4
  204. trap "trap - RETURN; rm -f \"$tracefile\"; printf '\033[?25h' >&4; exec 4>&-" RETURN
  205. (
  206. curl --trace-ascii "$tracefile" -s -L -o "$output" "$url"
  207. ) &
  208. local curl_pid=$!
  209. unbuffered_sed \
  210. -e 'y/ACDEGHLNORTV/acdeghlnortv/' \
  211. -e '/^0000: content-length:/p' \
  212. -e '/^<= recv data/p' \
  213. "$tracefile" | \
  214. {
  215. local length=0
  216. local bytes=0
  217. while IFS=" " read -r -a line; do
  218. [ "${#line[@]}" -lt 2 ] && continue
  219. local tag="${line[0]} ${line[1]}"
  220. if [ "$tag" = "0000: content-length:" ]; then
  221. length="${line[2]}"
  222. length=$(echo "$length" | tr -d '\r')
  223. bytes=0
  224. elif [ "$tag" = "<= recv" ]; then
  225. local size="${line[3]}"
  226. bytes=$(( bytes + size ))
  227. if [ "$length" -gt 0 ]; then
  228. print_progress "$bytes" "$length"
  229. fi
  230. fi
  231. done
  232. }
  233. wait $curl_pid
  234. local ret=$?
  235. echo "" >&4
  236. return $ret
  237. }
  238. download_and_install() {
  239. print_message info "\n${MUTED}Installing ${NC}opencode ${MUTED}version: ${NC}$specific_version"
  240. local tmp_dir="${TMPDIR:-/tmp}/opencode_install_$$"
  241. mkdir -p "$tmp_dir"
  242. if [[ "$os" == "windows" ]] || ! [ -t 2 ] || ! download_with_progress "$url" "$tmp_dir/$filename"; then
  243. # Fallback to standard curl on Windows, non-TTY environments, or if custom progress fails
  244. curl -# -L -o "$tmp_dir/$filename" "$url"
  245. fi
  246. if [ "$os" = "linux" ]; then
  247. tar -xzf "$tmp_dir/$filename" -C "$tmp_dir"
  248. else
  249. unzip -q "$tmp_dir/$filename" -d "$tmp_dir"
  250. fi
  251. mv "$tmp_dir/opencode" "$INSTALL_DIR"
  252. chmod 755 "${INSTALL_DIR}/opencode"
  253. rm -rf "$tmp_dir"
  254. }
  255. check_version
  256. download_and_install
  257. add_to_path() {
  258. local config_file=$1
  259. local command=$2
  260. if grep -Fxq "$command" "$config_file"; then
  261. print_message info "Command already exists in $config_file, skipping write."
  262. elif [[ -w $config_file ]]; then
  263. echo -e "\n# opencode" >> "$config_file"
  264. echo "$command" >> "$config_file"
  265. print_message info "${MUTED}Successfully added ${NC}opencode ${MUTED}to \$PATH in ${NC}$config_file"
  266. else
  267. print_message warning "Manually add the directory to $config_file (or similar):"
  268. print_message info " $command"
  269. fi
  270. }
  271. XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
  272. current_shell=$(basename "$SHELL")
  273. case $current_shell in
  274. fish)
  275. config_files="$HOME/.config/fish/config.fish"
  276. ;;
  277. zsh)
  278. config_files="$HOME/.zshrc $HOME/.zshenv $XDG_CONFIG_HOME/zsh/.zshrc $XDG_CONFIG_HOME/zsh/.zshenv"
  279. ;;
  280. bash)
  281. config_files="$HOME/.bashrc $HOME/.bash_profile $HOME/.profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  282. ;;
  283. ash)
  284. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  285. ;;
  286. sh)
  287. config_files="$HOME/.ashrc $HOME/.profile /etc/profile"
  288. ;;
  289. *)
  290. # Default case if none of the above matches
  291. config_files="$HOME/.bashrc $HOME/.bash_profile $XDG_CONFIG_HOME/bash/.bashrc $XDG_CONFIG_HOME/bash/.bash_profile"
  292. ;;
  293. esac
  294. if [[ "$no_modify_path" != "true" ]]; then
  295. config_file=""
  296. for file in $config_files; do
  297. if [[ -f $file ]]; then
  298. config_file=$file
  299. break
  300. fi
  301. done
  302. if [[ -z $config_file ]]; then
  303. print_message warning "No config file found for $current_shell. You may need to manually add to PATH:"
  304. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  305. elif [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
  306. case $current_shell in
  307. fish)
  308. add_to_path "$config_file" "fish_add_path $INSTALL_DIR"
  309. ;;
  310. zsh)
  311. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  312. ;;
  313. bash)
  314. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  315. ;;
  316. ash)
  317. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  318. ;;
  319. sh)
  320. add_to_path "$config_file" "export PATH=$INSTALL_DIR:\$PATH"
  321. ;;
  322. *)
  323. export PATH=$INSTALL_DIR:$PATH
  324. print_message warning "Manually add the directory to $config_file (or similar):"
  325. print_message info " export PATH=$INSTALL_DIR:\$PATH"
  326. ;;
  327. esac
  328. fi
  329. fi
  330. if [ -n "${GITHUB_ACTIONS-}" ] && [ "${GITHUB_ACTIONS}" == "true" ]; then
  331. echo "$INSTALL_DIR" >> $GITHUB_PATH
  332. print_message info "Added $INSTALL_DIR to \$GITHUB_PATH"
  333. fi
  334. echo -e ""
  335. echo -e "${MUTED}  ${NC} ▄ "
  336. echo -e "${MUTED}█▀▀█ █▀▀█ █▀▀█ █▀▀▄ ${NC}█▀▀▀ █▀▀█ █▀▀█ █▀▀█"
  337. echo -e "${MUTED}█░░█ █░░█ █▀▀▀ █░░█ ${NC}█░░░ █░░█ █░░█ █▀▀▀"
  338. echo -e "${MUTED}▀▀▀▀ █▀▀▀ ▀▀▀▀ ▀ ▀ ${NC}▀▀▀▀ ▀▀▀▀ ▀▀▀▀ ▀▀▀▀"
  339. echo -e ""
  340. echo -e ""
  341. echo -e "${MUTED}OpenCode includes free models, to start:${NC}"
  342. echo -e ""
  343. echo -e "cd <project> ${MUTED}# Open directory${NC}"
  344. echo -e "opencode ${MUTED}# Run command${NC}"
  345. echo -e ""
  346. echo -e "${MUTED}For more information visit ${NC}https://opencode.ai/docs"
  347. echo -e ""
  348. echo -e ""