github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/opts/env.go (about)

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