diff options
| author | Leonard Kugis <leonard@kug.is> | 2026-02-03 16:54:42 +0100 |
|---|---|---|
| committer | Leonard Kugis <leonard@kug.is> | 2026-02-03 16:54:42 +0100 |
| commit | 27e319f8d1e3a0e8f2db79f62d451768b11d62a3 (patch) | |
| tree | 501423803651a8442b81ecb52177dc524880ccfa /tools/fs_write.sh | |
| parent | 808af93158fce5c5197a646a9148191840800621 (diff) | |
| download | llm-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_write.sh')
| -rwxr-xr-x | tools/fs_write.sh | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/tools/fs_write.sh b/tools/fs_write.sh index e418835..746c24b 100755 --- a/tools/fs_write.sh +++ b/tools/fs_write.sh @@ -1,25 +1,56 @@ #!/usr/bin/env bash -set -e +set -uo pipefail # @describe Write the full file contents to a file at the specified path. - # @option --path! The path of the file to write to # @option --contents! The full contents to write to the file - # @env LLM_OUTPUT=/dev/stdout The output path ROOT_DIR="${LLM_ROOT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" +OUT="${LLM_OUTPUT:-/dev/stdout}" + +err() { + echo "ERROR: $*" >> "$OUT" + exit 0 +} main() { - if [[ -f "$argc_path" ]]; then - printf "%s" "$argc_contents" | git diff --no-index "$argc_path" - || true - "$ROOT_DIR/utils/guard_operation.sh" "Apply changes?" - else - "$ROOT_DIR/utils/guard_path.sh" "$argc_path" "Write '$argc_path'?" - mkdir -p "$(dirname "$argc_path")" + path="${argc_path:-}" + contents="${argc_contents:-}" + + [[ -z "$path" ]] && err "missing --path" + # contents darf leer sein – das ist erlaubt (File leeren) + dir="$(dirname "$path")" + + if [[ -e "$path" && ! -f "$path" ]]; then + err "target exists but is not a regular file: $path" + fi + + if [[ -f "$path" ]]; then + # Diff in Output schreiben (damit LLM es sieht) + printf "%s" "$contents" | git diff --no-index "$path" - >> "$OUT" 2>/dev/null || true + if ! "$ROOT_DIR/utils/guard_operation.sh" "Apply changes?" >>"$OUT" 2>&1; then + err "operation cancelled by user" + fi + else + if ! "$ROOT_DIR/utils/guard_path.sh" "$path" "Write '$path'?" >>"$OUT" 2>&1; then + err "operation cancelled by user" + fi + if ! mkdir -p "$dir" 2>/tmp/fs_write_mkdir.err; then + msg="$(cat /tmp/fs_write_mkdir.err 2>/dev/null || true)" + err "failed to create parent dir $dir: ${msg:-unknown error}" fi - printf "%s" "$argc_contents" > "$argc_path" - echo "The contents written to: $argc_path" >> "$LLM_OUTPUT" + fi + + if ! printf "%s" "$contents" > "$path" 2>/tmp/fs_write.err; then + msg="$(cat /tmp/fs_write.err 2>/dev/null || true)" + err "failed to write file $path: ${msg:-unknown error}" + fi + + echo "The contents written to: $path" >> "$OUT" + exit 0 } eval "$(argc --argc-eval "$0" "$@")" +main + |
