mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-14 19:27:58 +08:00
30 lines
550 B
Go
30 lines
550 B
Go
package vars
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
)
|
|
|
|
var errEnvMustBeString = errors.New("environment variable can only be set string values")
|
|
|
|
type envVariable struct {
|
|
name string
|
|
}
|
|
|
|
func (ev envVariable) Set(val interface{}) error {
|
|
if s, ok := val.(string); ok {
|
|
os.Setenv(ev.name, string(s))
|
|
return nil
|
|
}
|
|
return errEnvMustBeString
|
|
}
|
|
|
|
func (ev envVariable) Get() interface{} {
|
|
return string(os.Getenv(ev.name))
|
|
}
|
|
|
|
// FromEnv returns a Var corresponding to the named environment variable.
|
|
func FromEnv(name string) Var {
|
|
return envVariable{name}
|
|
}
|