Skip to content

Commit 498fd60

Browse files
committed
deploy: require jq for shell JSON parsing
1 parent ebcbf49 commit 498fd60

4 files changed

Lines changed: 34 additions & 80 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Baudbot is designed as shared engineering infrastructure, not a single-user desk
5252
| **CPU** | 2 vCPU | 4 vCPU |
5353
| **Disk** | 20 GB | 40 GB+ (repos, dependencies, Docker images) |
5454

55+
System package dependencies (installed by `baudbot install`): `git`, `curl`, `tmux`, `iptables`, `docker`, `gh`, `jq`, `sudo`.
56+
5557
## Quick Start
5658

5759
```bash

bin/doctor.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ else
7171
fail "varlock not found"
7272
fi
7373

74+
if command -v jq &>/dev/null; then
75+
pass "jq is installed"
76+
else
77+
fail "jq not found (required for shell JSON parsing)"
78+
fi
79+
7480
if command -v docker &>/dev/null; then
7581
pass "docker is available"
7682
else

bin/lib/json-common.sh

Lines changed: 23 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,48 @@
11
#!/bin/bash
22
# Shared JSON parsing helpers for shell scripts.
33
#
4+
# jq is a required runtime dependency.
5+
#
46
# Return codes:
57
# 0 => key found and value printed
68
# 1 => JSON parsed, but key missing/non-string
7-
# 2 => JSON/file/tool error
9+
# 2 => JSON/file/tool error (including missing jq)
810

911
_json_filter='if (type == "object") and has($k) and (.[$k] | type == "string") then .[$k] else empty end'
1012

13+
json_require_jq() {
14+
command -v jq >/dev/null 2>&1
15+
}
16+
1117
json_get_string() {
1218
local file="$1"
1319
local key="$2"
1420

1521
[ -n "$file" ] || return 2
1622
[ -n "$key" ] || return 2
1723
[ -r "$file" ] || return 2
18-
19-
if command -v jq >/dev/null 2>&1; then
20-
jq -er --arg k "$key" "$_json_filter" "$file" 2>/dev/null
21-
case "$?" in
22-
0) return 0 ;;
23-
1) return 1 ;;
24-
*) return 2 ;;
25-
esac
26-
fi
27-
28-
if ! command -v python3 >/dev/null 2>&1; then
29-
return 2
30-
fi
31-
32-
python3 - "$file" "$key" <<'PY'
33-
import json
34-
import sys
35-
36-
if len(sys.argv) != 3:
37-
sys.exit(2)
38-
39-
path = sys.argv[1]
40-
key = sys.argv[2]
41-
42-
try:
43-
with open(path, "r", encoding="utf-8") as f:
44-
data = json.load(f)
45-
except Exception:
46-
sys.exit(2)
47-
48-
if not isinstance(data, dict):
49-
sys.exit(1)
50-
51-
value = data.get(key)
52-
if not isinstance(value, str):
53-
sys.exit(1)
54-
55-
sys.stdout.write(value)
56-
PY
24+
json_require_jq || return 2
25+
26+
jq -er --arg k "$key" "$_json_filter" "$file" 2>/dev/null
27+
case "$?" in
28+
0) return 0 ;;
29+
1) return 1 ;;
30+
*) return 2 ;;
31+
esac
5732
}
5833

5934
json_get_string_stdin() {
6035
local key="$1"
6136

6237
[ -n "$key" ] || return 2
63-
64-
if command -v jq >/dev/null 2>&1; then
65-
jq -er --arg k "$key" "$_json_filter" 2>/dev/null
66-
case "$?" in
67-
0) return 0 ;;
68-
1) return 1 ;;
69-
*) return 2 ;;
70-
esac
71-
fi
72-
73-
if ! command -v python3 >/dev/null 2>&1; then
74-
return 2
75-
fi
76-
77-
python3 - "$key" <<'PY'
78-
import json
79-
import sys
80-
81-
if len(sys.argv) != 2:
82-
sys.exit(2)
83-
84-
key = sys.argv[1]
85-
86-
try:
87-
data = json.load(sys.stdin)
88-
except Exception:
89-
sys.exit(2)
90-
91-
if not isinstance(data, dict):
92-
sys.exit(1)
93-
94-
value = data.get(key)
95-
if not isinstance(value, str):
96-
sys.exit(1)
97-
98-
sys.stdout.write(value)
99-
PY
38+
json_require_jq || return 2
39+
40+
jq -er --arg k "$key" "$_json_filter" 2>/dev/null
41+
case "$?" in
42+
0) return 0 ;;
43+
1) return 1 ;;
44+
*) return 2 ;;
45+
esac
10046
}
10147

10248
json_get_string_or_empty() {

install.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ install_prereqs_ubuntu() {
163163

164164
for attempt in $(seq 1 5); do
165165
if DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=120 update -qq \
166-
&& DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=120 install -y -qq git curl tmux iptables docker.io gh sudo 2>&1 | tail -3; then
166+
&& DEBIAN_FRONTEND=noninteractive apt-get -o DPkg::Lock::Timeout=120 install -y -qq git curl tmux iptables docker.io gh jq sudo 2>&1 | tail -3; then
167167
return 0
168168
fi
169169

@@ -179,10 +179,10 @@ install_prereqs_ubuntu() {
179179
}
180180

181181
install_prereqs_arch() {
182-
pacman -Syu --noconfirm --needed git curl tmux iptables docker github-cli sudo 2>&1 | tail -5
182+
pacman -Syu --noconfirm --needed git curl tmux iptables docker github-cli jq sudo 2>&1 | tail -5
183183
}
184184

185-
info "Installing: git, curl, tmux, iptables, docker, gh, sudo"
185+
info "Installing: git, curl, tmux, iptables, docker, gh, jq, sudo"
186186
"install_prereqs_$DISTRO"
187187
info "Prerequisites installed"
188188

0 commit comments

Comments
 (0)