Feature request: Adding `--format=json` option
I'm working on a fork of this awesome how-to article I found at https://community.platform.sh/t/how-to-back-up-environment-variables/430
I noticed that the scripts used in that article don't take into account that the variable value could be multi-line; just like most JSON value entries are.
The script iterates the available variable list by new-lines by exploding the output of
platform variable:list --format=csv
And of course the script fails on the next step, when it reaches the multi-line value.
Then again, similarly, If I want to get a specific variable via
platform variable:get my_variable --level=project --format=csv --no-header
multi-line values will still cause issues if I try to iterate through.
However, if I could have used --format=json option with variable:list and variable:get commands, it would be much simpler to process the JSON output; and no matter what the value is, it would always have worked correctly.
Would it be possible to add a --format=json option?
Ideally, for all the commands where it's already available as a parameter; ot at least for the variable:list and variable:get commands? :)
I notice that script is in PHP, which has a built-in function for parsing the CSV format and accommodating line breaks (str_getcsv()) - maybe it should use that instead of explode(',', explode("\n", $csv)).
The main reason the CLI doesn't have --format=json is that JSON is already available in the underlying API, which can be requested directly given the correct authentication.
There are a few "hidden" CLI commands that are available to help with the API authentication:
# Make a cURL request to the currently selected project's API
# e.g. list project-level variables
platform project:curl /variables
# Make an authenticated cURL request to https://api.platform.sh
# e.g. fetch your account information
platform api:curl /me
# Output an access token to authenticate to the API
# (not recommended security-wise, but helps with calls from other HTTP clients)
platform auth:token
So you could run
# Environment -level variables
platform project:curl /environments/master/variables
# Project-level variables
platform project:curl /variables
and parse JSON from there
I've suggested that for the migration repo https://gitlab.com/contextualcode/platformsh-migration/-/issues/1
Thank you @pjcdawkins. This is very helpful.