cloudfoundry-community / node-cfenv

easy access to your Cloud Foundry application environment for node
Apache License 2.0
73 stars 20 forks source link

Possibility to only get a certain env-var (e.g. cg env APP vcap_services) #20

Closed rvplauborg closed 7 years ago

rvplauborg commented 7 years ago

It would be so nice to have this functionality - at work, I only need to get the VCAP_SERVICES attribute as well as the user-defined ones. I want to write a script to just pipe the output from cf-env into a file we then can use as our .env file in the project, so we don't have the .env file added to git.

As it is now, it requires me to write a string parsing script to actually read each line and in the end, return me VCAP_SERVICES and the user-defined environment variables. This would be nice to not have to do :)

pmuellr commented 7 years ago

Sorry, I'm not sure what you're asking for. Since the VCAP_SERVICES and and VCAP_APPLICATION environment variables contain JSON, perhaps a utility like jq will work well for you.

https://stedolan.github.io/jq/

jq reads JSON text from stdin (or maybe a file also), and then will reformat it, or allow you to make "queries" against the JSON object.

eg:

$ export JSON_ENV='{"a":1,"b":2,"c":3}'
$ echo $JSON_ENV | jq
{
  "a": 1,
  "b": 2,
  "c": 3
}
$ echo $JSON_ENV | jq .a
1
rvplauborg commented 7 years ago

Well, I also think I wrote something quite messy :)

I did not know that at all - any way you can do something similar on windows? The reason for my initial question was that I want to have a bat-script that calls cg-env APP and then pipes only e.g. VCAP_SERVICES into a file.

pmuellr commented 7 years ago

Closing as there is no problem reported here. Please re-open if I'm mistaken.