One can use the command cf target
to discover the current org and space.
% cf target
API endpoint: https://api.run.domain.com (API version 2.48.0)
User: admin
Org: myorg
Space: myspace
I want to use the REST API (cf curl /v2/...
) to get the current org and space. I do not want to parse the results of the command cf target
.
I typically write scripts in Bash that define variables by doing actions such as (for example to get the CF API version):
Result=$(cf curl /v2/info | jq -r '.api_version')
So, the question is: how can I get the org and space via cf curl
, assuming I have already authenticated (performed cf login
)?
how can I get the org and space via cf curl, assuming I have already authenticated (performed cf login)?
This doesn't comes from the CAPI. The API isn't managing this aspect of your state. This state is kept in the config.json
file on your local machine, which is managed by the cf cli.
If you want to get the current org and space, you can just read it from that file.
jq ".OrganizationFields.Name,.SpaceFields.Name" ~/.cf/config.json
The normal path to your config file is under your home directory in a folder .cf
, but the path to your config file can vary if CF_HOME
is set.
CF_HOME=path/to/dir/ Override path to default config directory
While I wouldn't recommend it, you can even update the config file manually to change your org or space. Again, not recommended but possible.
For future reference, if you wonder about how the cf cli does something the easiest way to see what API call's it's making is to run with trace enabled. This will output the HTTP request and response information to your console.
CF_TRACE=true cf <insert-command>
If you do this with cf target
you'll see that no commands are issued, which is because it's just reading the state from the file I mentioned. If you run something that will talk to the API like CF_TRACE=true cf orgs
then you'll see HTTP request and response information.
Last note, on Windows you need to run set CF_TRACE=true
first. Then you can run cf orgs
or whatever command you want and you'll get trace output. This is because the Windows command prompt doesn't let you set environment variables inline like Bash. If you're using Bash on Windows, this shouldn't be necessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With