databuild/runtime/execute_wrapper.sh.tpl
2025-04-17 15:53:55 -07:00

49 lines
1.4 KiB
Smarty
Executable file

#!/bin/bash
set -e
EXECUTE_BINARY="%{EXECUTE_PATH}"
JQ="%{JQ_PATH}"
if [[ ! -x "$EXECUTE_BINARY" ]]; then
EXECUTE_BASENAME=$(basename "$EXECUTE_BINARY")
if [[ ! -x "$EXECUTE_BASENAME" ]]; then
echo "Error: Execute binary not found or not executable at $EXECUTE_BINARY and $EXECUTE_BASENAME" >&2
exit 1
else
EXECUTE_BINARY=./$EXECUTE_BASENAME
fi
fi
# Ensure the binaries exist and are executable
if [[ ! -x "$JQ" ]]; then
JQ_PATH="../databuild+/runtime/$(basename $JQ)"
if [[ ! -x "$JQ_PATH" ]]; then
echo "Error: JQ binary not found or not executable at $JQ or $JQ_PATH" >&2
exit 1
else
JQ=$JQ_PATH
fi
fi
# First argument should be the path to a config file
CONFIG_FILE="$1"
# Create a temporary file for stdin if needed
if [[ -z "$CONFIG_FILE" ]] || [[ "$CONFIG_FILE" == "-" ]]; then
TMP_CONFIG=$(mktemp)
cat > "$TMP_CONFIG"
CONFIG_FILE="$TMP_CONFIG"
trap 'rm -f "$TMP_CONFIG"' EXIT
fi
# Extract and set environment variables from the config
eval "$("$JQ" -r '.env | to_entries | .[] | "export " + .key + "=\"" + .value + "\""' "$CONFIG_FILE")"
# Extract arguments from the config
ARGS=()
while IFS= read -r arg; do
ARGS+=("$arg")
done < <("$JQ" -r '.args[]' "$CONFIG_FILE")
# Run the execution with both environment variables (already set) and arguments
exec "$EXECUTE_BINARY" "${ARGS[@]}"