mirror of
https://github.com/anchore/syft.git
synced 2025-11-18 00:43:20 +01:00
fix: comma separated selectors in cataloger list command (#3804)
Signed-off-by: Keith Zantow <kzantow@gmail.com>
This commit is contained in:
parent
a5da154327
commit
b13ffdd304
@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/anchore/clio"
|
"github.com/anchore/clio"
|
||||||
|
"github.com/anchore/syft/cmd/syft/internal/options"
|
||||||
"github.com/anchore/syft/internal/bus"
|
"github.com/anchore/syft/internal/bus"
|
||||||
"github.com/anchore/syft/internal/task"
|
"github.com/anchore/syft/internal/task"
|
||||||
"github.com/anchore/syft/syft/cataloging"
|
"github.com/anchore/syft/syft/cataloging"
|
||||||
@ -86,11 +87,13 @@ func runCatalogerList(opts *catalogerListOptions) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func catalogerListReport(opts *catalogerListOptions, allTaskGroups [][]task.Task) (string, error) {
|
func catalogerListReport(opts *catalogerListOptions, allTaskGroups [][]task.Task) (string, error) {
|
||||||
|
defaultCatalogers := options.Flatten(opts.DefaultCatalogers)
|
||||||
|
selectCatalogers := options.Flatten(opts.SelectCatalogers)
|
||||||
selectedTaskGroups, selectionEvidence, err := task.SelectInGroups(
|
selectedTaskGroups, selectionEvidence, err := task.SelectInGroups(
|
||||||
allTaskGroups,
|
allTaskGroups,
|
||||||
cataloging.NewSelectionRequest().
|
cataloging.NewSelectionRequest().
|
||||||
WithDefaults(opts.DefaultCatalogers...).
|
WithDefaults(defaultCatalogers...).
|
||||||
WithExpression(opts.SelectCatalogers...),
|
WithExpression(selectCatalogers...),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("unable to select catalogers: %w", err)
|
return "", fmt.Errorf("unable to select catalogers: %w", err)
|
||||||
@ -99,7 +102,7 @@ func catalogerListReport(opts *catalogerListOptions, allTaskGroups [][]task.Task
|
|||||||
|
|
||||||
switch opts.Output {
|
switch opts.Output {
|
||||||
case "json":
|
case "json":
|
||||||
report, err = renderCatalogerListJSON(flattenTaskGroups(selectedTaskGroups), selectionEvidence, opts.DefaultCatalogers, opts.SelectCatalogers)
|
report, err = renderCatalogerListJSON(flattenTaskGroups(selectedTaskGroups), selectionEvidence, defaultCatalogers, selectCatalogers)
|
||||||
case "table", "":
|
case "table", "":
|
||||||
if opts.ShowHidden {
|
if opts.ShowHidden {
|
||||||
report = renderCatalogerListTables(allTaskGroups, selectionEvidence)
|
report = renderCatalogerListTables(allTaskGroups, selectionEvidence)
|
||||||
|
|||||||
@ -184,11 +184,11 @@ Default selections: 1
|
|||||||
• 'image'
|
• 'image'
|
||||||
Selection expressions: 6
|
Selection expressions: 6
|
||||||
• 'b' (intersect)
|
• 'b' (intersect)
|
||||||
• '-directory' (remove)
|
|
||||||
• '-c' (remove)
|
• '-c' (remove)
|
||||||
|
• '-directory' (remove)
|
||||||
• '-file' (remove)
|
• '-file' (remove)
|
||||||
• '+task3' (add)
|
|
||||||
• '+file-task1' (add)
|
• '+file-task1' (add)
|
||||||
|
• '+task3' (add)
|
||||||
┌────────────────┬──────────────────┐
|
┌────────────────┬──────────────────┐
|
||||||
│ FILE CATALOGER │ TAGS │
|
│ FILE CATALOGER │ TAGS │
|
||||||
├────────────────┼──────────────────┤
|
├────────────────┼──────────────────┤
|
||||||
@ -200,6 +200,43 @@ Selection expressions: 6
|
|||||||
│ task1 │ 1, a, b, image │
|
│ task1 │ 1, a, b, image │
|
||||||
│ task3 │ 3, c, d, directory │
|
│ task3 │ 3, c, d, directory │
|
||||||
└───────────────────┴────────────────────┘
|
└───────────────────┴────────────────────┘
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with comma separated expressions, table",
|
||||||
|
options: func() *catalogerListOptions {
|
||||||
|
c := defaultCatalogerListOptions()
|
||||||
|
c.Output = "table"
|
||||||
|
c.DefaultCatalogers = []string{
|
||||||
|
"image,task3,file-task1",
|
||||||
|
}
|
||||||
|
c.SelectCatalogers = []string{
|
||||||
|
"-task2,c,b",
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}(),
|
||||||
|
want: `
|
||||||
|
Default selections: 4
|
||||||
|
• 'file-task1'
|
||||||
|
• 'image'
|
||||||
|
• 'task3'
|
||||||
|
• 'file'
|
||||||
|
Selection expressions: 3
|
||||||
|
• 'b' (intersect)
|
||||||
|
• 'c' (intersect)
|
||||||
|
• '-task2' (remove)
|
||||||
|
┌────────────────┬──────────────────┐
|
||||||
|
│ FILE CATALOGER │ TAGS │
|
||||||
|
├────────────────┼──────────────────┤
|
||||||
|
│ file-task1 │ file, ft, ft-1-b │
|
||||||
|
│ file-task2 │ file, ft, ft-2-b │
|
||||||
|
└────────────────┴──────────────────┘
|
||||||
|
┌───────────────────┬────────────────────┐
|
||||||
|
│ PACKAGE CATALOGER │ TAGS │
|
||||||
|
├───────────────────┼────────────────────┤
|
||||||
|
│ task1 │ 1, a, b, image │
|
||||||
|
│ task3 │ 3, c, d, directory │
|
||||||
|
└───────────────────┴────────────────────┘
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -219,7 +256,7 @@ Selection expressions: 6
|
|||||||
return c
|
return c
|
||||||
}(),
|
}(),
|
||||||
want: `
|
want: `
|
||||||
{"default":["image"],"selection":["-directory","+task3","-c","b"],"catalogers":[{"name":"file-task1","tags":["file"]},{"name":"file-task2","tags":["file"]},{"name":"task1","tags":["b","image"]},{"name":"task3","tags":["task3"]}]}
|
{"default":["image"],"selection":["+task3","-c","-directory","b"],"catalogers":[{"name":"file-task1","tags":["file"]},{"name":"file-task2","tags":["file"]},{"name":"task1","tags":["b","image"]},{"name":"task3","tags":["task3"]}]}
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@ -272,12 +272,12 @@ func (cfg *Catalog) PostLoad() error {
|
|||||||
return fmt.Errorf("cannot use both 'catalogers' and 'select-catalogers'/'default-catalogers' flags")
|
return fmt.Errorf("cannot use both 'catalogers' and 'select-catalogers'/'default-catalogers' flags")
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.From = flatten(cfg.From)
|
cfg.From = Flatten(cfg.From)
|
||||||
|
|
||||||
cfg.Catalogers = flatten(cfg.Catalogers)
|
cfg.Catalogers = Flatten(cfg.Catalogers)
|
||||||
cfg.DefaultCatalogers = flatten(cfg.DefaultCatalogers)
|
cfg.DefaultCatalogers = Flatten(cfg.DefaultCatalogers)
|
||||||
cfg.SelectCatalogers = flatten(cfg.SelectCatalogers)
|
cfg.SelectCatalogers = Flatten(cfg.SelectCatalogers)
|
||||||
cfg.Enrich = flatten(cfg.Enrich)
|
cfg.Enrich = Flatten(cfg.Enrich)
|
||||||
|
|
||||||
// for backwards compatibility
|
// for backwards compatibility
|
||||||
cfg.DefaultCatalogers = append(cfg.DefaultCatalogers, cfg.Catalogers...)
|
cfg.DefaultCatalogers = append(cfg.DefaultCatalogers, cfg.Catalogers...)
|
||||||
@ -295,7 +295,7 @@ func (cfg *Catalog) PostLoad() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func flatten(commaSeparatedEntries []string) []string {
|
func Flatten(commaSeparatedEntries []string) []string {
|
||||||
var out []string
|
var out []string
|
||||||
for _, v := range commaSeparatedEntries {
|
for _, v := range commaSeparatedEntries {
|
||||||
for _, s := range strings.Split(v, ",") {
|
for _, s := range strings.Split(v, ",") {
|
||||||
|
|||||||
@ -139,7 +139,7 @@ func Test_enrichmentEnabled(t *testing.T) {
|
|||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.directives, func(t *testing.T) {
|
t.Run(test.directives, func(t *testing.T) {
|
||||||
got := enrichmentEnabled(flatten([]string{test.directives}), test.test)
|
got := enrichmentEnabled(Flatten([]string{test.directives}), test.test)
|
||||||
assert.Equal(t, test.expected, got)
|
assert.Equal(t, test.expected, got)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user