install 13 KB

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