cfenv

package module
v1.23.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: Apache-2.0 Imports: 7 Imported by: 235

README

Go CF Environment Package

CI CodeQL OpenSSF Scorecard Go Version Latest Release

Overview

cfenv is a package to assist you in writing Go apps that run on Cloud Foundry. It provides convenience functions and structures that map to Cloud Foundry environment variable primitives (http://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html).

Requirements

Go 1.25 or later.

Usage

go get github.com/cloudfoundry-community/go-cfenv

package main

import (
	"fmt"

	"github.com/cloudfoundry-community/go-cfenv"
)

func main() {
	appEnv, _ := cfenv.Current()

	fmt.Println("ID:", appEnv.ID)
	fmt.Println("Index:", appEnv.Index)
	fmt.Println("Name:", appEnv.Name)
	fmt.Println("Host:", appEnv.Host)
	fmt.Println("Port:", appEnv.Port)
	fmt.Println("Version:", appEnv.Version)
	fmt.Println("Home:", appEnv.Home)
	fmt.Println("MemoryLimit:", appEnv.MemoryLimit)
	fmt.Println("WorkingDir:", appEnv.WorkingDir)
	fmt.Println("TempDir:", appEnv.TempDir)
	fmt.Println("User:", appEnv.User)
	fmt.Println("Services:", appEnv.Services)
}
File-based service bindings

Apps with the file-based-vcap-services feature enabled receive their bindings in the file named by VCAP_SERVICES_FILE_PATH rather than in VCAP_SERVICES, which Cloud Foundry then does not set at all. cfenv.Current reads whichever of the two the platform provides, so switching the feature on needs no application change beyond a version of this package that supports it.

The SERVICE_BINDING_ROOT form of RFC-0030 is not supported: those bindings follow the Kubernetes servicebinding.io layout, which has no faithful translation into the VCAP_SERVICES shape this package exposes.

Contributing

Pull requests welcomed. Please run make check and make audit before submitting.

Documentation

Overview

Package cfenv provides information about the current app deployed on Cloud Foundry, including any bound service(s).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurrentEnv

func CurrentEnv() map[string]string

CurrentEnv translates the current environment to a map[string]string.

func Env

func Env(env []string) map[string]string

Env translates the provided environment to a map[string]string.

func IsRunningOnCF added in v1.17.0

func IsRunningOnCF() bool

IsRunningOnCF returns true if the current environment is Cloud Foundry and false if it is not Cloud Foundry.

Types

type App

type App struct {
	ID              string   `json:"-"`                // DEPRECATED id of the instance
	InstanceID      string   `json:"instance_id"`      // id of the instance
	AppID           string   `json:"application_id"`   // id of the application
	Index           int      `json:"instance_index"`   // index of the app
	Name            string   `json:"name"`             // name of the app
	Host            string   `json:"host"`             // host of the app
	Port            int      `json:"port"`             // port of the app
	Version         string   `json:"version"`          // version of the app
	ApplicationURIs []string `json:"application_uris"` // application uri of the app
	SpaceID         string   `json:"space_id"`         // id of the space
	SpaceName       string   `json:"space_name"`       // name of the space
	Home            string   // root folder for the deployed app
	MemoryLimit     string   // maximum amount of memory that each instance of the application can consume
	WorkingDir      string   // present working directory, where the buildpack that processed the application ran
	TempDir         string   // directory location where temporary and staging files are stored
	User            string   // user account under which the DEA runs
	Services        Services // services bound to the app
	CFAPI           string   `json:"cf_api"` // URL for the Cloud Foundry API endpoint
	Limits          *Limits  `json:"limits"` // limits imposed on this process
}

An App holds information about the current app running on Cloud Foundry.

func Current

func Current() (*App, error)

Current creates a new App with the current environment; returns an error if the current environment is not a Cloud Foundry environment.

func New

func New(env map[string]string) (*App, error)

New creates a new App with the provided environment.

type Limits added in v1.15.0

type Limits struct {
	Disk int `json:"disk"` // disk limit
	FDs  int `json:"fds"`  // file descriptors limit
	Mem  int `json:"mem"`  // memory limit
}

type Service

type Service struct {
	Name         string                 // the binding name if the binding has one, otherwise the instance name
	Label        string                 // name of the service offering; "user-provided" for a user-provided instance
	Tags         []string               // tags for the service
	Plan         string                 // plan of the service
	Credentials  map[string]interface{} // credentials for the service
	VolumeMounts []map[string]string    `mapstructure:"volume_mounts"` // volume mount info as provided by the nfsbroker

	SyslogDrainURL string `mapstructure:"syslog_drain_url"` // where the service wants app logs streamed; set by `cf cups -l`
	InstanceGUID   string `mapstructure:"instance_guid"`    // GUID of the service instance
	InstanceName   string `mapstructure:"instance_name"`    // name the user gave the service instance
	BindingGUID    string `mapstructure:"binding_guid"`     // GUID of the service binding
	BindingName    string `mapstructure:"binding_name"`     // name the user gave the binding, if any
}

Service describes a bound service. For bindable services Cloud Foundry will add connection details to the VCAP_SERVICES environment variable when you restart your application, after binding a service instance to your application.

The results are returned as a JSON document that contains an object for each service for which one or more instances are bound to the application. The service object contains a child object for each service instance of that service that is bound to the application.

func (*Service) Credential added in v1.20.0

func (s *Service) Credential(keys ...string) (interface{}, bool)

Credential walks the credentials one key at a time and returns the value at the end of the path, whatever its type: a string, a bool, the float64 a JSON number decodes to, or a nested map or slice.

Passing the keys separately means every key is addressable, including one that contains a dot:

uri, ok := service.Credential("protocols", "amqp", "uri")
url, ok := service.Credential("jdbc.url")

It reports false if any key along the path is absent, if the path descends through a value that is not a nested object, or if no keys are given.

func (*Service) CredentialPath added in v1.20.0

func (s *Service) CredentialPath(path string) (interface{}, bool)

CredentialPath is Credential addressed by a single dot-delimited path:

uri, ok := service.CredentialPath("protocols.amqp.uri")

Because the path is split on every dot, it cannot address a key that itself contains one — CredentialPath("jdbc.url") looks for "url" inside "jdbc" and reports false. Use Credential for those keys.

func (*Service) CredentialString added in v1.15.0

func (s *Service) CredentialString(key string) (string, bool)

type Services

type Services map[string][]Service

Services is an association of service labels to a slice of services with that label.

func (*Services) WithLabel

func (s *Services) WithLabel(label string) ([]Service, error)

WithLabel finds the service with the specified label.

func (*Services) WithName

func (s *Services) WithName(name string) (*Service, error)

WithName finds the service with the specified name.

func (*Services) WithNameUsingPattern

func (s *Services) WithNameUsingPattern(namePattern string) ([]Service, error)

WithName finds the service with a name pattern.

func (*Services) WithTag

func (s *Services) WithTag(tag string) ([]Service, error)

WithTag finds services with the specified tag.

func (*Services) WithTagUsingPattern

func (s *Services) WithTagUsingPattern(tagPattern string) ([]Service, error)

WithTag finds services with a tag pattern.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL