diff --git a/process-json.sh b/process-json.sh new file mode 100755 index 00000000..6e96fec8 --- /dev/null +++ b/process-json.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +# Function to print script usage +print_usage() { + echo "Usage: $0 [options] [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