env.go 785 B

123456789101112131415161718192021222324252627282930
  1. package opts // import "github.com/docker/docker/opts"
  2. import (
  3. "os"
  4. "strings"
  5. "github.com/pkg/errors"
  6. )
  7. // ValidateEnv validates an environment variable and returns it.
  8. // If no value is specified, it obtains its value from the current environment
  9. //
  10. // As on ParseEnvFile and related to #16585, environment variable names
  11. // are not validate whatsoever, it's up to application inside docker
  12. // to validate them or not.
  13. //
  14. // The only validation here is to check if name is empty, per #25099
  15. func ValidateEnv(val string) (string, error) {
  16. k, _, ok := strings.Cut(val, "=")
  17. if k == "" {
  18. return "", errors.New("invalid environment variable: " + val)
  19. }
  20. if ok {
  21. return val, nil
  22. }
  23. if envVal, ok := os.LookupEnv(k); ok {
  24. return k + "=" + envVal, nil
  25. }
  26. return val, nil
  27. }