diff --git a/locate/install.go b/locate/install.go index acb6306d7..8a55c74c2 100644 --- a/locate/install.go +++ b/locate/install.go @@ -3,6 +3,8 @@ package locate import ( "flag" "strings" + + "github.com/google/uuid" ) func (po *LocateOptions) installGenericFlags(flags *flag.FlagSet) { @@ -41,6 +43,32 @@ func (po *LocateOptions) installGenericFlags(flags *flag.FlagSet) { } return nil }) + + flags.Func("parent", "filter by parent (repeat).", + func(v string) error { + for _, t := range strings.Split(v, ",") { + t = strings.TrimSpace(t) + if t != "" { + po.Filters.Roots = append(po.Filters.Roots, t) + } + } + return nil + }) + + flags.Func("seq", "filter by sequence (repeat).", + func(v string) error { + for _, t := range strings.Split(v, ",") { + t = strings.TrimSpace(t) + if t != "" { + seq, err := uuid.Parse(t) + if err != nil { + return err + } + po.Filters.Sequences = append(po.Filters.Sequences, seq) + } + } + return nil + }) } func (po *LocateOptions) InstallLocateFlags(flags *flag.FlagSet) { diff --git a/locate/locate.go b/locate/locate.go index 503c73bd6..624331f9c 100644 --- a/locate/locate.go +++ b/locate/locate.go @@ -7,6 +7,7 @@ import ( "time" "github.com/PlakarKorp/kloset/objects" + "github.com/google/uuid" ) type SortOrder int @@ -36,6 +37,8 @@ type ItemFilters struct { Types []string Origins []string Roots []string + Sequences []uuid.UUID + Parents []string } func (it *ItemFilters) HasTag(tag string) bool { @@ -86,6 +89,30 @@ func (it ItemFilters) HasRoot(root string) bool { return false } +func (it ItemFilters) HasSequence(seq uuid.UUID) bool { + if seq == uuid.Nil { + return true + } + for _, t := range it.Sequences { + if t == seq { + return true + } + } + return false +} + +func (it ItemFilters) HasParent(parent string) bool { + if parent == "" { + return true + } + for _, t := range it.Parents { + if strings.HasPrefix(t, parent) { + return true + } + } + return false +} + type Item struct { ItemID objects.MAC Timestamp time.Time @@ -113,11 +140,13 @@ type LocateFilters struct { Tags []string `json:"tags,omitempty" yaml:"tags,omitempty"` IgnoreTags []string `json:"ignore_tags,omitempty" yaml:"ignore_tags,omitempty"` - Latest bool `json:"latest,omitempty" yaml:"latest,omitempty"` // if true, consider only the latest matching item - IDs []string `json:"ids,omitempty" yaml:"ids,omitempty"` - Types []string `json:"types,omitempty" yaml:"types,omitempty"` - Origins []string `json:"origins,omitempty" yaml:"origins,omitempty"` - Roots []string `json:"roots,omitempty" yaml:"roots,omitempty"` + Latest bool `json:"latest,omitempty" yaml:"latest,omitempty"` // if true, consider only the latest matching item + IDs []string `json:"ids,omitempty" yaml:"ids,omitempty"` + Types []string `json:"types,omitempty" yaml:"types,omitempty"` + Origins []string `json:"origins,omitempty" yaml:"origins,omitempty"` + Roots []string `json:"roots,omitempty" yaml:"roots,omitempty"` + Sequences []uuid.UUID `json:"sequences,omitempty" yaml:"sequences,omitempty"` + Parents []string `json:"parents,omitempty" yaml:"parents,omitempty"` } type LocatePeriods struct { @@ -283,6 +312,16 @@ func (lo *LocateOptions) Matches(it Item) bool { return false } } + for _, seq := range lo.Filters.Sequences { + if !it.Filters.HasSequence(seq) { + return false + } + } + for _, parent := range lo.Filters.Parents { + if !it.Filters.HasParent(parent) { + return false + } + } return true } diff --git a/locate/snapshots.go b/locate/snapshots.go index 039e67db1..f87d5f8be 100644 --- a/locate/snapshots.go +++ b/locate/snapshots.go @@ -13,6 +13,7 @@ import ( "github.com/PlakarKorp/kloset/objects" "github.com/PlakarKorp/kloset/repository" "github.com/PlakarKorp/kloset/snapshot" + "github.com/google/uuid" ) // LocateSnapshotIDs preserves the legacy behavior using policy.FilterAndSort. @@ -93,10 +94,14 @@ func buildPolicyItems(repo *repository.Repository) []Item { roots := []string{} types := []string{} origins := []string{} + sequences := []uuid.UUID{} + parents := []string{} for _, src := range h.Sources { roots = append(roots, src.Importer.Directory) types = append(types, src.Importer.Type) origins = append(origins, src.Importer.Origin) + sequences = append(sequences, h.Sequence) + parents = append(parents, hex.EncodeToString(h.Parent[:])) } it := Item{ @@ -112,6 +117,8 @@ func buildPolicyItems(repo *repository.Repository) []Item { Types: types, Origins: origins, Roots: roots, + Sequences: sequences, + Parents: parents, }, } diff --git a/snapshot/header/header.go b/snapshot/header/header.go index d40498794..4fd76c904 100644 --- a/snapshot/header/header.go +++ b/snapshot/header/header.go @@ -93,6 +93,8 @@ type Header struct { Tags []string `msgpack:"tags" json:"tags"` Context []KeyValue `msgpack:"context" json:"context"` Sources []Source `msgpack:"sources" json:"sources"` + Sequence uuid.UUID `msgpack:"sequence" json:"sequence"` + Parent objects.MAC `msgpack:"parent" json:"parent"` } func NewHeader(name string, identifier objects.MAC) *Header {