github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/tests/load-tests/ci-scripts/offline-token-regeneration/json_split_and_encode_v2.sh (about)

     1  #!/bin/bash
     2  
     3  # Check for input file argument
     4  if [ "$#" -ne 2 ]; then
     5      echo "Usage: $0 <input_file> <output_file>"
     6      exit 1
     7  fi
     8  
     9  input_file="$1"
    10  parts_count="$2" # Number of parts to split into
    11  
    12  # Function to split JSON, encode in base64, and save to separate files
    13  split_encode_and_save_json() {
    14      local input_file="$1"
    15      local parts_count="$2"
    16  
    17      # Calculate the total number of records in the JSON array
    18      local total_lines=$(jq '.creds | length' < "$input_file")
    19  
    20      # Calculate the number of records per part, ensuring it rounds up
    21      # This ensures that each part will have an equal number of records
    22      # with the last part possibly having fewer.
    23      local lines_per_part=$(( (total_lines + parts_count - 1) / parts_count ))
    24  
    25      # Split the JSON array, encode each part, and save to files
    26      for ((i = 0; i < parts_count; i++)); do
    27          # Calculate the start and end indices for each part
    28          local start=$(( i * lines_per_part ))
    29          local end=$(( start + lines_per_part ))
    30  
    31          # Adjust end index if it exceeds the total number of records
    32          # This is necessary for the last part if total_records is not
    33          # perfectly divisible by parts_count.
    34          if [ $end -gt $total_lines ]; then
    35              end=$total_lines
    36          fi
    37  
    38          # Extract part of the JSON array and save base64 encoded to a file
    39          jq "{ \"creds\": .creds | .[$start:$end] }" < "$input_file" | base64 -w 0 > "part_$(($i + 1)).encoded"
    40      done
    41  }
    42  
    43  split_encode_and_save_json "$input_file" "$parts_count"
    44  
    45  echo "Splitting and encoding completed. Part files are created."