-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode-bwrap
More file actions
executable file
·144 lines (134 loc) · 4.68 KB
/
Copy pathopencode-bwrap
File metadata and controls
executable file
·144 lines (134 loc) · 4.68 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
#!/usr/bin/env bash
set -euo pipefail
OPENCODE_BIN="${OPENCODE_BIN:-$(command -v opencode || true)}"
if [ -z "$OPENCODE_BIN" ]; then
echo "opencode-bwrap: opencode not found in PATH (set OPENCODE_BIN to point at the binary)" >&2
exit 1
fi
if [ ! -x "$OPENCODE_BIN" ]; then
echo "opencode-bwrap: '$OPENCODE_BIN' is not executable" >&2
exit 1
fi
OPENCODE_REAL="$(readlink -f "$OPENCODE_BIN")"
OPENCODE_DIR="$(dirname "$OPENCODE_REAL")"
OP_CONFIG="$HOME/.config/opencode"
OP_DATA="$HOME/.local/share/opencode"
OP_CACHE="$HOME/.cache/opencode"
OP_STATE="$HOME/.local/state/opencode"
mkdir -p "$OP_CONFIG" "$OP_DATA" "$OP_CACHE" "$OP_STATE"
BWRAP_CONFIG="$OP_CONFIG/opencode.bwrap.json"
BWRAP_OVERLAY=()
if [ -f "$BWRAP_CONFIG" ]; then
BWRAP_OVERLAY=(--ro-bind "$BWRAP_CONFIG" "$HOME/.config/opencode/opencode.json")
fi
PROJECT_DIR="$(pwd)"
if [ ! -d "$PROJECT_DIR" ]; then
echo "opencode-bwrap: current directory '$PROJECT_DIR' is not accessible" >&2
exit 1
fi
EXTRA_ENV=()
BWRAP_BINDS=()
WRAPPER_PATH=()
WRAPPER_CONF="$OP_CONFIG/opencode-bwrap.conf"
SANDBOX_PATH="/usr/bin:/bin:$OPENCODE_DIR"
if [ -r "$WRAPPER_CONF" ]; then
while IFS= read -r line; do
case "$line" in
''|'#'*) continue ;;
esac
read -r directive arg rest <<< "$line"
case "$directive" in
bind-ro|bind-rw)
src="$arg"
dest="${rest:-$src}"
if [ -z "$src" ] || [ ! -d "$src" ]; then
echo "opencode-bwrap: $WRAPPER_CONF: $directive '$arg' is not a directory" >&2
exit 1
fi
if [ "$directive" = 'bind-ro' ]; then
BWRAP_BINDS+=(--ro-bind "$src" "$dest")
else
BWRAP_BINDS+=(--bind "$src" "$dest")
fi
;;
path)
if [ -z "$arg" ]; then
echo "opencode-bwrap: $WRAPPER_CONF: path requires an argument" >&2
exit 1
fi
WRAPPER_PATH+=("$arg")
;;
env)
name="${arg%%=*}"
value="${arg#*=}"
if [ -z "$name" ] || [ "$arg" = "$value" ]; then
echo "opencode-bwrap: $WRAPPER_CONF: env requires NAME=VALUE" >&2
exit 1
fi
EXTRA_ENV+=(--setenv "$name" "$value")
;;
*)
echo "opencode-bwrap: $WRAPPER_CONF: unknown directive '$directive'" >&2
exit 1
;;
esac
done < "$WRAPPER_CONF"
for p in "${WRAPPER_PATH[@]}"; do
SANDBOX_PATH="$SANDBOX_PATH:$p"
done
fi
for v in HTTP_PROXY HTTPS_PROXY ALL_PROXY NO_PROXY http_proxy https_proxy all_proxy no_proxy; do
[ -n "${!v:-}" ] && EXTRA_ENV+=(--setenv "$v" "${!v}")
done
for v in ${OPENCODE_BWRAP_PASSTHROUGH:-}; do
[ -n "${!v:-}" ] && EXTRA_ENV+=(--setenv "$v" "${!v}")
done
[ -n "${NO_COLOR:-}" ] && EXTRA_ENV+=(--setenv NO_COLOR "$NO_COLOR")
exec bwrap \
--unshare-all \
--share-net \
--die-with-parent \
--new-session \
--proc /proc \
--dev /dev \
--tmpfs /tmp \
--tmpfs /run \
--tmpfs /dev/shm \
--ro-bind-try /run/systemd/resolve /run/systemd/resolve \
--ro-bind /usr /usr \
--symlink usr/bin /bin \
--symlink usr/sbin /sbin \
--ro-bind-try /etc/hosts /etc/hosts \
--ro-bind-try /etc/resolv.conf /etc/resolv.conf \
--ro-bind-try /etc/nsswitch.conf /etc/nsswitch.conf \
--ro-bind-try /etc/passwd /etc/passwd \
--ro-bind-try /etc/group /etc/group \
--ro-bind-try /etc/ssl /etc/ssl \
--ro-bind-try /etc/localtime /etc/localtime \
--ro-bind-try /etc/timezone /etc/timezone \
--ro-bind-try /etc/ld.so.cache /etc/ld.so.cache \
--ro-bind-try /lib /lib \
--ro-bind-try /lib64 /lib64 \
--ro-bind-try /lib32 /lib32 \
--ro-bind-try "$OPENCODE_DIR" "$OPENCODE_DIR" \
--dir "$HOME" \
--bind "$PROJECT_DIR" "$PROJECT_DIR" \
--bind "$OP_CONFIG" "$HOME/.config/opencode" \
--bind "$OP_DATA" "$HOME/.local/share/opencode" \
--bind "$OP_CACHE" "$HOME/.cache/opencode" \
--bind "$OP_STATE" "$HOME/.local/state/opencode" \
"${BWRAP_OVERLAY[@]}" \
"${BWRAP_BINDS[@]}" \
--clearenv \
--setenv HOME "$HOME" \
--setenv PATH "$SANDBOX_PATH" \
--setenv TMPDIR /tmp \
--setenv XDG_CONFIG_HOME "$HOME/.config" \
--setenv XDG_DATA_HOME "$HOME/.local/share" \
--setenv XDG_CACHE_HOME "$HOME/.cache" \
--setenv XDG_STATE_HOME "$HOME/.local/state" \
--setenv TERM "${TERM:-xterm}" \
--setenv LANG "${LANG:-C.UTF-8}" \
"${EXTRA_ENV[@]}" \
--chdir "$PROJECT_DIR" \
-- "$OPENCODE_REAL" "$@"