Skip to content

Commit

Permalink
Consider child layers/contexts in GetAllImages()
Browse files Browse the repository at this point in the history
  • Loading branch information
silphid committed Jul 15, 2021
1 parent c7dd0b0 commit 0e4e25c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
28 changes: 28 additions & 0 deletions src/internal/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package yey

import (
"fmt"
"sort"
"strings"

"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -139,6 +140,33 @@ func (c Context) getContextRecursively(names []string) (Context, []string, error
return ctx, names, nil
}

// GetAllImages returns the list of image names referenced in context recursively
func (c Context) GetAllImages() []string {
namesMap := make(map[string]struct{})

if c.Image != "" {
namesMap[c.Image] = struct{}{}
}

// Recurse into all child layers/contexts
for _, layer := range c.Layers {
for _, ctx := range layer.Contexts {
childImages := ctx.GetAllImages()
for _, childImage := range childImages {
namesMap[childImage] = struct{}{}
}
}
}

// Sort
sortedNames := make([]string, 0, len(namesMap))
for name := range namesMap {
sortedNames = append(sortedNames, name)
}
sort.Strings(sortedNames)
return sortedNames
}

// String returns a user-friendly yaml representation of this context
func (c Context) String() string {
buf, err := yaml.Marshal(c)
Expand Down
31 changes: 0 additions & 31 deletions src/internal/contexts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package yey

import (
"sort"
)

// Contexts represents a combinaison of base and named contexts
type Contexts struct {
Path string
Expand All @@ -16,30 +12,3 @@ func (c Contexts) Merge(source Contexts) Contexts {
Context: c.Context.Merge(source.Context),
}
}

// GetAllImages returns the list of image names referenced in all contexts
func (c Contexts) GetAllImages() []string {
namesMap := make(map[string]struct{})

if c.Context.Image != "" {
namesMap[c.Context.Image] = struct{}{}
}

for _, layer := range c.Layers {
for _, ctx := range layer.Contexts {
if ctx.Image != "" {
namesMap[ctx.Image] = struct{}{}
}
}
}

// TODO: consider nested layers

// Sort
sortedNames := make([]string, 0, len(namesMap))
for name := range namesMap {
sortedNames = append(sortedNames, name)
}
sort.Strings(sortedNames)
return sortedNames
}

0 comments on commit 0e4e25c

Please sign in to comment.