-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
With this change the failure handling is improved by printing the jq error in red color, printing the last successful jq query (meaning a query that didn't result in a non-zero exit code), and prints the result to the last successful query as well. Idea was proposed by @bazzargh on hackernews: https://news.ycombinator.com/item?id=38191147
- Loading branch information
Showing
2 changed files
with
38 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
JQ_REPL_JQ="${JQ_REPL_JQ:-jq}" | ||
|
||
query=$1 | ||
input_file=$2 | ||
last_success_file=$3 | ||
|
||
out="$last_success_file.new" | ||
trap 'rm -f "$out"' EXIT | ||
|
||
# for colorizing stderr | ||
red() { | ||
while read -r line; do | ||
printf "\033[31m%s\033[0m\n" "$line" | ||
done | ||
} | ||
|
||
# prints preview query result to stdout, colorizes stderr to red | ||
do_query() { | ||
$JQ_REPL_JQ --color-output ${JQ_REPL_ARGS:-} "$query" "$input_file" | ||
} 2> >(red) | ||
|
||
echo "Last successful jq expression: $query" | red >"$out" | ||
echo "Last success output:" | red >>"$out" | ||
if do_query | tee -a "$out"; then | ||
# save successful query plus the result | ||
mv "$out" "$last_success_file" | ||
else | ||
# print previously saved successful query | ||
cat "$last_success_file" | ||
fi |