2016-12-23 22:09:12 +03:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
2020-08-14 17:38:09 +03:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2016-12-23 22:09:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateEnv validates an environment variable and returns it.
|
2020-08-14 17:38:09 +03:00
|
|
|
// If no value is specified, it obtains its value from the current environment
|
2016-12-23 22:09:12 +03:00
|
|
|
//
|
|
|
|
// As on ParseEnvFile and related to #16585, environment variable names
|
2020-08-14 17:38:09 +03:00
|
|
|
// are not validated, and it's up to the application inside the container
|
2016-12-23 22:09:12 +03:00
|
|
|
// to validate them or not.
|
|
|
|
//
|
|
|
|
// The only validation here is to check if name is empty, per #25099
|
|
|
|
func ValidateEnv(val string) (string, error) {
|
2022-12-27 18:24:23 +03:00
|
|
|
k, _, hasValue := strings.Cut(val, "=")
|
|
|
|
if k == "" {
|
2020-08-14 17:38:09 +03:00
|
|
|
return "", errors.New("invalid environment variable: " + val)
|
2016-12-23 22:09:12 +03:00
|
|
|
}
|
2022-12-27 18:24:23 +03:00
|
|
|
if hasValue {
|
|
|
|
// val contains a "=" (but value may be an empty string)
|
2016-12-23 22:09:12 +03:00
|
|
|
return val, nil
|
|
|
|
}
|
2022-12-27 18:24:23 +03:00
|
|
|
if envVal, ok := os.LookupEnv(k); ok {
|
|
|
|
return k + "=" + envVal, nil
|
2016-12-23 22:09:12 +03:00
|
|
|
}
|
2020-08-14 17:38:09 +03:00
|
|
|
return val, nil
|
2016-12-23 22:09:12 +03:00
|
|
|
}
|