-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode.sh
More file actions
executable file
·142 lines (119 loc) · 4.48 KB
/
opencode.sh
File metadata and controls
executable file
·142 lines (119 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
DOCKER_GID=$(getent group docker | cut -d: -f3 || echo 999)
PROJECT_DIR="$(realpath "$(pwd)")"
VERSION_FILE="${SCRIPT_DIR}/.opencode-version"
OPENCODE_VERSION="${OPENCODE_VERSION:-latest}"
get_stored_version() {
if [[ -f "${VERSION_FILE}" ]]; then
cat "${VERSION_FILE}"
fi
}
get_latest_version() {
curl -fsSL https://api.github.com/repos/anomalyco/opencode/releases/latest 2>/dev/null | \
grep -m1 '"tag_name"' | sed 's/.*"tag_name": *"v\?\([^"]*\)".*/\1/'
}
check_for_updates() {
local timeout="${1:-5}"
local result
result=$(curl -fsSL --max-time "${timeout}" https://api.github.com/repos/anomalyco/opencode/releases/latest 2>/dev/null | \
grep -m1 '"tag_name"' | sed 's/.*"tag_name": *"v\?\([^"]*\)".*/\1/' || true)
echo "${result}"
}
show_update_banner() {
local new_version="$1"
echo ""
echo "╔══════════════════════════════════════════════════════════╗"
echo "║ New OpenCode version available! ║"
echo "║ ${new_version} ║"
echo "║ Press any key to continue... ║"
echo "╚══════════════════════════════════════════════════════════╝"
echo ""
if [[ -t 0 ]]; then
read -n 1 -s
else
sleep 1
fi
}
update_version_file() {
local version="$1"
echo "${version}" > "${VERSION_FILE}"
}
update_opencode() {
local latest
latest=$(check_for_updates)
if [[ -z "${latest}" ]]; then
echo "✗ Could not fetch latest version from GitHub" >&2
exit 1
fi
show_update_banner "${latest}"
OPENCODE_VERSION="${latest}"
echo "→ Updating OpenCode to version ${latest}..."
echo "→ Building opencode container with Docker client support (GID=${DOCKER_GID})"
docker build -t opencode-fk \
--build-arg OPENCODE_VERSION="${OPENCODE_VERSION}" \
--build-arg DOCKER_GID="${DOCKER_GID}" \
-f - "${SCRIPT_DIR}" < "${SCRIPT_DIR}/Dockerfile"
update_version_file "${latest}"
echo "✓ Updated to version ${latest}"
}
build_and_run() {
echo "→ Building opencode container with Docker client support (GID=${DOCKER_GID})"
docker build -t opencode-fk \
--build-arg OPENCODE_VERSION="${OPENCODE_VERSION}" \
--build-arg DOCKER_GID="${DOCKER_GID}" \
-f - "${SCRIPT_DIR}" < "${SCRIPT_DIR}/Dockerfile"
run_container
}
run_container() {
echo "→ Starting opencode v${OPENCODE_VERSION} (same-path mount — inner docker commands now work transparently)"
mkdir -p "${HOME}/.local/share/opencode/"
docker run -it --rm \
-e DISPLAY=$DISPLAY \
-v "${PROJECT_DIR}:${PROJECT_DIR}" \
-v "${PROJECT_DIR}:/workspace" \
-v "${HOME}/.config/opencode/":/home/dev/.config/opencode/ \
-v "${HOME}/.local/share/opencode/":/home/dev/.local/share/opencode/ \
-v "${HOME}/.ssh/config":/home/dev/.ssh/config \
-v "${HOME}/.ssh/sockets":/home/dev/.ssh/sockets \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-w "${PROJECT_DIR}" \
--network host \
--group-add "${DOCKER_GID}" \
opencode-fk \
bash -c "exec bash"
}
main() {
case "${1:-}" in
update)
update_opencode
;;
"")
local stored latest
stored=$(get_stored_version)
latest=$(check_for_updates 1)
if [[ -n "${latest}" && "${latest}" != "${stored}" ]]; then
show_update_banner "${latest}"
fi
if [[ -n "${stored}" ]]; then
OPENCODE_VERSION="${stored}"
run_container
else
if [[ -z "${latest}" ]]; then
echo "→ First run: no stored version found, fetching latest..."
latest=$(get_latest_version)
fi
OPENCODE_VERSION="${latest}"
build_and_run
update_version_file "${latest}"
fi
;;
*)
echo "Usage: $0 [update]" >&2
exit 1
;;
esac
}
main "$@"