Skip to content

Commit

Permalink
feat: prompt confirmation for fs write operations outside cwd (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Aug 2, 2024
1 parent f5c67b3 commit d66d893
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 28 deletions.
32 changes: 22 additions & 10 deletions agents/coder/tools.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#!/usr/bin/env bash
set -e

# @env FS_BASE_DIR=. The base dir

# @cmd Create a new file at the specified path with content.
# @option --path! The path where the file should be created
# @option --content! The content of the file
fs_create() {
path="$FS_BASE_DIR/$argc_path"
printf "%s" "$argc_content" > "$path"
echo "File created: $path" >> "$LLM_OUTPUT"
_guard_path "$argc_path" Create
printf "%s" "$argc_content" > "$argc_path"
echo "File created: $argc_path" >> "$LLM_OUTPUT"
}

# @cmd Apply changes to a file. Use this when you need to edit an existing file.
Expand All @@ -20,10 +18,10 @@ fs_create() {
# @option --content! The new content to apply to the file
# @meta require-tools git
fs_edit() {
path="$FS_BASE_DIR/$argc_path"
if [[ -f "$path" ]]; then
if [[ -f "$argc_path" ]]; then
_guard_path "$argc_path" Edit
changed=0
printf "%s" "$argc_content" | git diff --no-index "$path" - || {
printf "%s" "$argc_content" | git diff --no-index "$argc_path" - || {
changed=1
}
if [[ "$changed" -eq 0 ]]; then
Expand All @@ -37,11 +35,25 @@ fs_edit() {
exit 1
fi
fi
printf "%s" "$argc_content" > "$path"
printf "%s" "$argc_content" > "$argc_path"
echo "Applied changes" >> "$LLM_OUTPUT"
fi
else
echo "Not found file: $path" >> "$LLM_OUTPUT"
echo "Not found file: $argc_path" >> "$LLM_OUTPUT"
fi
}

_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
}

Expand Down
4 changes: 1 addition & 3 deletions tools/fs_cat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ set -e
# @describe Read the contents of a file at the specified path.
# Use this when you need to examine the contents of an existing file.

# @env FS_BASE_DIR=. The base dir
# @option --path! The path of the file to read

main() {
path="$FS_BASE_DIR/$argc_path"
cat "$path" >> "$LLM_OUTPUT"
cat "$argc_path" >> "$LLM_OUTPUT"
}

eval "$(argc --argc-eval "$0" "$@")"
4 changes: 1 addition & 3 deletions tools/fs_ls.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ set -e

# @describe List all files and directories at the specified path.

# @env FS_BASE_DIR=. The base dir
# @option --path! The path of the directory to list

main() {
path="$FS_BASE_DIR/$argc_path"
ls -1 "$path" >> "$LLM_OUTPUT"
ls -1 "$argc_path" >> "$LLM_OUTPUT"
}

eval "$(argc --argc-eval "$0" "$@")"
6 changes: 2 additions & 4 deletions tools/fs_mkdir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ set -e

# @describe Create a new directory at the specified path.

# @env FS_BASE_DIR=. The base dir
# @option --path! The path of the directory to create

main() {
path="$FS_BASE_DIR/$argc_path"
mkdir -p "$path"
echo "Directory created: $path" >> "$LLM_OUTPUT"
mkdir -p "$argc_path"
echo "Directory created: $argc_path" >> "$LLM_OUTPUT"
}

eval "$(argc --argc-eval "$0" "$@")"
22 changes: 19 additions & 3 deletions tools/fs_rm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,25 @@ set -e
# @option --path! The path of the file or directory to remove

main() {
path="$FS_BASE_DIR/$argc_path"
rm -rf "$path"
echo "Path removed: $path" >> "$LLM_OUTPUT"
if [[ -f "$argc_path" ]]; then
_guard_path "$argc_path" Remove
rm -rf "$argc_path"
fi
echo "Path removed: $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" "$@")"
23 changes: 18 additions & 5 deletions tools/fs_write.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" "$@")"

0 comments on commit d66d893

Please sign in to comment.