2018-02-15 16:59:49 +08:00
|
|
|
package vars
|
2018-01-05 06:54:04 +08:00
|
|
|
|
|
|
|
import (
|
2018-01-05 06:59:58 +08:00
|
|
|
"errors"
|
2018-01-05 06:54:04 +08:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2018-01-05 06:59:58 +08:00
|
|
|
var errEnvMustBeString = errors.New("environment variable can only be set string values")
|
|
|
|
|
2018-01-05 06:54:04 +08:00
|
|
|
type envVariable struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2018-01-30 01:39:41 +08:00
|
|
|
func (ev envVariable) Set(val interface{}) error {
|
2018-01-25 09:40:15 +08:00
|
|
|
if s, ok := val.(string); ok {
|
2019-07-20 20:55:36 +08:00
|
|
|
os.Setenv(ev.name, s)
|
2018-01-05 06:59:58 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errEnvMustBeString
|
2018-01-05 06:54:04 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 01:39:41 +08:00
|
|
|
func (ev envVariable) Get() interface{} {
|
2019-07-20 20:55:36 +08:00
|
|
|
return os.Getenv(ev.name)
|
2018-01-05 06:54:04 +08:00
|
|
|
}
|
|
|
|
|
2018-11-18 22:23:31 +08:00
|
|
|
// FromEnv returns a Var corresponding to the named environment variable.
|
|
|
|
func FromEnv(name string) Var {
|
2018-01-05 06:54:04 +08:00
|
|
|
return envVariable{name}
|
|
|
|
}
|