feat: create skill variable helper

This commit is contained in:
2026-05-28 17:16:42 -04:00
parent 544ab6f781
commit cc58168c4e
2 changed files with 84 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
#!/usr/bin/env bash
# Skill-local variable store. Values live in <skill-dir>/.vars/<NAME>.
#
# Usage:
# variable.sh --get NAME [--require-exec RELPATH]
# Prints value to stdout, exits 0.
# --require-exec asserts that VALUE/RELPATH
# exists and is executable; if not, prints
# the fix instructions to stderr and exits 2.
# variable.sh --set NAME VALUE Writes value, exits 0.
#
# Callers should treat any non-zero exit as fatal; the stderr message tells
# the caller (agent or user) exactly how to populate or correct the value.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SKILL_DIR="$(dirname "$SCRIPT_DIR")"
STORE="$SKILL_DIR/.vars"
SELF="$0"
usage() {
cat >&2 <<EOF
Usage:
$SELF --get NAME [--require-exec RELPATH]
$SELF --set NAME VALUE
EOF
exit 2
}
op=""
name=""
value=""
require_exec=""
while [[ $# -gt 0 ]]; do
case "$1" in
--get|--set) op="${1#--}"; shift ;;
--require-exec) [[ $# -ge 2 ]] || usage; require_exec="$2"; shift 2 ;;
--*) usage ;;
*)
if [[ -z "$name" ]]; then name="$1"
elif [[ -z "$value" ]]; then value="$1"
else usage; fi
shift ;;
esac
done
[[ -n "$op" && -n "$name" ]] || usage
case "$op" in
get)
[[ -z "$value" ]] || usage
file="$STORE/$name"
if [[ ! -f "$file" ]]; then
cat >&2 <<EOF
$SELF: $name is not set.
Ask the user for the value, then set it:
$SELF --set $name <value>
EOF
exit 2
fi
val="$(cat "$file")"
if [[ -n "$require_exec" && ! -x "$val/$require_exec" ]]; then
cat >&2 <<EOF
$SELF: $name is set to '$val' but '$val/$require_exec' is missing or not executable.
Update it with a valid path:
$SELF --set $name <new-value>
EOF
exit 2
fi
printf '%s' "$val"
;;
set)
[[ -n "$value" ]] || usage
[[ "$name" =~ ^[A-Z][A-Z0-9_]*$ ]] || {
echo >&2 "$SELF: invalid name '$name' (must match [A-Z][A-Z0-9_]*)"
exit 2
}
mkdir -p "$STORE"
printf '%s' "$value" > "$STORE/$name"
;;
esac