MCPcopy Index your code
hub / github.com/coder/coder / MergeExtraEnvs

Function MergeExtraEnvs

coderd/provisionerdserver/provisionerdserver.go:3471–3504  ·  view source on GitHub ↗

MergeExtraEnvs applies extra environment variables to the given map, respecting the merge_strategy field on each env. When merge_strategy is empty or "replace", the value overwrites any existing entry. "append" and "prepend" join values with a ":" separator (PATH-style). "error" causes a failure if

(env map[string]string, extraEnvs []*sdkproto.Env)

Source from the content-addressed store, hash-verified

3469// "append" and "prepend" join values with a ":" separator (PATH-style).
3470// "error" causes a failure if the key already exists.
3471func MergeExtraEnvs(env map[string]string, extraEnvs []*sdkproto.Env) error {
3472 for _, e := range extraEnvs {
3473 strategy := e.GetMergeStrategy()
3474 if strategy == "" {
3475 strategy = "replace"
3476 }
3477 existing, exists := env[e.GetName()]
3478 switch strategy {
3479 case "error":
3480 if exists {
3481 return xerrors.Errorf(
3482 "duplicate env var %q: merge_strategy is %q but variable is already defined",
3483 e.GetName(), strategy,
3484 )
3485 }
3486 env[e.GetName()] = e.GetValue()
3487 case "append":
3488 if exists && existing != "" {
3489 env[e.GetName()] = existing + ":" + e.GetValue()
3490 } else {
3491 env[e.GetName()] = e.GetValue()
3492 }
3493 case "prepend":
3494 if exists && existing != "" {
3495 env[e.GetName()] = e.GetValue() + ":" + existing
3496 } else {
3497 env[e.GetName()] = e.GetValue()
3498 }
3499 default: // "replace"
3500 env[e.GetName()] = e.GetValue()
3501 }
3502 }
3503 return nil
3504}
3505
3506func encodeSubagentEnvs(envs []*sdkproto.Env) (pqtype.NullRawMessage, error) {
3507 if len(envs) == 0 {

Callers 3

TestMergeExtraEnvsFunction · 0.92
InsertWorkspaceResourceFunction · 0.85
encodeSubagentEnvsFunction · 0.85

Calls 4

GetMergeStrategyMethod · 0.80
GetNameMethod · 0.45
ErrorfMethod · 0.45
GetValueMethod · 0.45

Tested by 1

TestMergeExtraEnvsFunction · 0.74