Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions locate/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package locate
import (
"flag"
"strings"

"github.com/google/uuid"
)

func (po *LocateOptions) installGenericFlags(flags *flag.FlagSet) {
Expand Down Expand Up @@ -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) {
Expand Down
49 changes: 44 additions & 5 deletions locate/locate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/PlakarKorp/kloset/objects"
"github.com/google/uuid"
)

type SortOrder int
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 7 additions & 0 deletions locate/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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{
Expand All @@ -112,6 +117,8 @@ func buildPolicyItems(repo *repository.Repository) []Item {
Types: types,
Origins: origins,
Roots: roots,
Sequences: sequences,
Parents: parents,
},
}

Expand Down
2 changes: 2 additions & 0 deletions snapshot/header/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading