Home > Software design >  My gcloud query fetches 2 sets of values, how do I display them separately?
My gcloud query fetches 2 sets of values, how do I display them separately?

Time:10-13

The following gcloud query (somewhat) returns all the GCP folders within an organization:

ORG=$(\
  gcloud organizations list \
  --format="value(name)")
FOLDERS=$(\
  gcloud resource-manager folders list \
  --format="value(ID,displayName)" \
  --organization=${ORG})

for FOLDER in ${FOLDERS}
do
  echo ${FOLDER}
done
  1. How do I echo the folder IDs and folder displayNames separately?

  2. Also, the folder displayNames contain whitespaces. I presume I'd need to use base64 encoding decoding to display them correctly. But I don't know how to do that.

Can someone please guide me? Thanks.

CodePudding user response:

My approach to this has been to output CSV from the gcloud command that's providing the loop's range using format="csv(ID,displayName) to give values that are comma-separated and then use IFS and read to split the string into the 2 fields

In your for loop you can use:

IFS=, read ID,DISPLAY_NAME <<< ${FOLDER}

IIRC your can --format="csv(ID, displayName.encode(base64))" to encode displayName and then use bash's base64 --decode to decode it. I have an example on here somewhere using it with gcloud but am on my phone and it's less easy to search.

Found it: https://stackoverflow.com/a/63852670/609290

By the way, these functions are all documented -- albeit not easy to find -- as gcloud topic projections

  • Related