github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/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 parts := strings.SplitN(e, "=", 2) 13 cache[parts[0]] = i 14 } 15 16 for _, value := range overrides { 17 // Values w/o = means they want this env to be removed/unset. 18 if !strings.Contains(value, "=") { 19 if i, exists := cache[value]; exists { 20 defaults[i] = "" // Used to indicate it should be removed 21 } 22 continue 23 } 24 25 // Just do a normal set/update 26 parts := strings.SplitN(value, "=", 2) 27 if i, exists := cache[parts[0]]; 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 }