-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode.sh
More file actions
executable file
·103 lines (85 loc) · 2.84 KB
/
opencode.sh
File metadata and controls
executable file
·103 lines (85 loc) · 2.84 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
#!/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/'
}
update_version_file() {
local version="$1"
echo "${version}" > "${VERSION_FILE}"
}
update_opencode() {
local latest
latest=$(get_latest_version)
if [[ -z "${latest}" ]]; then
echo "✗ Could not fetch latest version from GitHub" >&2
exit 1
fi
OPENCODE_VERSION="${latest}"
echo "→ Updating OpenCode to version ${latest}..."
build_and_run
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 (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
stored=$(get_stored_version)
if [[ -n "${stored}" ]]; then
OPENCODE_VERSION="${stored}"
run_container
else
echo "→ First run: no stored version found, fetching latest..."
OPENCODE_VERSION=$(get_latest_version)
build_and_run
update_version_file "${OPENCODE_VERSION}"
fi
;;
*)
echo "Usage: $0 [update]" >&2
exit 1
;;
esac
}
main "$@"