set -e
TOOL_NAME="{{tool_name}}"
TOOL_COMMAND="{{tool_command}}"
SUPPORTS_JSON="{{supports_json}}"
log() {
echo "[$TOOL_NAME] $1" >&2
}
error_exit() {
echo "{\"status\": \"error\", \"error\": \"$1\", \"tool\": \"$TOOL_NAME\"}" >&2
exit 1
}
INPUT_FILE=""
OUTPUT_FILE=""
VERBOSE=false
JSON_MODE=false
ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--input|-i)
INPUT_FILE="$2"
shift 2
;;
--output|-o)
OUTPUT_FILE="$2"
shift 2
;;
--json)
JSON_MODE=true
shift
;;
--verbose|-v)
VERBOSE=true
shift
;;
--help)
echo "Usage: $0 [OPTIONS] [-- COMMAND_ARGS]"
echo "Options:"
echo " --input FILE Input file"
echo " --output FILE Output file"
echo " --json Enable JSON mode"
echo " --verbose Verbose output"
echo " --help Show this help"
echo " -- COMMAND_ARGS Arguments to pass to wrapped command"
exit 0
;;
--)
shift
ARGS+=("$@")
break
;;
*)
ARGS+=("$1")
shift
;;
esac
done
if [[ "$VERBOSE" == "true" ]]; then
log "Starting $TOOL_NAME"
log "Input file: $INPUT_FILE"
log "Output file: $OUTPUT_FILE"
log "JSON mode: $JSON_MODE"
log "Command args: ${ARGS[*]}"
fi
if [[ -n "$INPUT_FILE" ]]; then
if [[ ! -f "$INPUT_FILE" ]]; then
error_exit "Input file not found: $INPUT_FILE"
fi
if [[ "$JSON_MODE" == "true" ]] && [[ "$SUPPORTS_JSON" == "true" ]]; then
if command -v jq >/dev/null 2>&1; then
JSON_INPUT=$(cat "$INPUT_FILE")
ARGS+=($(echo "$JSON_INPUT" | jq -r 'to_entries[] | "--\(.key) \(.value)"' 2>/dev/null || echo ""))
else
log "Warning: jq not available, cannot parse JSON input"
fi
fi
fi
if [[ "$VERBOSE" == "true" ]]; then
log "Executing: $TOOL_COMMAND ${ARGS[*]}"
fi
OUTPUT=""
ERROR_OUTPUT=""
EXIT_CODE=0
if [[ -n "$OUTPUT_FILE" ]]; then
if "$TOOL_COMMAND" "${ARGS[@]}" > "$OUTPUT_FILE" 2>"${OUTPUT_FILE}.stderr"; then
EXIT_CODE=0
else
EXIT_CODE=$?
ERROR_OUTPUT=$(cat "${OUTPUT_FILE}.stderr" 2>/dev/null || echo "Command failed with exit code $EXIT_CODE")
fi
else
if OUTPUT=$("$TOOL_COMMAND" "${ARGS[@]}" 2>&1); then
EXIT_CODE=0
else
EXIT_CODE=$?
ERROR_OUTPUT="$OUTPUT"
OUTPUT=""
fi
fi
if [[ "$JSON_MODE" == "true" ]] && [[ "$SUPPORTS_JSON" == "true" ]]; then
if [[ -n "$OUTPUT" ]] && echo "$OUTPUT" | jq . >/dev/null 2>&1; then
JSON_RESULT="$OUTPUT"
else
JSON_RESULT=$(cat <<EOF
{
"status": "$([[ $EXIT_CODE -eq 0 ]] && echo \"success\" || echo \"error\")",
"output": $(echo "$OUTPUT" | jq -R -s . 2>/dev/null || echo "null"),
"exit_code": $EXIT_CODE,
"tool": "$TOOL_NAME",
"command": "$TOOL_COMMAND",
"args": $(echo "${ARGS[*]}" | jq -R -s 'split(" ")' 2>/dev/null || echo "[]")
}
EOF
)
fi
echo "$JSON_RESULT"
else
if [[ $EXIT_CODE -eq 0 ]]; then
echo "$OUTPUT"
else
echo "$ERROR_OUTPUT" >&2
exit $EXIT_CODE
fi
fi
exit $EXIT_CODE