-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode.sh
More file actions
executable file
·194 lines (161 loc) · 5.44 KB
/
Copy pathopencode.sh
File metadata and controls
executable file
·194 lines (161 loc) · 5.44 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/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
}
validate_paths() {
local -a paths=("$@")
local path resolved
for path in "${paths[@]}"; do
resolved="$(realpath "${path}")"
if [[ ! -e "${resolved}" ]]; then
echo "✗ Path does not exist: ${path}" >&2
return 1
fi
done
local -a sorted
mapfile -t sorted < <(printf "%s\n" "${paths[@]}" | sort)
for (( i=1; i<${#sorted[@]}; i++ )); do
if [[ "${sorted[$i]}" == "${sorted[$((i-1))]}" ]]; then
echo "✗ Duplicate path specified: ${sorted[$i]}" >&2
return 1
fi
done
}
run_container() {
echo "→ Starting opencode v${OPENCODE_VERSION}"
mkdir -p "${HOME}/.local/share/opencode/"
local -a volume_args=(
-v "${PROJECT_DIR}:${PROJECT_DIR}"
)
for path in "${EXTRA_MOUNTS[@]}"; do
volume_args+=(-v "${path}:${path}")
done
volume_args+=(
-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
)
docker run -it --rm \
-e DISPLAY=$DISPLAY \
"${volume_args[@]}" \
-w "${PROJECT_DIR}" \
--network host \
--group-add "${DOCKER_GID}" \
opencode-fk \
bash -c "exec bash"
}
main() {
case "${1:-}" in
update)
update_opencode
;;
"")
PROJECT_DIR="$(realpath "$(pwd)")"
EXTRA_MOUNTS=()
check_and_run
;;
*)
PROJECT_DIR="$(realpath "${1}")"
mapfile -t EXTRA_MOUNTS < <(resolve_extra_mounts "${@:2}")
if ! validate_paths "${PROJECT_DIR}" "${EXTRA_MOUNTS[@]}"; then
exit 1
fi
check_and_run
;;
esac
}
check_and_run() {
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
}
resolve_extra_mounts() {
local path resolved
for path in "$@"; do
resolved="$(realpath "${path}")"
echo "${resolved}"
done
}
main "$@"