github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/container/env.go (about) 1 package container // import "github.com/docker/docker/container" 2 3 import ( 4 "strings" 5 ) 6 7 // ReplaceOrAppendEnvValues returns the defaults with the overrides either 8 // replaced by env key or appended to the list 9 func ReplaceOrAppendEnvValues(defaults, overrides []string) []string { 10 cache := make(map[string]int, len(defaults)) 11 for i, e := range defaults { 12 index := strings.Index(e, "=") 13 cache[e[:index]] = i 14 } 15 16 for _, value := range overrides { 17 // Values w/o = means they want this env to be removed/unset. 18 index := strings.Index(value, "=") 19 if index < 0 { 20 // no "=" in value 21 if i, exists := cache[value]; exists { 22 defaults[i] = "" // Used to indicate it should be removed 23 } 24 continue 25 } 26 27 if i, exists := cache[value[:index]]; exists { 28 defaults[i] = value 29 } else { 30 defaults = append(defaults, value) 31 } 32 } 33 34 // Now remove all entries that we want to "unset" 35 for i := 0; i < len(defaults); i++ { 36 if defaults[i] == "" { 37 defaults = append(defaults[:i], defaults[i+1:]...) 38 i-- 39 } 40 } 41 42 return defaults 43 }