2016-12-23 14:09:12 -05:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
2020-08-14 10:38:09 -04:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2016-12-23 14:09:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidateEnv validates an environment variable and returns it.
|
2020-08-14 10:38:09 -04:00
|
|
|
// If no value is specified, it obtains its value from the current environment
|
2016-12-23 14:09:12 -05:00
|
|
|
//
|
|
|
|
// As on ParseEnvFile and related to #16585, environment variable names
|
2020-08-14 10:38:09 -04:00
|
|
|
// are not validated, and it's up to the application inside the container
|
2016-12-23 14:09:12 -05: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 10:24:23 -05:00
|
|
|
k, _, hasValue := strings.Cut(val, "=")
|
|
|
|
if k == "" {
|
2020-08-14 10:38:09 -04:00
|
|
|
return "", errors.New("invalid environment variable: " + val)
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
if hasValue {
|
|
|
|
// val contains a "=" (but value may be an empty string)
|
2016-12-23 14:09:12 -05:00
|
|
|
return val, nil
|
|
|
|
}
|
2022-12-27 10:24:23 -05:00
|
|
|
if envVal, ok := os.LookupEnv(k); ok {
|
|
|
|
return k + "=" + envVal, nil
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|
2020-08-14 10:38:09 -04:00
|
|
|
return val, nil
|
2016-12-23 14:09:12 -05:00
|
|
|
}
|