|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +OPENCODE_BIN_DIR="${OPENCODE_INSTALL_DIR:-${RUNNER_TOOL_CACHE:-$HOME/.cache}/opencode/bin}" |
| 6 | +OPENCODE_CACHE_DIR="${XDG_CACHE_HOME:-$HOME/.cache}" |
| 7 | +INSTALL_URL="${OPENCODE_INSTALL_URL:-https://opencode.ai/install}" |
| 8 | +MAX_ATTEMPTS="${OPENCODE_INSTALL_ATTEMPTS:-3}" |
| 9 | + |
| 10 | +mkdir -p "$OPENCODE_BIN_DIR" |
| 11 | +mkdir -p "$OPENCODE_CACHE_DIR" |
| 12 | + |
| 13 | +export OPENCODE_INSTALL_DIR="$OPENCODE_BIN_DIR" |
| 14 | +export XDG_CACHE_HOME="$OPENCODE_CACHE_DIR" |
| 15 | +export PATH="$OPENCODE_BIN_DIR:$PATH" |
| 16 | + |
| 17 | +link_opencode_bin() { |
| 18 | + local source_bin="$1" |
| 19 | + |
| 20 | + if [ -x "$source_bin" ] && [ "$source_bin" != "$OPENCODE_BIN_DIR/opencode" ]; then |
| 21 | + ln -sf "$source_bin" "$OPENCODE_BIN_DIR/opencode" |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | +if command -v opencode >/dev/null 2>&1; then |
| 26 | + echo "OpenCode already installed at: $(command -v opencode)" |
| 27 | + opencode --version || true |
| 28 | + exit 0 |
| 29 | +fi |
| 30 | + |
| 31 | +attempt=1 |
| 32 | +while [ "$attempt" -le "$MAX_ATTEMPTS" ]; do |
| 33 | + echo "Installing OpenCode (attempt ${attempt}/${MAX_ATTEMPTS})" |
| 34 | + |
| 35 | + if curl \ |
| 36 | + --fail \ |
| 37 | + --silent \ |
| 38 | + --show-error \ |
| 39 | + --location \ |
| 40 | + --retry 5 \ |
| 41 | + --retry-all-errors \ |
| 42 | + --retry-delay 2 \ |
| 43 | + "$INSTALL_URL" | bash; then |
| 44 | + break |
| 45 | + fi |
| 46 | + |
| 47 | + if [ "$attempt" -eq "$MAX_ATTEMPTS" ]; then |
| 48 | + echo "OpenCode installation failed after ${MAX_ATTEMPTS} attempts" >&2 |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + |
| 52 | + sleep $((attempt * 5)) |
| 53 | + attempt=$((attempt + 1)) |
| 54 | +done |
| 55 | + |
| 56 | +if [ ! -x "$OPENCODE_BIN_DIR/opencode" ] && [ -x "$HOME/.opencode/bin/opencode" ]; then |
| 57 | + link_opencode_bin "$HOME/.opencode/bin/opencode" |
| 58 | +fi |
| 59 | + |
| 60 | +if ! command -v opencode >/dev/null 2>&1; then |
| 61 | + echo "OpenCode install script finished, but 'opencode' is still unavailable" >&2 |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +echo "OpenCode installed at: $(command -v opencode)" |
| 66 | +opencode --version || true |
0 commit comments