summaryrefslogtreecommitdiffstats
path: root/tests/shell/helpers/json-pretty.sh
blob: 5407a8420058d0bfb9d4e0e57e30a0caff90e6c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/bin/bash -e

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

	# 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