From d706b05653c4c2e0a3ab46bcffc3d2e583ee0c1a Mon Sep 17 00:00:00 2001 From: MaxJa4 <74194322+MaxJa4@users.noreply.github.com> Date: Mon, 28 Aug 2023 21:08:03 +0200 Subject: [PATCH] Allow override of lookup env --- envconfig.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/envconfig.go b/envconfig.go index 3a2aac7..491be2d 100644 --- a/envconfig.go +++ b/envconfig.go @@ -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) } @@ -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")