confd: fix scary error message on systems without USB ports

Check if the "usb-ports" key exists in /run/system.json before calling jq,
otherise we get the following error message:

Nov  8 05:11:45 ix bootstrap[3349]: jq: error (at <stdin>:0): Cannot iterate over null (null)

Also, minor whitespace cleanup and bashisms fixes.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2024-11-08 14:30:04 +01:00
parent d692a3d745
commit 9ac77be9eb
+15 -11
View File
@@ -1,19 +1,23 @@
#!/bin/sh
set -e
usb_ports=$(cat /run/system.json | jq -r '.["usb-ports"] | map(.name) | unique | join(" ")')
if jq -e '.["usb-ports"]' /run/system.json > /dev/null; then
usb_ports=$(jq -r '.["usb-ports"] | map(.name) | unique | join(" ")' /run/system.json)
else
usb_ports=""
fi
gen_port()
{
local port="$1"
cat <<EOF
{
"class": "infix-hardware:usb",
"name": "$port",
"state": {
"admin-state": "unlocked"
}
}
port="$1"
cat <<EOF
{
"class": "infix-hardware:usb",
"name": "$port",
"state": {
"admin-state": "unlocked"
}
}
EOF
}
first=1
@@ -24,7 +28,7 @@ cat <<EOF
EOF
for port in $usb_ports; do
if [ $first -eq 0 ]; then
echo -n ','
echo -n ','
fi
first=0;
gen_port "$port"