cfsvcenv

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2017 License: MIT Imports: 4 Imported by: 0

README

cfsvcenv · Travis-CI GoDoc Report card

cfsvcenv is a Go package for binding Cloud Foundry service credentials (including user-provided services) to environment variables.

Rationale

Your Go program uses Cloud Foundry services to provide configuration to it. Cloud Foundry does this by injecting the configuration into the VCAP_SERVICES environment variable.

However, you want to keep your program CF-agnostic, and you also want to keep development simple

cfsvcenv binds the CF service credentials into the OS's environment so that your Go program does not need any special treatment when used within CF.

Usage and examples

Caveats

You cannot define environment variables in the global scope because these have not yet been bound. This is because the init() function that does the binding has not yet run.

For example this won't work:

package main

import (
	"fmt"
	"os"
)

var serviceAPIKey = os.Getenv("SERVICE_API_KEY")

func main() {
	fmt.Println(serviceAPIKey) // Will be empty
}

Development

go test -race ./...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bind

func Bind() error

Bind will take each of the Cloud Foundry service credentials found in the VCAP_SERVICES environment variable and put each services's credential value in the environment using setenv.

In order to avoid conflicts across services that have a credential with the same name (e.g. `service-1` and `service-2` both have a credential named `API_KEY`), each environment variable is prefixed with a transformed service name. Credential name will also be transformed - this is because it is conventional for environment variables to be upper case. E.g. for the `service-1` credential `api-key`, the resulting environment variable will be named `SERVICE_1_API_KEY`.

It is assumed that the service `credentials` is a map of key/value pairs. If it is not, that service will simply be ignored. Each credential value is converted to a string using the %v verb from fmt. In this way, it does not make sense to use anything except primitives for credential values.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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