mirror of
https://github.com/alexbelgium/hassio-addons.git
synced 2026-05-30 04:14:04 +02:00
Test for bashio
This commit is contained in:
@@ -1,12 +1,16 @@
|
|||||||
#!/command/with-contenv bashio
|
#!/command/with-contenv bashio
|
||||||
# shellcheck shell=bash
|
# shellcheck shell=bash
|
||||||
|
|
||||||
echo "Starting..."
|
set -e # Exit immediately if a command exits with a non-zero status
|
||||||
|
|
||||||
|
|
||||||
# Detect if this is PID1 (main container process) — do this once at the start
|
# Detect if this is PID1 (main container process) — do this once at the start
|
||||||
PID1=false
|
PID1=false
|
||||||
if [ "$$" -eq 1 ]; then
|
if [ "$$" -eq 1 ]; then
|
||||||
PID1=true
|
PID1=true
|
||||||
|
echo "Starting as entrypoint"
|
||||||
|
else
|
||||||
|
echo "Starting custom scripts"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
######################
|
######################
|
||||||
@@ -21,27 +25,33 @@ fi
|
|||||||
candidate_shebangs+=(
|
candidate_shebangs+=(
|
||||||
"/usr/bin/env bashio"
|
"/usr/bin/env bashio"
|
||||||
"/usr/bin/bashio"
|
"/usr/bin/bashio"
|
||||||
"/usr/bin/bash"
|
|
||||||
"/usr/bin/sh"
|
|
||||||
"/bin/bash"
|
|
||||||
"/bin/sh"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Find the first valid shebang interpreter in candidate list
|
# Find the first valid shebang interpreter in candidate list by probing bashio::addon.version
|
||||||
shebang=""
|
shebang=""
|
||||||
|
tmp="$(mktemp)"
|
||||||
|
trap 'rm -f "$tmp"' EXIT
|
||||||
|
|
||||||
for candidate in "${candidate_shebangs[@]}"; do
|
for candidate in "${candidate_shebangs[@]}"; do
|
||||||
command_path="${candidate%% *}"
|
echo "Trying $candidate"
|
||||||
# Test if command exists and can actually execute a shell command (for shells)
|
# Build a tiny probe script that prints the addon version
|
||||||
if [ -x "$command_path" ]; then
|
printf '#!%s\n' "$candidate" >"$tmp"
|
||||||
# Try as both 'sh -c' and 'bashio echo' style
|
cat >>"$tmp" <<'EOF'
|
||||||
if "$command_path" -c 'echo yes' >/dev/null 2>&1 || "$command_path" echo "yes" >/dev/null 2>&1; then
|
out="$(bashio::addon.version 2>/dev/null || true)"
|
||||||
shebang="$candidate"
|
[ -n "$out" ] && printf '%s\n' "$out"
|
||||||
break
|
EOF
|
||||||
fi
|
chmod +x "$tmp"
|
||||||
|
|
||||||
|
# Run the probe and check for at least one digit in the output
|
||||||
|
out="$(exec "$tmp" 2>/dev/null || true)"
|
||||||
|
if printf '%s' "$out" | grep -qE '[0-9]'; then
|
||||||
|
shebang="$candidate"
|
||||||
|
break
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ -z "$shebang" ]; then
|
if [ -z "$shebang" ]; then
|
||||||
echo "ERROR: No valid shebang found!"
|
echo "ERROR: No valid shebang found!" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user