From d3930a6e21210617642f3d6b2a6774e58b7e9fa8 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 17 Aug 2024 08:14:10 +0800 Subject: refactor: improve scripts/run-{tool,agent}.sh (#95) --- scripts/run-tool.sh | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) (limited to 'scripts/run-tool.sh') diff --git a/scripts/run-tool.sh b/scripts/run-tool.sh index 26d3e83..ecbd372 100755 --- a/scripts/run-tool.sh +++ b/scripts/run-tool.sh @@ -57,34 +57,35 @@ run() { tool_path="$(cygpath -w "$tool_path")" fi - data="$( - echo "$tool_data" | \ - jq -r ' - to_entries | .[] | - (.key | split("_") | join("-")) as $key | - if .value | type == "array" then - .value | .[] | "--\($key)\n\(. | @json)" - elif .value | type == "boolean" then - if .value then "--\($key)" else "" end - else - "--\($key)\n\(.value | @json)" - end' - )" || { + jq_script="$(cat <<-'EOF' +def escape_shell_word: + tostring + | gsub("'"; "'\"'\"'") + | gsub("\n"; "'$'\\n''") + | "'\(.)'"; +def to_args: + to_entries | .[] | + (.key | split("_") | join("-")) as $key | + if .value | type == "array" then + .value | .[] | "--\($key) \(. | escape_shell_word)" + elif .value | type == "boolean" then + if .value then "--\($key)" else "" end + else + "--\($key) \(.value | escape_shell_word)" + end; +[ to_args ] | join(" ") +EOF +)" + args="$(echo "$tool_data" | jq -r "$jq_script")" || { die "Invalid JSON data" } - while IFS= read -r line; do - if [[ "$line" == '--'* ]]; then - args+=("$line") - else - args+=("$(echo "$line" | jq -r '.')") - fi - done <<< "$data" + no_llm_output=0 if [[ -z "$LLM_OUTPUT" ]]; then no_llm_output=1 export LLM_OUTPUT="$(mktemp)" fi - "$tool_path" "${args[@]}" + eval "'$tool_path' $args" if [[ "$no_llm_output" -eq 1 ]]; then cat "$LLM_OUTPUT" fi -- cgit v1.2.3