Reputation: 49
I have downloaded my gcloud configs as json (with gcloud config configurations list --format=json). Then I picked up one config out from the collection returned, and updated all the features/flags I want in that json and saved as a file. Now I want to create a new configuration with this updated JSON file, how can I do that? Thanks in advance!
Upvotes: 0
Views: 2256
Reputation: 496
I'm assuming that you are on a Unix-like machine, and that your configurations are stored in ~/.config/gcloud/configurations
.
I'm also assuming that you created the original JSON output by running something similar to the following, and then editing the file in-place:
gcloud config configurations activate SOME_CONFIG
gcloud config list --format=json > newconfig.json
First, create the new configuration that you want to populate:
gcloud config configurations create newconfig
You can confirm that this file was created by running:
ls ~/.config/gcloud/configurations/config_newconfig
Next, use this hidden command to convert the JSON file to config format, and overwrite the configuration file:
gcloud meta list-from-json newconfig.json --format=config > ~/.config/gcloud/configurations/config_newconfig
You can confirm that things are as expected by running:
gcloud config configurations activate newconfig
gcloud config list
Upvotes: 2
Reputation: 40136
Please test this thoroughly; improvements welcome
Requires: jq
name=="foo"
.properties
object into an array {"key":.., "value":[...]}
{"section":...,"properties"...}
converting values to an array and filtering null
{"section":...,"properties":{"key":...,"value":...}
gcloud config set {section}/${properties[key]} ${properties[value]}
NOTE There ought to be a way to do this with a single
map
: capturing thesection
value and then for each property key:value pair (where value !=null
) emitting thegcloud config set
command
FILTER='.[]
|select(.name=="foo")
|.properties|to_entries
|map(
{
"section": .key,
"properties":.value|to_entries|.[]|select(.value!=null)
}
)
|map(
"gcloud config set "+.section+"/"+.properties["key"]+" "+.properties["value"]
)
|.[]'
# Or cat /path/to/your/saved.json
gcloud config configurations list --format=json \
| jq -r "${FILTER}"
Example: https://jqplay.org/s/ZO_6urQma8
You could pipe the output into a script and check the script before running it.
Upvotes: 2