cloudfoundry / cli

The official command line client for Cloud Foundry
https://docs.cloudfoundry.org/cf-cli
Apache License 2.0
1.75k stars 926 forks source link

cf env is not parseable #860

Closed erjoalgo closed 8 years ago

erjoalgo commented 8 years ago
Getting env variables for app mg-efj-dtvbdtd in org jztcbat.foffxtydoicfegb@ws.hfl / space dev as wzfqhda.akkgwskvefqowxb@nh.gec...
OK

System-Provided:
{
 "VCAP_SERVICES": {
  "sbvuzm-uen": [
   {
    "dvwjncbhrhw": {
     "cjbnbxNn": "aprgc://n9076l71-3pxl-5037-rs03-913skgg54028.nypjjc-cbq.vkz.kbq-kk.sqt.qknanj.fz/bghcu/ftsuq",
     "mqeokppir": "l8497n33-0mat-0421-bc22-035hfvr34036",
     "cof": "ihsvs://b2093n27-8msu-1777-zv12-283utkg92242.sgrutu-wiy.rcn.rgt-in.uao.kwxjub.dk",
     "uzrq": {
      "lxdq-jwcrfm-vbru": "A-Tsrxynnc-Hpfd-Mv",
      "rglm-ygoxby-tkktv": "q0224d60-6txb-8379-ww95-717dqct71050"
     }
    },
    "vzxmo": "qnkutl-wnn",
    "whlf": "mpx-glj-bzcyluf",
    "ytem": "Gppzrq",
    "dipprwxs": kuor,
    "sukxdd_oufaz_ymz": rmwe,
    "nmil": []
   }
  ]
 }
}

{
 "FRWY_ZOAFBYUUVUV": {
  "zldotvgmomi_vo": "wl07485n-rn58-53h2-wmh4-8x47764nwrz6",
  "xfdznrfguzj_ydlo": "sx-qfc-mlqgwxy",
  "iibpzcdytul_kdjm": [
   "xt-kyz-xwoafni.ucu.kny-ip.kpp.hworns.ag"
  ],
  "roolhukysab_waqzqzi": "g4a90100-f79y-85d8-j8t9-24rn3h5v43ue",
  "dpwyeh": {
   "lfie": 8167,
   "hqo": 00121,
   "ymf": 797
  },
  "nrus": "is-ayo-jdxzami",
  "lzdht_sw": "19wv240l-84ki-4r7s-4ej8-g8g68lpoe8t1",
  "oqrhf_cboc": "gxd",
  "tult": [
   "zr-kne-owuzrpn.zmf.qip-ad.cao.rylhsp.vh"
  ],
  "aqzst": wonp,
  "qtrsllo": "s8h86732-q47s-35d8-e7n6-44mv6x7w86dd"
 }
}

No user-defined env variables have been set

No running env variables have been set

No staging env variables have been set

Could you please send non-json output to stderr? Could you also combine "System provided" and "application provided" into the same json?

cf-gitbot commented 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.

dkoper commented 8 years ago

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

dkoper commented 8 years ago

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.

erjoalgo commented 8 years ago

I was not aware of cf curl and the /v2/app/:guid/env endpoint. I would be in favor of closing this permanently.

jfsanchez91 commented 4 years ago

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

fusionfox commented 1 year ago

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 🤦