bake: restore previous override merge behavior

For array fields, overrides are merged together
but override is not merged with the target. If merging
with target is desired we can add support for
overrides with += operator in the future.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
pull/783/head
Tonis Tiigi 3 years ago
parent 0e4d7aa7a9
commit 7a7b73c043

@ -31,8 +31,8 @@ type File struct {
} }
type Override struct { type Override struct {
Key string Value string
Value string ArrValue []string
} }
func defaultFilenames() []string { func defaultFilenames() []string {
@ -246,8 +246,8 @@ func (c Config) expandTargets(pattern string) ([]string, error) {
return names, nil return names, nil
} }
func (c Config) newOverrides(v []string) (map[string][]Override, error) { func (c Config) newOverrides(v []string) (map[string]map[string]Override, error) {
m := map[string][]Override{} m := map[string]map[string]Override{}
for _, v := range v { for _, v := range v {
parts := strings.SplitN(v, "=", 2) parts := strings.SplitN(v, "=", 2)
@ -269,16 +269,19 @@ func (c Config) newOverrides(v []string) (map[string][]Override, error) {
kk := strings.SplitN(parts[0], ".", 2) kk := strings.SplitN(parts[0], ".", 2)
for _, name := range names { for _, name := range names {
t := m[name] t, ok := m[name]
if !ok {
o := Override{ t = map[string]Override{}
Key: kk[1], m[name] = t
}
if len(parts) == 2 {
o.Value = parts[1]
} }
o := t[kk[1]]
switch keys[1] { switch keys[1] {
case "output", "cache-to", "cache-from", "tags", "platform", "secrets", "ssh":
if len(parts) == 2 {
o.ArrValue = append(o.ArrValue, parts[1])
}
case "args": case "args":
if len(keys) != 3 { if len(keys) != 3 {
return nil, errors.Errorf("invalid key %s, args requires name", parts[0]) return nil, errors.Errorf("invalid key %s, args requires name", parts[0])
@ -290,11 +293,14 @@ func (c Config) newOverrides(v []string) (map[string][]Override, error) {
} }
o.Value = v o.Value = v
} }
fallthrough
default:
if len(parts) == 2 {
o.Value = parts[1]
}
} }
t = append(t, o) t[kk[1]] = o
m[name] = t
} }
} }
return m, nil return m, nil
@ -326,7 +332,7 @@ func (c Config) group(name string, visited map[string]struct{}) []string {
return targets return targets
} }
func (c Config) ResolveTarget(name string, overrides map[string][]Override) (*Target, error) { func (c Config) ResolveTarget(name string, overrides map[string]map[string]Override) (*Target, error) {
t, err := c.target(name, map[string]struct{}{}, overrides) t, err := c.target(name, map[string]struct{}{}, overrides)
if err != nil { if err != nil {
return nil, err return nil, err
@ -342,7 +348,7 @@ func (c Config) ResolveTarget(name string, overrides map[string][]Override) (*Ta
return t, nil return t, nil
} }
func (c Config) target(name string, visited map[string]struct{}, overrides map[string][]Override) (*Target, error) { func (c Config) target(name string, visited map[string]struct{}, overrides map[string]map[string]Override) (*Target, error) {
if _, ok := visited[name]; ok { if _, ok := visited[name]; ok {
return nil, nil return nil, nil
} }
@ -476,10 +482,10 @@ func (t *Target) Merge(t2 *Target) {
t.Inherits = append(t.Inherits, t2.Inherits...) t.Inherits = append(t.Inherits, t2.Inherits...)
} }
func (t *Target) AddOverrides(overrides []Override) error { func (t *Target) AddOverrides(overrides map[string]Override) error {
for _, o := range overrides { for key, o := range overrides {
value := o.Value value := o.Value
keys := strings.SplitN(o.Key, ".", 2) keys := strings.SplitN(key, ".", 2)
switch keys[0] { switch keys[0] {
case "context": case "context":
t.Context = &value t.Context = &value
@ -503,21 +509,21 @@ func (t *Target) AddOverrides(overrides []Override) error {
} }
t.Labels[keys[1]] = value t.Labels[keys[1]] = value
case "tags": case "tags":
t.Tags = append(t.Tags, value) t.Tags = o.ArrValue
case "cache-from": case "cache-from":
t.CacheFrom = append(t.CacheFrom, value) t.CacheFrom = o.ArrValue
case "cache-to": case "cache-to":
t.CacheTo = append(t.CacheTo, value) t.CacheTo = o.ArrValue
case "target": case "target":
t.Target = &value t.Target = &value
case "secrets": case "secrets":
t.Secrets = append(t.Secrets, value) t.Secrets = o.ArrValue
case "ssh": case "ssh":
t.SSH = append(t.SSH, value) t.SSH = o.ArrValue
case "platform": case "platform":
t.Platforms = append(t.Platforms, value) t.Platforms = o.ArrValue
case "output": case "output":
t.Outputs = append(t.Outputs, value) t.Outputs = o.ArrValue
case "no-cache": case "no-cache":
noCache, err := strconv.ParseBool(value) noCache, err := strconv.ParseBool(value)
if err != nil { if err != nil {

Loading…
Cancel
Save