Skip to content

Commit

Permalink
Fix context merging bug.
Browse files Browse the repository at this point in the history
Make contexts field private.
Other minor changes and clean-up.
  • Loading branch information
silphid committed May 18, 2021
1 parent 1ad2dab commit 5b5d8c5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
7 changes: 1 addition & 6 deletions src/internal/context.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package yey

// Context represents an execution context for yey (env vars and volumes)
// Context represents execution configuration for some docker container
type Context struct {
Image string
Env map[string]string
Mounts map[string]string
}

var None = Context{
Env: make(map[string]string),
Mounts: make(map[string]string),
}

// Clone returns a deep-copy of this context
func (c Context) Clone() Context {
clone := c
Expand Down
27 changes: 14 additions & 13 deletions src/internal/contexts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ const (
BaseContextName = "base"
)

// Contexts represents yey's current config persisted to disk
// Contexts represents a combinaison of base and named contexts
type Contexts struct {
Context `yaml:",inline"`
Contexts map[string]Context
contexts map[string]Context
}

// Clone returns a deep-copy of this context
// Clone returns a deep-copy of this object
func (c Contexts) Clone() Contexts {
clone := c
for key, value := range c.Contexts {
clone.Contexts[key] = value.Clone()
for key, value := range c.contexts {
clone.contexts[key] = value.Clone()
}
return clone
}
Expand All @@ -28,12 +28,12 @@ func (c Contexts) Clone() Contexts {
func (c Contexts) Merge(source Contexts) Contexts {
clone := c.Clone()
clone.Context.Merge(source.Context)
for key, value := range c.Contexts {
existing, ok := c.Contexts[key]
for key, value := range source.contexts {
existing, ok := clone.contexts[key]
if ok {
c.Contexts[key] = existing.Merge(value)
c.contexts[key] = existing.Merge(value)
} else {
c.Contexts[key] = value
c.contexts[key] = value
}
}
return clone
Expand All @@ -44,7 +44,7 @@ func (c Contexts) Merge(source Contexts) Contexts {
func (c Contexts) GetContextNames() ([]string, error) {
// Extract unique names
namesMap := make(map[string]bool)
for name := range c.Contexts {
for name := range c.contexts {
namesMap[name] = true
}

Expand All @@ -66,12 +66,13 @@ func (c Contexts) GetContextNames() ([]string, error) {
// GetContext returns context with given name, or base context
// if name is "base".
func (c Contexts) GetContext(name string) (Context, error) {
base := c.Context
if name == BaseContextName {
return c.Context, nil
return base, nil
}
context, ok := c.Contexts[name]
context, ok := c.contexts[name]
if !ok {
return Context{}, fmt.Errorf("context %q not found", name)
}
return context, nil
return base.Merge(context), nil
}
2 changes: 1 addition & 1 deletion src/internal/contexts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func loadContexts(baseFile, ctx1Key, ctx1File, ctx2Key, ctx2File string) Contexts {
return Contexts{
Context: loadContext(baseFile),
Contexts: map[string]Context{
contexts: map[string]Context{
ctx1Key: loadContext(ctx1File),
ctx2Key: loadContext(ctx2File),
},
Expand Down

0 comments on commit 5b5d8c5

Please sign in to comment.