aboutsummaryrefslogtreecommitdiffstats
path: root/tools/fs_cat.sh
diff options
context:
space:
mode:
authorLeonard Kugis <leonard@kug.is>2026-02-03 16:54:42 +0100
committerLeonard Kugis <leonard@kug.is>2026-02-03 16:54:42 +0100
commit27e319f8d1e3a0e8f2db79f62d451768b11d62a3 (patch)
tree501423803651a8442b81ecb52177dc524880ccfa /tools/fs_cat.sh
parent808af93158fce5c5197a646a9148191840800621 (diff)
downloadllm-functions-docker-27e319f8d1e3a0e8f2db79f62d451768b11d62a3.tar.gz
Refined tool return error codes.
Now they all return with exit 0 and error messages instead. This prevents early query abortion on fatal errors.
Diffstat (limited to 'tools/fs_cat.sh')
-rwxr-xr-xtools/fs_cat.sh36
1 files changed, 32 insertions, 4 deletions
diff --git a/tools/fs_cat.sh b/tools/fs_cat.sh
index 9fcf702..d02ca21 100755
--- a/tools/fs_cat.sh
+++ b/tools/fs_cat.sh
@@ -1,15 +1,43 @@
#!/usr/bin/env bash
-set -e
+set -uo pipefail
# @describe Read the contents of a file at the specified path.
# Use this when you need to examine the contents of an existing file.
-
# @option --path! The path of the file to read
-
# @env LLM_OUTPUT=/dev/stdout The output path
+OUT="${LLM_OUTPUT:-/dev/stdout}"
+MAX_BYTES="${LLM_MAX_BYTES:-200000}"
+
+sanitize() {
+ if command -v iconv >/dev/null 2>&1; then
+ iconv -f UTF-8 -t UTF-8 -c
+ else
+ cat
+ fi
+}
+
+err() {
+ echo "ERROR: $*" >> "$OUT"
+ exit 0
+}
+
main() {
- cat "$argc_path" >> "$LLM_OUTPUT"
+ path="${argc_path:-}"
+ [[ -z "$path" ]] && err "missing --path"
+ [[ ! -e "$path" ]] && err "file not found: $path"
+ [[ ! -f "$path" ]] && err "not a regular file: $path"
+ [[ ! -r "$path" ]] && err "file not readable: $path"
+
+ # Heuristik: Binärdateien nicht in den Chat kippen
+ if LC_ALL=C grep -qP '\x00' "$path" 2>/dev/null; then
+ err "file appears to be binary (NUL bytes detected): $path"
+ fi
+
+ head -c "$MAX_BYTES" "$path" | sanitize >> "$OUT"
+ exit 0
}
eval "$(argc --argc-eval "$0" "$@")"
+main
+