From d66d8938f1f845e92966835f70bc2f97ebb8246b Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 2 Aug 2024 23:26:49 +0800 Subject: feat: prompt confirmation for fs write operations outside cwd (#90) --- tools/fs_write.sh | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) (limited to 'tools/fs_write.sh') diff --git a/tools/fs_write.sh b/tools/fs_write.sh index 7718b8d..59ee812 100755 --- a/tools/fs_write.sh +++ b/tools/fs_write.sh @@ -6,15 +6,28 @@ set -e # If the file doesn't exist, it will be created. # Always provide the full intended contents of the file. -# @env FS_BASE_DIR=. The base dir # @option --path! The path of the file to write to # @option --contents! The full contents to write to the file main() { - path="$FS_BASE_DIR/$argc_path" - mkdir -p "$(dirname "$path")" - printf "%s" "$argc_contents" > "$path" - echo "The contents written to: $path" >> "$LLM_OUTPUT" + _guard_path "$argc_path" Write + mkdir -p "$(dirname "$argc_path")" + printf "%s" "$argc_contents" > "$argc_path" + echo "The contents written to: $argc_path" >> "$LLM_OUTPUT" +} + +_guard_path() { + path="$(realpath "$1")" + action="$2" + if [[ ! "$path" == "$(pwd)"* ]]; then + if [ -t 1 ]; then + read -r -p "$action $path? [Y/n] " ans + if [[ "$ans" == "N" || "$ans" == "n" ]]; then + echo "Aborted!" + exit 1 + fi + fi + fi } eval "$(argc --argc-eval "$0" "$@")" -- cgit v1.2.3