forked from E4S-Project/testsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process-json.sh
executable file
·67 lines (57 loc) · 1.62 KB
/
process-json.sh
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
# Function to print script usage
print_usage() {
echo "Usage: $0 [options] <json_file> [key (setup, clean, compile, run{default})] [value (pass{default}, fail, missing)]"
echo "Options:"
echo " -l Print output on a single line"
echo " -h Print this help message"
}
# Function to parse JSON and extract the test values
parse_json() {
local json_file="$1"
local print_on_single_line="$2"
local key="$3"
local value="$4"
# Read the JSON file
local json_data=$(cat "$json_file")
# Check if -l option is passed
local output_separator=$'\n'
if [[ "$print_on_single_line" == "-l" ]]; then
output_separator=" "
fi
# Extract relevant entries and filter them
local filtered_entries=$(echo "$json_data" | jq -c --arg key "$key" --arg value "$value" '.[] | select(.test_stages[$key] == $value and .test != null)')
# Loop through each filtered entry and print the directory path
while IFS= read -r entry; do
local test_value=$(echo "$entry" | jq -r '.test')
local directory_path=$(basename "$test_value")
echo -n "$directory_path$output_separator"
done <<< "$filtered_entries"
echo # Print a new line after the output
}
# Check if -h option is passed
if [[ "$1" == "-h" ]]; then
print_usage
exit 0
fi
# Check if a JSON file argument is provided
if [[ -z "$1" ]]; then
print_usage
exit 1
fi
# Check if -l option is passed
if [[ "$1" == "-l" ]]; then
if [[ -n "$2" ]]; then
parse_json "$2" "-l" "${3:-run}" "${4:-pass}"
else
print_usage
exit 1
fi
else
if [[ -n "$1" ]]; then
parse_json "$1" "" "${2:-run}" "${3:-pass}"
else
print_usage
exit 1
fi
fi