Closed erjoalgo closed 8 years ago
We have created an issue in Pivotal Tracker to manage this:
https://www.pivotaltracker.com/story/show/121683759
The labels on this github issue will be updated when the story is started.
Hi @erjoalgo,
I believe the cf env
command was introduced for users to verify environment variables they set using e.g. cf set-env
had been set. Why are you looking for (I assume) a way to parse them as JSON?
Cheers, Dies Koper CF CLI PM
Closing. Will revisit when we understand the use case better. A resolution to part of the issue (interpersing json and non-json output) is considered in #864 and #851.
I was not aware of cf curl
and the /v2/app/:guid/env
endpoint. I would be in favor of closing this permanently.
Closing. Will revisit when we understand the use case better. A resolution to part of the issue (interpersing json and non-json output) is considered in #864 and #851.
Because sometimes you want to use the CF CLI in a self-developed automation tool :) and in JSON format it is easiest to parse the output. I think you should add a parameter to cf env
like cf env your-app-name --json
. That's the use case
I managed to extract the JSON from the output of cf env
using a regex:
cf env "$APPNAME" | sed -n '/[]{"}[]/p'
I then found the /v2/app/:guid/env
endpoint that erjoalgo mentioned above which just returns JSON.
That seems like a much more reliable and sensible way to do it:
cf curl "/v2/apps/$(cf app --guid "$APPNAME")/env"
Then you can use jq
to get whatever value you need:
cf curl "/v2/apps/$(cf app --guid "$APPNAME")/env" | jq -r '.system_env_json.VCAP_APPLICATION.application_name'
I found this GitHub Issue while searching for a way to get the uri
from a connected Postgres service and use it to connect to the database with psql
:
psql $(cf curl "/v2/apps/$(cf app --guid "$APPNAME")/env" | jq -r '.system_env_json.VCAP_SERVICES.postgresql[0].credentials.uri')
For convenience, you can wrap it in a function that allows you to also pass other arguments to psql
:
cfpsql() { psql $(cf curl "/v2/apps/$(cf app --guid "$1")/env" | jq -r '.system_env_json.VCAP_SERVICES.postgresql[0].credentials.uri') "${@:2}"; }
Hopefully some of that saves somebody else some time 🤦
Could you please send non-json output to stderr? Could you also combine "System provided" and "application provided" into the same json?