feat(rtk): add an rtk plugin that routes Bash calls through rtk #284
No files matched your search
@@ -1,7 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
# PreToolUse hook: route a Bash tool call through `rtk hook claude`, which may
|
||||
# rewrite the command to its rtk-prefixed form. rtk's output is passed through
|
||||
# unchanged. Without rtk on PATH it does nothing. Limits: the rtk plugin's README.
|
||||
# when rtk succeeds. The hook always exits 0: a PreToolUse exit 2 blocks the tool
|
||||
# call, and rtk exits 2 on a usage error (e.g. an rtk without `hook claude`), so
|
||||
# any rtk failure is swallowed and the call proceeds unchanged. Without rtk on
|
||||
# PATH it does nothing. Limits: the rtk plugin's README.
|
||||
set -euo pipefail
|
||||
|
||||
# apm could deploy this file to other runtimes, where it must do nothing.
|
||||
@@ -13,4 +16,7 @@ esac
|
||||
|
||||
command -v rtk > /dev/null 2>&1 || exit 0
|
||||
|
||||
exec rtk hook claude
|
||||
# stdin (the hook payload) is inherited by rtk inside the substitution.
|
||||
out="$(rtk hook claude)" || exit 0
|
||||
[[ -z "$out" ]] || printf '%s\n' "$out"
|
||||
exit 0
|
||||
@@ -62,6 +62,29 @@ if [[ $RC -eq 0 && "$OUT" == stub-output ]]; then pass; else fail "want rc 0 and
|
||||
if [[ "$(cat "$WORK/stub-args" 2> /dev/null)" == "hook claude" ]]; then pass; else fail "stub args: $(cat "$WORK/stub-args" 2> /dev/null)"; fi
|
||||
if [[ "$(cat "$WORK/stub-stdin" 2> /dev/null)" == "$(payload "git status")" ]]; then pass; else fail "stub stdin: $(cat "$WORK/stub-stdin" 2> /dev/null)"; fi
|
||||
|
||||
# stub_rtk <name> <body>: writes a stub rtk with <body> to $WORK/<name>/rtk.
|
||||
stub_rtk() {
|
||||
mkdir -p "$WORK/$1"
|
||||
printf '#!/usr/bin/env bash\ncat > /dev/null\n%s\n' "$2" > "$WORK/$1/rtk"
|
||||
chmod +x "$WORK/$1/rtk"
|
||||
}
|
||||
stub_rtk fail2-bin "echo 'error: unrecognized subcommand' >&2; exit 2"
|
||||
stub_rtk fail1-bin "exit 1"
|
||||
stub_rtk silent-bin "exit 0"
|
||||
|
||||
echo "== rtk exits 2 (usage error): exit 0, no stdout, never blocks"
|
||||
run "$HOOK" "$WORK/fail2-bin:$WORK/base-bin" "git status"
|
||||
if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "rtk exit 2: want exit 0 and no stdout, got $RC: $OUT"; fi
|
||||
|
||||
echo "== rtk exits 1: exit 0, no stdout"
|
||||
run "$HOOK" "$WORK/fail1-bin:$WORK/base-bin" "git status"
|
||||
if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "rtk exit 1: want exit 0 and no stdout, got $RC: $OUT"; fi
|
||||
|
||||
echo "== rtk prints nothing: exit 0, no stdout at all (not even a newline)"
|
||||
RC=0
|
||||
payload "git status" | PATH="$WORK/silent-bin:$WORK/base-bin" "$TIMEOUT" 10 "$HOOK" > "$WORK/raw-out" 2> "$WORK/stderr" || RC=$?
|
||||
if [[ $RC -eq 0 && ! -s "$WORK/raw-out" ]]; then pass; else fail "silent rtk: want exit 0 and zero-byte stdout, got $RC: $(od -c "$WORK/raw-out")"; fi
|
||||
|
||||
echo "== rtk absent: exit 0, no output"
|
||||
run "$HOOK" "$WORK/base-bin" "git status"
|
||||
if [[ $RC -eq 0 && -z "$OUT" && ! -s "$WORK/stderr" ]]; then pass; else fail "want silent exit 0, got $RC: $OUT $(cat "$WORK/stderr")"; fi
|
||||
@@ -78,6 +101,8 @@ else
|
||||
REAL_PATH="$(dirname "$REAL_RTK"):$WORK/base-bin"
|
||||
run "$HOOK" "$REAL_PATH" "git status"
|
||||
if [[ $RC -eq 0 && "$OUT" == *'"command":"rtk git status"'* ]]; then pass; else fail "git status: want rewrite, got $RC: $OUT"; fi
|
||||
# Depends on rtk's built-in rules as of 0.42.4: `echo` has no rewrite rule,
|
||||
# so rtk passes it through with no output. A later rtk may add one.
|
||||
run "$HOOK" "$REAL_PATH" "echo hello"
|
||||
if [[ $RC -eq 0 && -z "$OUT" ]]; then pass; else fail "echo hello: want no output, got $RC: $OUT"; fi
|
||||
fi
|
||||
|
||||
Reference in new issue
Block a user