Ejemplo n.º 1
0
// MergeEnv merges vars with the variables in env taking care to concatenate
// values as per the concat and ignore parameters similarly to SetEnvFromProfiles.
func MergeEnv(concat map[string]string, ignore map[string]bool, env *envvar.Vars, vars ...[]string) {
	for _, ev := range vars {
		for _, tmp := range ev {
			k, v := envvar.SplitKeyValue(tmp)
			if ignore[k] {
				continue
			}
			if sep := concat[k]; len(sep) > 0 {
				ov := env.GetTokens(k, sep)
				nv := envvar.SplitTokens(v, sep)
				env.SetTokens(k, append(ov, nv...), " ")
				continue
			}
			env.Set(k, v)
		}
	}
}
Ejemplo n.º 2
0
// MergeEnv merges environment variables in base with those
// in vars according to the suppled policies.
func MergeEnv(policies map[string]MergePolicy, base *envvar.Vars, vars ...[]string) {
	// Remove any variables that have the IgnoreBase policy.
	for k, _ := range base.ToMap() {
		switch policies[k].Action {
		case Ignore, IgnoreBaseAndAppend, IgnoreBaseAndPrepend, IgnoreBaseAndUseFirst, IgnoreBaseAndUseLast:
			base.Delete(k)
		}
	}
	for _, ev := range vars {
		for _, tmp := range ev {
			k, v := envvar.SplitKeyValue(tmp)
			policy := policies[k]
			action := policy.Action
			switch policy.Action {
			case IgnoreBaseAndAppend:
				action = Append
			case IgnoreBaseAndPrepend:
				action = Prepend
			case IgnoreBaseAndUseLast:
				action = Last
			case IgnoreBaseAndUseFirst:
				action = First
			}
			switch action {
			case Ignore, IgnoreProfiles:
				continue
			case Append, Prepend:
				sep := policy.Separator
				ov := base.GetTokens(k, sep)
				nv := envvar.SplitTokens(v, sep)
				if action == Append {
					base.SetTokens(k, append(ov, nv...), sep)
				} else {
					base.SetTokens(k, append(nv, ov...), sep)
				}
			case First:
				if !base.Contains(k) {
					base.Set(k, v)
				}
			case Last:
				base.Set(k, v)
			}
		}
	}
}
Ejemplo n.º 3
0
// SetEnvFromProfiles populates the embedded environment with the environment
// variables stored in the specified profiles for the specified target if
// new-style profiles are being used, otherwise it uses compiled in values as per
// the original profiles implementation.
// The profiles parameter contains a comma separated list of profile names; if the
// requested target does not exist for any of these profiles then those profiles
// will be ignored. The 'concat' parameter includes a map of variable names
// whose values are to concatenated with any existing ones rather than
// overwriting them (e.g. CFLAGS for example). The value of the concat map
// is the separator to use for that environment  variable (e.g. space for
// CFLAGs or ':' for PATH-like ones).
func (ch *ConfigHelper) SetEnvFromProfiles(concat map[string]string, ignore map[string]bool, profiles string, target Target) {
	if ch.profilesMode || ch.legacyMode {
		return
	}
	for _, profile := range strings.Split(profiles, ",") {
		t := LookupProfileTarget(profile, target)
		if t == nil {
			continue
		}
		for _, tmp := range t.Env.Vars {
			k, v := envvar.SplitKeyValue(tmp)
			if ignore[k] {
				continue
			}
			if sep := concat[k]; len(sep) > 0 {
				ov := ch.Vars.GetTokens(k, sep)
				nv := envvar.SplitTokens(v, sep)
				ch.Vars.SetTokens(k, append(ov, nv...), " ")
				continue
			}
			ch.Vars.Set(k, v)
		}
	}
}