env

package
Version: v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2018 License: MIT Imports: 5 Imported by: 22

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsVarNotFound

func IsVarNotFound(err error) bool

IsVarNotFound returns a boolean indicating whether the error is known to report that an environment variable is not found.

func IsVarNotParsable

func IsVarNotParsable(err error) bool

IsVarNotParsable returns a boolean indicating whether the error is known to report that an environment variable is not parsable.

func NewVarNotFoundErr

func NewVarNotFoundErr(name string) error

NewVarNotFoundErr makes a new error that indicates that an environment variable is not found.

func NewVarNotParsableErr

func NewVarNotParsableErr(name string) error

NewVarNotParsableErr makes a new error that indicates that an environment variable is not parsable.

func NoopLookup added in v0.0.6

func NoopLookup(name string) (string, bool)

NoopLookup always returns false for the environment variable with the given name. Useful as a fallback to return for an env lookup that may not always be applicable, e.g. if not running in CloudFoundry, a user provided service lookup won't be applicable.

Types

type Lookup

type Lookup func(name string) (string, bool)

Lookup must return the value for the environment variable with the given name and whether or not it was found.

func NewLookupFromUPS added in v0.0.6

func NewLookupFromUPS(app *cfenv.App, name string) Lookup

NewLookupFromUPS looks for a CloudFoundry bound service with the given name. This allows sourcing environment variables from a user-provided service. If no service is found, a passthrough lookup is used that always returns false.

type VarSet

type VarSet struct {
	// contains filtered or unexported fields
}

VarSet provides methods to access environment variables from a list of Lookup sources.

func NewVarSet

func NewVarSet(opts ...VarSetOpt) *VarSet

NewVarSet makes a new VarSet from opts.

func (*VarSet) AppendSource

func (v *VarSet) AppendSource(s Lookup) *VarSet

AppendSource adds s as another lookup source after all existing sources.

func (*VarSet) Bool

func (v *VarSet) Bool(name string) (bool, error)

Bool gets the boolean environment variable with the given name, if set. If not found, returns false. If found and cannot parse, returns an error.

func (*VarSet) IsSet added in v0.0.5

func (v *VarSet) IsSet(name string) bool

IsSet looks for a given name within all lookup sources in order. If found, true is returned, else false.

func (*VarSet) Lookup added in v0.0.4

func (v *VarSet) Lookup(name string) (string, bool)

Lookup looks for a given name within all lookup sources in order. If no variable is found, an empty string and false is returned.

func (*VarSet) MustBool

func (v *VarSet) MustBool(name string) bool

MustBool gets the boolean environment variable with the given name, if set. If not set, false is returned. This matches behavior one would often see with command line boolean arguments: if you don't set it, it defaults to false. It will panic with an error if it is not a valid boolean. If desired, callers can use IsVarNotParsable when recovering from the panic in order to check type of the error.

func (*VarSet) MustHexEncodedByteArray

func (v *VarSet) MustHexEncodedByteArray(name string, decodedByteLength int) []byte

MustHexEncodedByteArray gets the hex-encoded environment variable with the given name, if set. It will panic with an error if it is not set if it is not a valid hex-encoded string or if its length does not match decodedByteLength. If desired, callers can use IsVarNotFound and IsVarNotParsable when recovering from the panic in order to check type of the error.

func (*VarSet) MustString

func (v *VarSet) MustString(name string) string

MustString gets the string environment variable with the given name, if set. It will panic with an error if it is not set. If desired, callers can use IsVarNotFound when recovering from the panic in order to check type of the error.

func (*VarSet) String

func (v *VarSet) String(name, defaultVal string) string

String gets the string environment variable with the given name, if set. If not found, returns defaultVal.

type VarSetOpt

type VarSetOpt func(v *VarSet)

VarSetOpt is a type which defines VarSet options.

func WithMapLookup

func WithMapLookup(m map[string]string) VarSetOpt

WithMapLookup configures the VarSet to use the given map as a lookup source.

Example
package main

import (
	"fmt"

	"github.com/govau/cf-common/env"
)

func main() {
	m := map[string]string{
		"FOO": "bar",
	}

	vs := env.NewVarSet(env.WithMapLookup(m))

	v := vs.MustString("FOO")

	fmt.Println(v)
}
Output:

func WithOSLookup

func WithOSLookup() VarSetOpt

WithOSLookup configures the VarSet to use the OS env as a lookup source.

Example
package main

import (
	"fmt"
	"os"

	"github.com/govau/cf-common/env"
)

func main() {
	os.Setenv("FOO", "bar") // Simulate OS env.

	vs := env.NewVarSet(env.WithOSLookup())

	v := vs.MustString("FOO")

	fmt.Println(v)
}
Output:

func WithUPSLookup

func WithUPSLookup(app *cfenv.App, name string) VarSetOpt

WithUPSLookup configures the VarSet to use the CloudFoundry user-provided service with the given name as a lookup source.

Example
app, err := cfenv.Current()
if err != nil {
	// ...
}

opts := []env.VarSetOpt{
	env.WithOSLookup(), // Always look in the OS env first.
	env.WithUPSLookup(app, "service-1"),
}

vs := env.NewVarSet(opts...)

v := vs.MustString("FOO")

fmt.Println(v)
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL