diff options
| author | sigoden <sigoden@gmail.com> | 2024-11-16 07:20:55 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-16 07:20:55 +0800 |
| commit | 6d30c22b82a5ac1a5775d8137c3b08a581770273 (patch) | |
| tree | 164bf3358334719b8fcdaa3b3db42ad378709276 /tools | |
| parent | 1b96d4ee4cadf05163d653373423a615723240aa (diff) | |
| download | llm-functions-docker-6d30c22b82a5ac1a5775d8137c3b08a581770273.tar.gz | |
feat(tools): add fs_patch.sh (#124)
Diffstat (limited to 'tools')
| -rwxr-xr-x | tools/fs_patch.sh | 42 | ||||
| -rwxr-xr-x | tools/fs_write.sh | 5 |
2 files changed, 43 insertions, 4 deletions
diff --git a/tools/fs_patch.sh b/tools/fs_patch.sh new file mode 100755 index 0000000..14e46d2 --- /dev/null +++ b/tools/fs_patch.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +set -e + +# @describe Apply a patch to a file at the specified path. +# This can be used to edit the file, without having to rewrite the whole file. + +# @option --path! The path of the file to apply to +# @option --contents! The patch to apply to the file +# +# Here is an example of a patch block that can be applied to modify the file to request the user's name: +# --- a/hello.py +# +++ b/hello.py +# \@@ ... @@ +# def hello(): +# - print("Hello World") +# + name = input("What is your name? ") +# + print(f"Hello {name}") + +# @env LLM_OUTPUT=/dev/stdout The output path + +main() { + if [ ! -f "$argc_path" ]; then + echo "Not found file: $argc_path" + exit 1 + fi + root_dir="${LLM_ROOT_DIR:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" + new_contents="$(awk -f "$root_dir/utils/patch.awk" "$argc_path" <(printf "%s" "$argc_contents"))" + printf "%s" "$new_contents" | git diff --no-index "$argc_path" - || true + if [ -t 1 ]; then + echo + read -r -p "Apply changes? [Y/n] " ans + if [[ "$ans" == "N" || "$ans" == "n" ]]; then + echo "Aborted!" + exit 1 + fi + fi + printf "%s" "$new_contents" > "$argc_path" + + echo "The patch applied to: $argc_path" >> "$LLM_OUTPUT" +} + +eval "$(argc --argc-eval "$0" "$@")" diff --git a/tools/fs_write.sh b/tools/fs_write.sh index bdf8e85..0e0dba0 100755 --- a/tools/fs_write.sh +++ b/tools/fs_write.sh @@ -1,10 +1,7 @@ #!/usr/bin/env bash set -e -# @describe Write the contents to a file at the specified path. -# If the file exists, only the necessary changes will be applied. -# If the file doesn't exist, it will be created. -# Always provide the full intended contents of the file. +# @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 |
