2018-03-03 12:54:20 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-09-03 13:48:39 +08:00
|
|
|
// ErrNonExistentEnvVar is raised by the get-env command when the environment
|
|
|
|
// variable does not exist.
|
|
|
|
var ErrNonExistentEnvVar = errors.New("non-existent environment variable")
|
2018-03-03 12:54:20 +08:00
|
|
|
|
|
|
|
func init() {
|
2022-03-20 23:50:25 +08:00
|
|
|
addBuiltinFns(map[string]any{
|
2018-03-03 12:54:20 +08:00
|
|
|
"has-env": hasEnv,
|
|
|
|
"get-env": getEnv,
|
|
|
|
"set-env": os.Setenv,
|
|
|
|
"unset-env": os.Unsetenv,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func hasEnv(key string) bool {
|
|
|
|
_, ok := os.LookupEnv(key)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func getEnv(key string) (string, error) {
|
|
|
|
value, ok := os.LookupEnv(key)
|
|
|
|
if !ok {
|
2020-09-03 13:48:39 +08:00
|
|
|
return "", ErrNonExistentEnvVar
|
2018-03-03 12:54:20 +08:00
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|