builtin_fn_env.go 591 B

1234567891011121314151617181920212223242526272829303132
  1. package eval
  2. import (
  3. "errors"
  4. "os"
  5. )
  6. // ErrNonExistentEnvVar is raised by the get-env command when the environment
  7. // variable does not exist.
  8. var ErrNonExistentEnvVar = errors.New("non-existent environment variable")
  9. func init() {
  10. addBuiltinFns(map[string]any{
  11. "has-env": hasEnv,
  12. "get-env": getEnv,
  13. "set-env": os.Setenv,
  14. "unset-env": os.Unsetenv,
  15. })
  16. }
  17. func hasEnv(key string) bool {
  18. _, ok := os.LookupEnv(key)
  19. return ok
  20. }
  21. func getEnv(key string) (string, error) {
  22. value, ok := os.LookupEnv(key)
  23. if !ok {
  24. return "", ErrNonExistentEnvVar
  25. }
  26. return value, nil
  27. }