Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow override of lookupEnv for _FILE support #210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ type Setter interface {
Set(value string) error
}

// `os.Getenv` cannot differentiate between an explicitly set empty value
// and an unset value. `os.LookupEnv` is preferred to `syscall.Getenv`,
// but it is only available in go1.5 or newer. We're using Go build tags
// here to use os.LookupEnv for >=go1.5
// LookupEnv is a function that retrieves the value of the environment variable
var Lookup func(string) (string, bool) = lookupEnv

func (e *ParseError) Error() string {
return fmt.Sprintf("envconfig.Process: assigning %[1]s to %[2]s: converting '%[3]s' to type %[4]s. details: %[5]s", e.KeyName, e.FieldName, e.Value, e.TypeName, e.Err)
}
Expand Down Expand Up @@ -186,13 +193,9 @@ func Process(prefix string, spec interface{}) error {

for _, info := range infos {

// `os.Getenv` cannot differentiate between an explicitly set empty value
// and an unset value. `os.LookupEnv` is preferred to `syscall.Getenv`,
// but it is only available in go1.5 or newer. We're using Go build tags
// here to use os.LookupEnv for >=go1.5
value, ok := lookupEnv(info.Key)
value, ok := Lookup(info.Key)
if !ok && info.Alt != "" {
value, ok = lookupEnv(info.Alt)
value, ok = Lookup(info.Alt)
}

def := info.Tags.Get("default")
Expand Down