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
|
|
|
// envVariable represents an environment variable.
|
|
|
|
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 {
|
2018-01-05 06:59:58 +08:00
|
|
|
os.Setenv(ev.name, string(s))
|
|
|
|
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{} {
|
2018-01-25 09:40:15 +08:00
|
|
|
return string(os.Getenv(ev.name))
|
2018-01-05 06:54:04 +08:00
|
|
|
}
|
|
|
|
|
2018-01-05 06:59:58 +08:00
|
|
|
// NewEnv returns an environment variable.
|
2018-03-08 21:20:31 +08:00
|
|
|
func NewEnv(name string) Var {
|
2018-01-05 06:54:04 +08:00
|
|
|
return envVariable{name}
|
|
|
|
}
|