2018-03-03 12:54:20 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2018-03-03 12:59:32 +08:00
|
|
|
var errNonExistentEnvVar = errors.New("non-existent environment variable")
|
2018-03-03 12:54:20 +08:00
|
|
|
|
2020-01-18 21:12:50 +08:00
|
|
|
//elvdoc:fn set-env
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// set-env $name $value
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Sets an environment variable to the given value. Example:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> set-env X foobar
|
|
|
|
// ~> put $E:X
|
|
|
|
// ▶ foobar
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// @cf get-env has-env unset-env
|
|
|
|
|
|
|
|
//elvdoc:fn unset-env
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// unset-env $name
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Unset an environment variable. Example:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> E:X = foo
|
|
|
|
// ~> unset-env X
|
|
|
|
// ~> has-env X
|
|
|
|
// ▶ $false
|
|
|
|
// ~> put $E:X
|
|
|
|
// ▶ ''
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// @cf has-env get-env set-env
|
|
|
|
|
2018-03-03 12:54:20 +08:00
|
|
|
func init() {
|
|
|
|
addBuiltinFns(map[string]interface{}{
|
|
|
|
"has-env": hasEnv,
|
|
|
|
"get-env": getEnv,
|
|
|
|
"set-env": os.Setenv,
|
|
|
|
"unset-env": os.Unsetenv,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
//elvdoc:fn has-env
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// has-env $name
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Test whether an environment variable exists. Examples:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> has-env PATH
|
|
|
|
// ▶ $true
|
|
|
|
// ~> has-env NO_SUCH_ENV
|
|
|
|
// ▶ $false
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// @cf get-env set-env unset-env
|
|
|
|
|
2018-03-03 12:54:20 +08:00
|
|
|
func hasEnv(key string) bool {
|
|
|
|
_, ok := os.LookupEnv(key)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
//elvdoc:fn get-env
|
|
|
|
//
|
|
|
|
// ```elvish
|
|
|
|
// get-env $name
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// Gets the value of an environment variable. Throws an exception if the
|
|
|
|
// environment variable does not exist. Examples:
|
|
|
|
//
|
|
|
|
// ```elvish-transcript
|
|
|
|
// ~> get-env LANG
|
|
|
|
// ▶ zh_CN.UTF-8
|
|
|
|
// ~> get-env NO_SUCH_ENV
|
|
|
|
// Exception: non-existent environment variable
|
|
|
|
// [tty], line 1: get-env NO_SUCH_ENV
|
|
|
|
// ```
|
|
|
|
//
|
|
|
|
// @cf has-env set-env unset-env
|
|
|
|
|
2018-03-03 12:54:20 +08:00
|
|
|
func getEnv(key string) (string, error) {
|
|
|
|
value, ok := os.LookupEnv(key)
|
|
|
|
if !ok {
|
2018-03-03 13:00:42 +08:00
|
|
|
return "", errNonExistentEnvVar
|
2018-03-03 12:54:20 +08:00
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|