github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/tests/load-tests/ci-scripts/merge-json.sh (about)

     1  #!/bin/bash
     2  
     3  # Function to check if jq is installed and install it if not (for Ubuntu-based systems)
     4  check_and_install_jq() {
     5      if ! command -v jq &>/dev/null; then
     6          echo "jq not found. Please Install it"
     7          exit 1
     8      fi
     9  }
    10  
    11  # make a dummy kube config
    12  mkdir -p ~/.kube
    13  cat <<EOF > ~/.kube/config
    14  apiVersion: v1
    15  clusters:
    16  - cluster:
    17      server: YOUR_KUBE_API_SERVER_URL
    18    name: YOUR_CLUSTER_NAME
    19  contexts:
    20  - context:
    21      cluster: YOUR_CLUSTER_NAME
    22      user: YOUR_USER_NAME
    23    name: YOUR_CONTEXT_NAME
    24  current-context: YOUR_CONTEXT_NAME
    25  kind: Config
    26  preferences: {}
    27  users:
    28  - name: YOUR_USER_NAME
    29    user:
    30      token: YOUR_KUBE_API_TOKEN
    31  EOF
    32  
    33  
    34  
    35  # Function to decode and save JSON data from variable to pre_N.json
    36  decode_and_save_json() {
    37      local variable_name="$1"
    38      local file_name="pre_${variable_name#*_}.json"
    39      echo "${!variable_name}" | base64 --decode > "$file_name"
    40  }
    41  
    42  # Get the total number of parameters passed to the script (N)
    43  N=$#
    44  
    45  # Loop through all the variables
    46  for ((i = 1; i <= N; i++)); do
    47      var_name="STAGING_USERS_$i"
    48      decode_and_save_json "$var_name"
    49  done
    50  
    51  # Merge all the pre_N.json files into output.json
    52  jq -s '[.[].creds]' pre_*.json > data.json
    53  jq 'add' data.json > users.json
    54  
    55  
    56  # Optionally, you can remove the temporary pre_N.json files
    57  # Uncomment the next line to delete the files
    58  rm pre_*.json
    59  
    60  echo "Decoded JSON data merged and stored in users.json."