config/env.go
func (c *EnvConfig) Env() []string {
	// In pristine mode, just return the custom environment. If the user did not
	// specify a custom environment, just return the empty slice to force an
	// empty environment. We cannot return nil here because the later call to
	// os/exec will think we want to inherit the parent.
	if BoolVal(c.Pristine) {
		if len(c.Custom) > 0 {
			return c.Custom
		}
		return []string{}
	}

	// Pull all the key-value pairs out of the environment
	environ := os.Environ()
	keys := make([]string, len(environ))
	env := make(map[string]string, len(environ))
	for i, v := range environ {
		list := strings.SplitN(v, "=", 2)
		keys[i] = list[0]
		env[list[0]] = list[1]
	}

	// anyGlobMatch is a helper function which checks if any of the given globs
	// match the string.
	anyGlobMatch := func(s string, patterns []string) bool {
		for _, pattern := range patterns {
			if matched, _ := filepath.Match(pattern, s); matched {
				return true
			}
		}
		return false
	}

	// Pull out any envvars that match the whitelist.
	if len(c.Whitelist) > 0 {
		newKeys := make([]string, 0, len(keys))
		for _, k := range keys {
			if anyGlobMatch(k, c.Whitelist) {
				newKeys = append(newKeys, k)
			}
		}
		keys = newKeys
	}

	// Remove any envvars that match the blacklist.
	if len(c.Blacklist) > 0 {
		newKeys := make([]string, 0, len(keys))
		for _, k := range keys {
			if !anyGlobMatch(k, c.Blacklist) {
				newKeys = append(newKeys, k)
			}
		}
		keys = newKeys
	}

	// Build the final list using only the filtered keys.
	finalEnv := make([]string, 0, len(keys)+len(c.Custom))
	for _, k := range keys {
		finalEnv = append(finalEnv, k+"="+env[k])
	}

	// Append remaining custom environment.
	finalEnv = append(finalEnv, c.Custom...)

	return finalEnv
}
func (c *EnvConfig) Env() []string {
	// In pristine mode, just return the custom environment. If the user did not
	// specify a custom environment, just return the empty slice to force an
	// empty environment. We cannot return nil here because the later call to
	// os/exec will think we want to inherit the parent.
	if BoolVal(c.Pristine) {
		if len(c.Custom) > 0 {
			return c.Custom
		}
		return []string{}
	}

	// Pull all the key-value pairs out of the environment
	environ := os.Environ()
	keys := make([]string, len(environ))
	env := make(map[string]string, len(environ))
	for i, v := range environ {
		list := strings.SplitN(v, "=", 2)
		keys[i] = list[0]
		env[list[0]] = list[1]
	}

	// anyGlobMatch is a helper function which checks if any of the given globs
	// match the string.
	anyGlobMatch := func(s string, patterns []string) bool {
		for _, pattern := range patterns {
			if matched, _ := filepath.Match(pattern, s); matched {
				return true
			}
		}
		return false
	}

	// Pull out any envvars that match the whitelist.
	if len(c.Whitelist) > 0 {
		newKeys := make([]string, 0, len(keys))
		for _, k := range keys {
			if anyGlobMatch(k, c.Whitelist) {
				newKeys = append(newKeys, k)
			}
		}
		keys = newKeys
	}

	// Remove any envvars that match the blacklist.
	if len(c.Blacklist) > 0 {
		newKeys := make([]string, 0, len(keys))
		for _, k := range keys {
			if !anyGlobMatch(k, c.Blacklist) {
				newKeys = append(newKeys, k)
			}
		}
		keys = newKeys
	}

	// Build the final list using only the filtered keys.
	finalEnv := make([]string, 0, len(keys)+len(c.Custom))
	for _, k := range keys {
		finalEnv = append(finalEnv, k+"="+env[k])
	}

	// Append remaining custom environment.
	finalEnv = append(finalEnv, c.Custom...)

	return finalEnv
}
func ( c * EnvConfig ) Env ( ) [ ] string { // In pristine mode, just return the custom environment. If the user did not // specify a custom environment, just return the empty slice to force an // empty environment. We cannot return nil here because the later call to // os/exec will think we want to inherit the parent. if BoolVal ( c . Pristine ) { if len ( c . Custom ) > 0 { return c . Custom } return [ ] string { } } // Pull all the key-value pairs out of the environment environ := os . Environ ( ) keys := make ( [ ] string , len ( environ ) ) env := make ( map [ string ] string , len ( environ ) ) for i , v := range environ { list := strings . SplitN ( v , " " , 2 ) keys [ i ] = list [ 0 ] env [ list [ 0 ] ] = list [ 1 ] } // anyGlobMatch is a helper function which checks if any of the given globs // match the string. anyGlobMatch := func ( s string , patterns [ ] string ) bool { for _ , pattern := range patterns { if matched , _ := filepath . Match ( pattern , s ) ; matched { return true } } return false } // Pull out any envvars that match the whitelist. if len ( c . Whitelist ) > 0 { newKeys := make ( [ ] string , 0 , len ( keys ) ) for _ , k := range keys { if anyGlobMatch ( k , c . Whitelist ) { newKeys = append ( newKeys , k ) } } keys = newKeys } // Remove any envvars that match the blacklist. if len ( c . Blacklist ) > 0 { newKeys := make ( [ ] string , 0 , len ( keys ) ) for _ , k := range keys { if ! anyGlobMatch ( k , c . Blacklist ) { newKeys = append ( newKeys , k ) } } keys = newKeys } // Build the final list using only the filtered keys. finalEnv := make ( [ ] string , 0 , len ( keys ) + len ( c . Custom ) ) for _ , k := range keys { finalEnv = append ( finalEnv , k + " " + env [ k ] ) } // Append remaining custom environment. finalEnv = append ( finalEnv , c . Custom ... ) return finalEnv }
// Env calculates and returns the finalized environment for this exec
// configuration. It takes into account pristine, custom environment, whitelist,
// and blacklist values.
Env calculates and returns the finalized environment for this exec configuration . It takes into account pristine custom environment whitelist and blacklist values .