source.go raw

   1  package env
   2  
   3  import "os"
   4  
   5  // Source represents a source of environment variables.
   6  type Source interface {
   7  	// LookupEnv retrieves the value of the environment variable named by the key.
   8  	LookupEnv(key string) (value string, ok bool)
   9  }
  10  
  11  // OS is the main [Source] that uses [os.LookupEnv].
  12  var OS Source = sourceFunc(os.LookupEnv)
  13  
  14  // Map is a [Source] implementation useful in tests.
  15  type Map map[string]string
  16  
  17  // LookupEnv implements the [Source] interface.
  18  func (m Map) LookupEnv(key string) (string, bool) {
  19  	value, ok := m[key]
  20  	return value, ok
  21  }
  22  
  23  type sourceFunc func(key string) (string, bool)
  24  
  25  func (fn sourceFunc) LookupEnv(key string) (string, bool) { return fn(key) }
  26