230 lines
6.6 KiB
Bash
Executable File
230 lines
6.6 KiB
Bash
Executable File
#!/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 the patch to
|
|
# @option --contents! The patch to apply to the file
|
|
|
|
# @env LLM_OUTPUT=/dev/stdout The output path
|
|
|
|
PROMPT_UTILS="${LLM_ROOT_DIR:-$(dirname "${BASH_SOURCE[0]}")/..}/utils/prompt-utils.sh"
|
|
# shellcheck disable=SC1090
|
|
source "$PROMPT_UTILS"
|
|
|
|
# shellcheck disable=SC2154
|
|
main() {
|
|
if [[ ! -f "$argc_path" ]]; then
|
|
error "Unable to find the specified file: $argc_path"
|
|
exit 1
|
|
fi
|
|
|
|
new_contents="$(patch_file "$argc_path" <(printf "%s" "$argc_contents"))"
|
|
printf "%s" "$new_contents" | git diff --no-index "$argc_path" - || true
|
|
|
|
guard_operation "Apply changes?"
|
|
|
|
printf "%s" "$new_contents" > "$argc_path"
|
|
|
|
info "Applied the patch to: $argc_path" >> "$LLM_OUTPUT"
|
|
}
|
|
|
|
# ARGC-BUILD {
|
|
# This block was generated by argc (https://github.com/sigoden/argc).
|
|
# Modifying it manually is not recommended
|
|
|
|
_argc_run() {
|
|
if [[ "${1:-}" == "___internal___" ]]; then
|
|
_argc_die "error: unsupported ___internal___ command"
|
|
fi
|
|
if [[ "${OS:-}" == "Windows_NT" ]] && [[ -n "${MSYSTEM:-}" ]]; then
|
|
set -o igncr
|
|
fi
|
|
argc__args=("$(basename "$0" .sh)" "$@")
|
|
argc__positionals=()
|
|
_argc_index=1
|
|
_argc_len="${#argc__args[@]}"
|
|
_argc_tools=()
|
|
_argc_parse
|
|
if [ -n "${argc__fn:-}" ]; then
|
|
$argc__fn "${argc__positionals[@]}"
|
|
fi
|
|
}
|
|
|
|
_argc_usage() {
|
|
cat <<-'EOF'
|
|
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.
|
|
|
|
USAGE: --path <PATH> --contents <CONTENTS>
|
|
|
|
OPTIONS:
|
|
--path <PATH> The path of the file to apply the patch to
|
|
--contents <CONTENTS> The patch to apply to the file
|
|
-h, --help Print help
|
|
-V, --version Print version
|
|
|
|
ENVIRONMENTS:
|
|
LLM_OUTPUT The output path [default: /dev/stdout]
|
|
EOF
|
|
exit
|
|
}
|
|
|
|
_argc_version() {
|
|
echo 0.0.0
|
|
exit
|
|
}
|
|
|
|
_argc_parse() {
|
|
local _argc_key _argc_action
|
|
local _argc_subcmds=""
|
|
while [[ $_argc_index -lt $_argc_len ]]; do
|
|
_argc_item="${argc__args[_argc_index]}"
|
|
_argc_key="${_argc_item%%=*}"
|
|
case "$_argc_key" in
|
|
--help | -help | -h)
|
|
_argc_usage
|
|
;;
|
|
--version | -version | -V)
|
|
_argc_version
|
|
;;
|
|
--)
|
|
_argc_dash="${#argc__positionals[@]}"
|
|
argc__positionals+=("${argc__args[@]:$((_argc_index + 1))}")
|
|
_argc_index=$_argc_len
|
|
break
|
|
;;
|
|
--path)
|
|
_argc_take_args "--path <PATH>" 1 1 "-" ""
|
|
_argc_index=$((_argc_index + _argc_take_args_len + 1))
|
|
if [[ -z "${argc_path:-}" ]]; then
|
|
argc_path="${_argc_take_args_values[0]:-}"
|
|
else
|
|
_argc_die "error: the argument \`--path\` cannot be used multiple times"
|
|
fi
|
|
;;
|
|
--contents)
|
|
_argc_take_args "--contents <CONTENTS>" 1 1 "-" ""
|
|
_argc_index=$((_argc_index + _argc_take_args_len + 1))
|
|
if [[ -z "${argc_contents:-}" ]]; then
|
|
argc_contents="${_argc_take_args_values[0]:-}"
|
|
else
|
|
_argc_die "error: the argument \`--contents\` cannot be used multiple times"
|
|
fi
|
|
;;
|
|
*)
|
|
if _argc_maybe_flag_option "-" "$_argc_item"; then
|
|
_argc_die "error: unexpected argument \`$_argc_key\` found"
|
|
fi
|
|
argc__positionals+=("$_argc_item")
|
|
_argc_index=$((_argc_index + 1))
|
|
;;
|
|
esac
|
|
done
|
|
_argc_require_params "error: the following required arguments were not provided:" \
|
|
'argc_path:--path <PATH>' 'argc_contents:--contents <CONTENTS>'
|
|
if [[ -n "${_argc_action:-}" ]]; then
|
|
$_argc_action
|
|
else
|
|
argc__fn=main
|
|
if [[ "${argc__positionals[0]:-}" == "help" ]] && [[ "${#argc__positionals[@]}" -eq 1 ]]; then
|
|
_argc_usage
|
|
fi
|
|
if [[ -z "${LLM_OUTPUT:-}" ]]; then
|
|
export LLM_OUTPUT=/dev/stdout
|
|
fi
|
|
fi
|
|
}
|
|
|
|
_argc_take_args() {
|
|
_argc_take_args_values=()
|
|
_argc_take_args_len=0
|
|
local param="$1" min="$2" max="$3" signs="$4" delimiter="$5"
|
|
if [[ "$min" -eq 0 ]] && [[ "$max" -eq 0 ]]; then
|
|
return
|
|
fi
|
|
local _argc_take_index=$((_argc_index + 1)) _argc_take_value
|
|
if [[ "$_argc_item" == *=* ]]; then
|
|
_argc_take_args_values=("${_argc_item##*=}")
|
|
else
|
|
while [[ $_argc_take_index -lt $_argc_len ]]; do
|
|
_argc_take_value="${argc__args[_argc_take_index]}"
|
|
if _argc_maybe_flag_option "$signs" "$_argc_take_value"; then
|
|
if [[ "${#_argc_take_value}" -gt 1 ]]; then
|
|
break
|
|
fi
|
|
fi
|
|
_argc_take_args_values+=("$_argc_take_value")
|
|
_argc_take_args_len=$((_argc_take_args_len + 1))
|
|
if [[ "$_argc_take_args_len" -ge "$max" ]]; then
|
|
break
|
|
fi
|
|
_argc_take_index=$((_argc_take_index + 1))
|
|
done
|
|
fi
|
|
if [[ "${#_argc_take_args_values[@]}" -lt "$min" ]]; then
|
|
_argc_die "error: incorrect number of values for \`$param\`"
|
|
fi
|
|
if [[ -n "$delimiter" ]] && [[ "${#_argc_take_args_values[@]}" -gt 0 ]]; then
|
|
local item values arr=()
|
|
for item in "${_argc_take_args_values[@]}"; do
|
|
IFS="$delimiter" read -r -a values <<<"$item"
|
|
arr+=("${values[@]}")
|
|
done
|
|
_argc_take_args_values=("${arr[@]}")
|
|
fi
|
|
}
|
|
|
|
_argc_require_params() {
|
|
local message="$1" missed_envs="" item name render_name
|
|
for item in "${@:2}"; do
|
|
name="${item%%:*}"
|
|
render_name="${item##*:}"
|
|
if [[ -z "${!name:-}" ]]; then
|
|
missed_envs="$missed_envs"$'\n'" $render_name"
|
|
fi
|
|
done
|
|
if [[ -n "${missed_envs}" ]]; then
|
|
_argc_die "$message$missed_envs"
|
|
fi
|
|
}
|
|
|
|
_argc_maybe_flag_option() {
|
|
local signs="$1" arg="$2"
|
|
if [[ -z "$signs" ]]; then
|
|
return 1
|
|
fi
|
|
local cond=false
|
|
if [[ "$signs" == *"+"* ]]; then
|
|
if [[ "$arg" =~ ^\+[^+].* ]]; then
|
|
cond=true
|
|
fi
|
|
elif [[ "$arg" == -* ]]; then
|
|
if (( ${#arg} < 3 )) || [[ ! "$arg" =~ ^---.* ]]; then
|
|
cond=true
|
|
fi
|
|
fi
|
|
if [[ "$cond" == "false" ]]; then
|
|
return 1
|
|
fi
|
|
local value="${arg%%=*}"
|
|
if [[ "$value" =~ [[:space:]] ]]; then
|
|
return 1
|
|
fi
|
|
return 0
|
|
}
|
|
|
|
_argc_die() {
|
|
if [[ $# -eq 0 ]]; then
|
|
cat
|
|
else
|
|
echo "$*" >&2
|
|
fi
|
|
exit 1
|
|
}
|
|
|
|
_argc_run "$@"
|
|
|
|
# ARGC-BUILD }
|