diff options
Diffstat (limited to 'tests/shell/helpers/json-pretty.sh')
-rwxr-xr-x | tests/shell/helpers/json-pretty.sh | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/tests/shell/helpers/json-pretty.sh b/tests/shell/helpers/json-pretty.sh index 0d6972b8..5407a842 100755 --- a/tests/shell/helpers/json-pretty.sh +++ b/tests/shell/helpers/json-pretty.sh @@ -1,17 +1,30 @@ #!/bin/bash -e -# WARNING: the output is not guaranteed to be stable. +exec_pretty() { + # The output of this command must be stable (and `jq` and python + # fallback must generate the same output. -if command -v jq &>/dev/null ; then - # If we have, use `jq` - exec jq -fi + if command -v jq &>/dev/null ; then + # If we have, use `jq` + exec jq + fi -# Fallback to python. -exec python -c ' + # Fallback to python. + exec python -c ' import json import sys parsed = json.load(sys.stdin) print(json.dumps(parsed, indent=2)) ' +} + +[ "$#" -le 1 ] || { echo "At most one argument supported" ; exit 1 ; } + +if [ "$#" -eq 1 ] ; then + # One argument passed. This must be a JSON file. + [ -f "$1" ] || { echo "File \"$1\" does not exist" ; exit 1 ; } + exec_pretty < "$1" +fi + +exec_pretty |