Documentation ¶
Index ¶
- func IsVarNotFound(err error) bool
- func IsVarNotParsable(err error) bool
- func NewVarNotFoundErr(name string) error
- func NewVarNotParsableErr(name string) error
- func NoopLookup(name string) (string, bool)
- type Lookup
- type VarSet
- func (v *VarSet) AppendSource(s Lookup) *VarSet
- func (v *VarSet) Bool(name string) (bool, error)
- func (v *VarSet) IsSet(name string) bool
- func (v *VarSet) Lookup(name string) (string, bool)
- func (v *VarSet) MustBool(name string) bool
- func (v *VarSet) MustHexEncodedByteArray(name string, decodedByteLength int) []byte
- func (v *VarSet) MustString(name string) string
- func (v *VarSet) String(name, defaultVal string) string
- type VarSetOpt
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsVarNotFound ¶
IsVarNotFound returns a boolean indicating whether the error is known to report that an environment variable is not found.
func IsVarNotParsable ¶
IsVarNotParsable returns a boolean indicating whether the error is known to report that an environment variable is not parsable.
func NewVarNotFoundErr ¶
NewVarNotFoundErr makes a new error that indicates that an environment variable is not found.
func NewVarNotParsableErr ¶
NewVarNotParsableErr makes a new error that indicates that an environment variable is not parsable.
func NoopLookup ¶ added in v0.0.6
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 ¶
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
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 (*VarSet) AppendSource ¶
AppendSource adds s as another lookup source after all existing sources.
func (*VarSet) Bool ¶
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
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
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 ¶
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 ¶
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 ¶
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.
type VarSetOpt ¶
type VarSetOpt func(v *VarSet)
VarSetOpt is a type which defines VarSet options.
func WithMapLookup ¶
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 ¶
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: