Skip to content

Commit

Permalink
feat(collection): expand in collection
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Dec 26, 2024
1 parent 2a5c3e5 commit 1f9bc63
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package registry
import (
"context"
"fmt"
"sort"

"github.com/mb0/glob"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -223,6 +224,9 @@ func ExpandNames(names []string) []string {
expandedNames = append(expandedNames, matches...)
}

// Ensure predictable order
sort.Strings(expandedNames)

return expandedNames
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/types/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@
// to a resource.
package types

import (
"github.com/mb0/glob"
)

// Collection is a collection of strings
type Collection []string

// Expand returns a collection by using the Collection which may contain glob patterns and match to the source
// and returns the expanded collection, if there are no matches, it includes the original element from the collection.
func (c Collection) Expand(base []string) Collection {
var expanded Collection
for _, sc := range c {
matches, _ := glob.GlobStrings(base, sc)

if matches == nil {
expanded = append(expanded, sc)
continue
}

expanded = append(expanded, matches...)
}

return expanded
}

// Intersect returns the intersection of two collections
func (c Collection) Intersect(o Collection) Collection {
mo := o.toMap()
Expand Down
12 changes: 8 additions & 4 deletions pkg/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,32 @@ func ResolveResourceTypes(
base Collection,
includes, excludes, alternatives []Collection,
alternativeMappings map[string]string) Collection {

Check failure on line 11 in pkg/types/utils.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

// Loop over the alternatives and build a list of the old style resource types
for _, cl := range alternatives {
expandedCl := cl.Expand(base)
oldStyle := Collection{}
for _, c := range cl {
for _, c := range expandedCl {
os, found := alternativeMappings[c]
if found {
oldStyle = append(oldStyle, os)
}
}

base = base.Union(cl)
base = base.Union(expandedCl)
base = base.Remove(oldStyle)
}

for _, i := range includes {
expandedI := i.Expand(base)
if len(i) > 0 {
base = base.Intersect(i)
base = base.Intersect(expandedI)
}
}

for _, e := range excludes {
base = base.Remove(e)
expandedE := e.Expand(base)
base = base.Remove(expandedE)
}

return base
Expand Down

0 comments on commit 1f9bc63

Please sign in to comment.