Skip to content

Commit

Permalink
Make public some more methods that are useful for consumers of this c…
Browse files Browse the repository at this point in the history
…lass
  • Loading branch information
aeijdenberg committed Jul 11, 2018
1 parent d32dbbd commit f855260
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 5 additions & 3 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
// and whether or not it was found.
type Lookup func(name string) (string, bool)

// passthruLookup always returns false for the environment variable with the
// given name.
func passthruLookup(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.
func NoopLookup(name string) (string, bool) {
return "", false
}

Expand Down
12 changes: 6 additions & 6 deletions env/ups.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import (
"github.com/cloudfoundry-community/go-cfenv"
)

// WithUPSLookup configures the VarSet to use the Cloud Foundry user-provided
// WithUPSLookup configures the VarSet to use the CloudFoundry user-provided
// service with the given name as a lookup source.
func WithUPSLookup(app *cfenv.App, name string) VarSetOpt {
return func(v *VarSet) {
v.AppendSource(newLookupFromUPS(app, name))
v.AppendSource(NewLookupFromUPS(app, name))
}
}

// newLookupFromUPS looks for a Cloud Foundry bound service with the given name.
// 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.
func newLookupFromUPS(app *cfenv.App, name string) Lookup {
func NewLookupFromUPS(app *cfenv.App, name string) Lookup {
if app == nil {
return passthruLookup
return NoopLookup
}
service, err := app.Services.WithName(name)
// The only error WithName will return is that the service was not found,
// so it is OK that we ignore the error here.
if err != nil {
return passthruLookup
return NoopLookup
}
return func(k string) (string, bool) {
v, ok := service.Credentials[k]
Expand Down

0 comments on commit f855260

Please sign in to comment.