|
| 1 | +#!/usr/bin/env bash |
| 2 | +# install-flex — Flexion fork of opencode, automated installer |
| 3 | +# |
| 4 | +# Usage: |
| 5 | +# curl -fsSL https://raw.githubusercontent.com/flexion/opencode/flex/install-flex | bash |
| 6 | +# |
| 7 | +# What this does: |
| 8 | +# 1. Checks prerequisites (git, bun, aws-cli v2) |
| 9 | +# 2. Prompts: clone directory, AWS account ID, preferred AWS region |
| 10 | +# 3. Clones git@github.com:flexion/opencode.git (flex branch) — skipped if already present |
| 11 | +# 4. Installs dependencies and builds the native binary |
| 12 | +# 5. Writes [profile ClaudeCodeAccess] to ~/.aws/config |
| 13 | +# 6. Writes ~/.config/opencode/opencode.json (Bedrock + model config) |
| 14 | +# 7. Appends opencode-work() launcher function to ~/.zshrc or ~/.bashrc |
| 15 | +# |
| 16 | +# Existing files are never overwritten; each step is skipped with a warning |
| 17 | +# if the output already exists. |
| 18 | + |
| 19 | +set -euo pipefail |
| 20 | + |
| 21 | +# ── colors ──────────────────────────────────────────────────────────────────── |
| 22 | +if [ -t 1 ]; then |
| 23 | + BOLD=$'\033[1m'; GREEN=$'\033[0;32m'; YELLOW=$'\033[1;33m' |
| 24 | + BLUE=$'\033[0;34m'; RED=$'\033[0;31m'; NC=$'\033[0m' |
| 25 | +else |
| 26 | + BOLD=''; GREEN=''; YELLOW=''; BLUE=''; RED=''; NC='' |
| 27 | +fi |
| 28 | + |
| 29 | +# ── constants ───────────────────────────────────────────────────────────────── |
| 30 | +SSO_START_URL="https://identitycenter.amazonaws.com/ssoins-6684680a9b285ea2" |
| 31 | +SSO_REGION="us-east-2" |
| 32 | +SSO_ROLE_NAME="ClaudeCodeAccess" |
| 33 | +AWS_PROFILE="ClaudeCodeAccess" |
| 34 | +REPO_URL="git@github.com:flexion/opencode.git" |
| 35 | +BRANCH="flex" |
| 36 | + |
| 37 | +# ── helpers ─────────────────────────────────────────────────────────────────── |
| 38 | +info() { printf '%s▶%s %s\n' "$BLUE" "$NC" "$*"; } |
| 39 | +success() { printf '%s✔%s %s\n' "$GREEN" "$NC" "$*"; } |
| 40 | +warn() { printf '%s⚠%s %s\n' "$YELLOW" "$NC" "$*"; } |
| 41 | +die() { printf '%s✖%s %s\n' "$RED" "$NC" "$*" >&2; exit 1; } |
| 42 | + |
| 43 | +# ── step 1: prerequisites ───────────────────────────────────────────────────── |
| 44 | +check_prereqs() { |
| 45 | + info "Checking prerequisites..." |
| 46 | + local missing=() |
| 47 | + command -v git >/dev/null 2>&1 || missing+=("git") |
| 48 | + command -v bun >/dev/null 2>&1 || missing+=("bun (https://bun.sh)") |
| 49 | + command -v aws >/dev/null 2>&1 || missing+=("aws-cli v2 (https://aws.amazon.com/cli/)") |
| 50 | + if [ ${#missing[@]} -gt 0 ]; then |
| 51 | + die "Missing prerequisites:$(printf '\n • %s' "${missing[@]}")" |
| 52 | + fi |
| 53 | + success "Prerequisites OK" |
| 54 | +} |
| 55 | + |
| 56 | +# ── step 2: gather inputs ───────────────────────────────────────────────────── |
| 57 | +# All reads use /dev/tty so prompts work when stdin is the script (curl | bash). |
| 58 | +gather_inputs() { |
| 59 | + printf '\n%sFlexion opencode installer%s\n' "$BOLD" "$NC" |
| 60 | + printf '────────────────────────────────────────────\n' |
| 61 | + |
| 62 | + printf 'Clone directory [%s]: ' "$HOME/opencode" >/dev/tty |
| 63 | + IFS= read -r CLONE_DIR </dev/tty |
| 64 | + CLONE_DIR="${CLONE_DIR:-$HOME/opencode}" |
| 65 | + |
| 66 | + while true; do |
| 67 | + printf 'AWS account ID: ' >/dev/tty |
| 68 | + IFS= read -r ACCOUNT_ID </dev/tty |
| 69 | + [ -n "${ACCOUNT_ID:-}" ] && break |
| 70 | + warn "AWS account ID is required." |
| 71 | + done |
| 72 | + |
| 73 | + printf 'Preferred AWS region [us-east-1]: ' >/dev/tty |
| 74 | + IFS= read -r AWS_REGION </dev/tty |
| 75 | + AWS_REGION="${AWS_REGION:-us-east-1}" |
| 76 | + echo |
| 77 | +} |
| 78 | + |
| 79 | +# ── step 3: clone & build ───────────────────────────────────────────────────── |
| 80 | +clone_and_build() { |
| 81 | + if [ -d "$CLONE_DIR/.git" ]; then |
| 82 | + warn "Directory $CLONE_DIR already exists — skipping clone" |
| 83 | + info "Fetching and checking out $BRANCH branch..." |
| 84 | + git -C "$CLONE_DIR" fetch origin |
| 85 | + git -C "$CLONE_DIR" checkout "$BRANCH" |
| 86 | + else |
| 87 | + info "Cloning $REPO_URL → $CLONE_DIR ..." |
| 88 | + git clone --branch "$BRANCH" "$REPO_URL" "$CLONE_DIR" |
| 89 | + fi |
| 90 | + |
| 91 | + info "Installing dependencies..." |
| 92 | + # BUN_CONFIG_REGISTRY override prevents private registry redirects (e.g. CMS Artifactory) |
| 93 | + # from breaking public package resolution. |
| 94 | + (cd "$CLONE_DIR" && BUN_CONFIG_REGISTRY=https://registry.npmjs.org bun install) |
| 95 | + |
| 96 | + info "Building opencode binary (this takes about a minute)..." |
| 97 | + BUN_CONFIG_REGISTRY=https://registry.npmjs.org \ |
| 98 | + bun run --cwd "$CLONE_DIR/packages/opencode" build --single --skip-embed-web-ui |
| 99 | + |
| 100 | + success "Binary built" |
| 101 | +} |
| 102 | + |
| 103 | +# ── step 4: AWS SSO config ──────────────────────────────────────────────────── |
| 104 | +write_aws_config() { |
| 105 | + mkdir -p "$HOME/.aws" |
| 106 | + local config="$HOME/.aws/config" |
| 107 | + |
| 108 | + if grep -q "\[profile $AWS_PROFILE\]" "$config" 2>/dev/null; then |
| 109 | + warn "AWS profile [$AWS_PROFILE] already in $config — skipping" |
| 110 | + return |
| 111 | + fi |
| 112 | + |
| 113 | + info "Writing AWS SSO profile to $config..." |
| 114 | + cat >>"$config" <<EOF |
| 115 | +
|
| 116 | +[profile $AWS_PROFILE] |
| 117 | +sso_start_url = $SSO_START_URL |
| 118 | +sso_region = $SSO_REGION |
| 119 | +sso_account_id = $ACCOUNT_ID |
| 120 | +sso_role_name = $SSO_ROLE_NAME |
| 121 | +region = $AWS_REGION |
| 122 | +EOF |
| 123 | + success "AWS profile [$AWS_PROFILE] written" |
| 124 | +} |
| 125 | + |
| 126 | +# ── step 5: opencode provider config ───────────────────────────────────────── |
| 127 | +write_opencode_config() { |
| 128 | + local config_dir="$HOME/.config/opencode" |
| 129 | + local config_file="$config_dir/opencode.json" |
| 130 | + mkdir -p "$config_dir" |
| 131 | + |
| 132 | + if [ -f "$config_file" ]; then |
| 133 | + warn "opencode config already exists at $config_file — skipping" |
| 134 | + return |
| 135 | + fi |
| 136 | + |
| 137 | + info "Writing opencode config to $config_file..." |
| 138 | + cat >"$config_file" <<EOF |
| 139 | +{ |
| 140 | + "\$schema": "https://opencode.ai/config.json", |
| 141 | + "model": "amazon-bedrock/us.anthropic.claude-sonnet-4-6", |
| 142 | + "enabled_providers": ["amazon-bedrock"], |
| 143 | + "plugin": [], |
| 144 | + "provider": { |
| 145 | + "amazon-bedrock": { |
| 146 | + "options": { |
| 147 | + "region": "$AWS_REGION" |
| 148 | + }, |
| 149 | + "models": { |
| 150 | + "writer.palmyra-x4-v1:0": { |
| 151 | + "name": "Writer Palmyra X4", |
| 152 | + "tool_call": false, |
| 153 | + "limit": { "context": 128000, "output": 8192 } |
| 154 | + }, |
| 155 | + "writer.palmyra-x5-v1:0": { |
| 156 | + "name": "Writer Palmyra X5", |
| 157 | + "tool_call": false, |
| 158 | + "limit": { "context": 1000000, "output": 8192 } |
| 159 | + }, |
| 160 | + "deepseek.r1-v1:0": { |
| 161 | + "name": "DeepSeek R1 (A)", |
| 162 | + "tool_call": false, |
| 163 | + "reasoning": false, |
| 164 | + "limit": { "context": 64000, "output": 32768 } |
| 165 | + }, |
| 166 | + "mistral.pixtral-large-2502-v1:0": { |
| 167 | + "name": "Mistral Pixtral Large", |
| 168 | + "tool_call": false, |
| 169 | + "limit": { "context": 128000, "output": 8192 } |
| 170 | + }, |
| 171 | + "meta.llama4-maverick-17b-instruct-v1:0": { |
| 172 | + "name": "Meta Llama 4 Maverick 17B", |
| 173 | + "tool_call": false, |
| 174 | + "limit": { "context": 1000000, "output": 8192 } |
| 175 | + }, |
| 176 | + "meta.llama4-scout-17b-instruct-v1:0": { |
| 177 | + "name": "Meta Llama 4 Scout 17B", |
| 178 | + "tool_call": false, |
| 179 | + "limit": { "context": 10000000, "output": 8192 } |
| 180 | + }, |
| 181 | + "amazon.nova-2-lite-v1:0": { |
| 182 | + "name": "Amazon Nova 2 Lite", |
| 183 | + "limit": { "context": 300000, "output": 5120 } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | +EOF |
| 190 | + success "opencode config written" |
| 191 | +} |
| 192 | + |
| 193 | +# ── step 6: shell launcher function ────────────────────────────────────────── |
| 194 | +write_shell_alias() { |
| 195 | + local rc_file |
| 196 | + case "$(basename "${SHELL:-bash}")" in |
| 197 | + zsh) rc_file="$HOME/.zshrc" ;; |
| 198 | + *) rc_file="$HOME/.bashrc" ;; |
| 199 | + esac |
| 200 | + |
| 201 | + if grep -q "opencode-work()" "$rc_file" 2>/dev/null; then |
| 202 | + warn "opencode-work() already defined in $rc_file — skipping" |
| 203 | + return |
| 204 | + fi |
| 205 | + |
| 206 | + info "Appending opencode-work() to $rc_file..." |
| 207 | + |
| 208 | + # Single-quoted heredoc prevents variable expansion inside the function body. |
| 209 | + # CLONE_DIR_PLACEHOLDER is substituted after writing via perl. |
| 210 | + cat >>"$rc_file" <<'SHELL_EOF' |
| 211 | +
|
| 212 | +# ── Flexion opencode launcher ───────────────────────────────────────────────── |
| 213 | +opencode-work() { |
| 214 | + local profile="ClaudeCodeAccess" |
| 215 | + local arch os |
| 216 | + case "$(uname -m)" in arm64|aarch64) arch="arm64" ;; *) arch="x64" ;; esac |
| 217 | + case "$(uname -s)" in Darwin) os="darwin" ;; Linux) os="linux" ;; *) |
| 218 | + echo "Unsupported OS: $(uname -s)" && return 1 ;; esac |
| 219 | + echo "Logging in to AWS SSO ($profile)..." |
| 220 | + aws sso login --profile "$profile" || return 1 |
| 221 | + eval "$(aws configure export-credentials --profile "$profile" --format env)" |
| 222 | + "CLONE_DIR_PLACEHOLDER/packages/opencode/dist/opencode-${os}-${arch}/bin/opencode" "$@" |
| 223 | +} |
| 224 | +# ───────────────────────────────────────────────────────────────────────────── |
| 225 | +SHELL_EOF |
| 226 | + |
| 227 | + # Substitute actual clone path. Uses | as delimiter to safely handle / in paths. |
| 228 | + perl -i -pe "s|CLONE_DIR_PLACEHOLDER|${CLONE_DIR}|g" "$rc_file" |
| 229 | + |
| 230 | + success "opencode-work() added to $rc_file" |
| 231 | +} |
| 232 | + |
| 233 | +# ── main ────────────────────────────────────────────────────────────────────── |
| 234 | +main() { |
| 235 | + check_prereqs |
| 236 | + gather_inputs |
| 237 | + clone_and_build |
| 238 | + write_aws_config |
| 239 | + write_opencode_config |
| 240 | + write_shell_alias |
| 241 | + |
| 242 | + local rc_file |
| 243 | + case "$(basename "${SHELL:-bash}")" in |
| 244 | + zsh) rc_file="~/.zshrc" ;; |
| 245 | + *) rc_file="~/.bashrc" ;; |
| 246 | + esac |
| 247 | + |
| 248 | + printf '\n%s%sInstallation complete!%s\n' "$GREEN" "$BOLD" "$NC" |
| 249 | + printf '────────────────────────────────────────────\n' |
| 250 | + printf ' Binary: %s/packages/opencode/dist/opencode-*/bin/opencode\n' "$CLONE_DIR" |
| 251 | + printf ' AWS: profile=%s account=%s region=%s\n' "$AWS_PROFILE" "$ACCOUNT_ID" "$AWS_REGION" |
| 252 | + printf ' Config: %s/.config/opencode/opencode.json\n' "$HOME" |
| 253 | + printf '\n Next steps:\n' |
| 254 | + printf ' 1. source %s\n' "$rc_file" |
| 255 | + printf ' 2. opencode-work\n\n' |
| 256 | +} |
| 257 | + |
| 258 | +main |
0 commit comments