213 lines
4.5 KiB
Bash
213 lines
4.5 KiB
Bash
declare -A context
|
|
declare -A outputs
|
|
declare -a output_names
|
|
|
|
matched_any=false
|
|
|
|
echo "=== CONTEXT ==="
|
|
printf '%s\n' "${CONTEXT:-}"
|
|
|
|
echo
|
|
echo "=== RULES ==="
|
|
printf '%s\n' "${RULES:-}"
|
|
|
|
trim() {
|
|
local var="$*"
|
|
var="${var#"${var%%[![:space:]]*}"}"
|
|
var="${var%"${var##*[![:space:]]}"}"
|
|
printf '%s' "$var"
|
|
}
|
|
|
|
load_context() {
|
|
local key
|
|
local value
|
|
|
|
while IFS='=' read -r key value; do
|
|
key="$(trim "${key:-}")"
|
|
value="$(trim "${value:-}")"
|
|
|
|
[[ -z "$key" ]] && continue
|
|
[[ "$key" =~ ^# ]] && continue
|
|
|
|
context["$key"]="$value"
|
|
done <<< "${CONTEXT:-}"
|
|
}
|
|
|
|
evaluate_condition() {
|
|
local condition="$1"
|
|
|
|
local variable
|
|
local operator
|
|
local expected
|
|
local actual
|
|
|
|
read -r variable operator expected <<< "$condition"
|
|
|
|
if [[ -z "${variable:-}" || -z "${operator:-}" || -z "${expected:-}" ]]; then
|
|
echo "Invalid condition: $condition"
|
|
return 1
|
|
fi
|
|
|
|
if [[ ! -v context["$variable"] ]]; then
|
|
echo "Unknown context variable '$variable'"
|
|
return 1
|
|
fi
|
|
|
|
actual="${context[$variable]}"
|
|
|
|
echo "Evaluating: '$variable' actual='$actual' operator='$operator' expected='$expected'"
|
|
|
|
case "$operator" in
|
|
"==")
|
|
[[ "$actual" == "$expected" ]]
|
|
;;
|
|
|
|
"!=")
|
|
[[ "$actual" != "$expected" ]]
|
|
;;
|
|
|
|
"contains")
|
|
[[ "$actual" == *"$expected"* ]]
|
|
;;
|
|
|
|
"startsWith")
|
|
[[ "$actual" == "$expected"* ]]
|
|
;;
|
|
|
|
"endsWith")
|
|
[[ "$actual" == *"$expected" ]]
|
|
;;
|
|
|
|
">"|">="|"<"|"<=")
|
|
if [[ ! "$actual" =~ ^-?[0-9]+$ ]]; then
|
|
echo "Numeric operator '$operator' requires integer value. Actual: '$actual'"
|
|
return 1
|
|
fi
|
|
|
|
if [[ ! "$expected" =~ ^-?[0-9]+$ ]]; then
|
|
echo "Numeric operator '$operator' requires integer value. Expected: '$expected'"
|
|
return 1
|
|
fi
|
|
|
|
case "$operator" in
|
|
">")
|
|
(( actual > expected ))
|
|
;;
|
|
">=")
|
|
(( actual >= expected ))
|
|
;;
|
|
"<")
|
|
(( actual < expected ))
|
|
;;
|
|
"<=")
|
|
(( actual <= expected ))
|
|
;;
|
|
esac
|
|
;;
|
|
|
|
*)
|
|
echo "Unsupported operator: $operator"
|
|
return 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
evaluate_conditions() {
|
|
local conditions="$1"
|
|
local condition
|
|
|
|
while IFS= read -r condition; do
|
|
condition="$(trim "${condition:-}")"
|
|
|
|
[[ -z "$condition" ]] && continue
|
|
[[ "$condition" =~ ^# ]] && continue
|
|
|
|
if evaluate_condition "$condition"; then
|
|
return 0
|
|
fi
|
|
done <<< "$conditions"
|
|
|
|
return 1
|
|
}
|
|
|
|
store_output() {
|
|
local output="$1"
|
|
local name
|
|
local value
|
|
|
|
output="$(trim "${output:-}")"
|
|
|
|
[[ -z "$output" ]] && return 0
|
|
[[ "$output" =~ ^# ]] && return 0
|
|
|
|
if [[ ! "$output" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
|
|
echo "Invalid variable definition: $output"
|
|
return 1
|
|
fi
|
|
|
|
name="${output%%=*}"
|
|
value="${output#*=}"
|
|
|
|
if [[ ! -v outputs["$name"] ]]; then
|
|
output_names+=("$name")
|
|
fi
|
|
|
|
outputs["$name"]="$value"
|
|
|
|
echo "Set output candidate: $name=$value"
|
|
}
|
|
|
|
apply_variables() {
|
|
local variables="$1"
|
|
local output
|
|
|
|
while IFS= read -r output; do
|
|
store_output "$output"
|
|
done <<< "$variables"
|
|
}
|
|
|
|
finalize_rule() {
|
|
local conditions="$1"
|
|
local variables="$2"
|
|
|
|
conditions="$(trim "$conditions")"
|
|
variables="$(trim "$variables")"
|
|
|
|
[[ -z "$conditions" && -z "$variables" ]] && return 0
|
|
|
|
echo
|
|
echo "=== PARSED RULE ==="
|
|
echo "--- conditions ---"
|
|
printf '%s\n' "$conditions"
|
|
echo "--- set ---"
|
|
printf '%s\n' "$variables"
|
|
|
|
if [[ -z "$conditions" ]]; then
|
|
echo "Rule has no conditions, skipping"
|
|
return 0
|
|
fi
|
|
|
|
if [[ -z "$variables" ]]; then
|
|
echo "Rule has no variables to set, skipping"
|
|
return 0
|
|
fi
|
|
|
|
if evaluate_conditions "$conditions"; then
|
|
echo "Rule matched"
|
|
matched_any=true
|
|
apply_variables "$variables"
|
|
else
|
|
echo "Rule did not match"
|
|
fi
|
|
}
|
|
|
|
parse_rules() {
|
|
local line
|
|
local current_conditions=""
|
|
local current_variables=""
|
|
local mode=""
|
|
|
|
while IFS= read -r line; do
|
|
line="$(trim "${line:-}")"
|
|
|
|
[[ -z "$line" ]] && continue |