diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..604dd83 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/nerdynz/trove + +go 1.12 + +require github.com/joho/godotenv v1.3.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ead7071 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc= +github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg= diff --git a/trove.go b/trove.go index b89023b..ce33a2e 100644 --- a/trove.go +++ b/trove.go @@ -143,13 +143,21 @@ func (s *Settings) GetBool(setting string) bool { return val } +// Get gets and caches an ENV setting. theDefault will be used if setting is blank and is optional func (s *Settings) Get(setting string) string { + return s.GetWithDefault(setting, "") +} + +func (s *Settings) GetWithDefault(setting string, theDefault string) string { val, ok := s.strings[setting] if !ok { s.sLock.Lock() // map is shared across the whole application, so it needs to be defer s.sLock.Unlock() newVal := os.Getenv(setting) + if newVal == "" && theDefault != "" { + return theDefault + } s.strings[setting] = newVal val = newVal }