Skip to content

Commit 1993949

Browse files
authored
preserve --from order (#4350)
Signed-off-by: Alex Goodman <[email protected]>
1 parent 8a22d39 commit 1993949

File tree

3 files changed

+104
-7
lines changed

3 files changed

+104
-7
lines changed

cmd/syft/internal/commands/cataloger_list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ func runCatalogerList(opts *catalogerListOptions) error {
8787
}
8888

8989
func catalogerListReport(opts *catalogerListOptions, allTaskGroups [][]task.Task) (string, error) {
90-
defaultCatalogers := options.Flatten(opts.DefaultCatalogers)
91-
selectCatalogers := options.Flatten(opts.SelectCatalogers)
90+
defaultCatalogers := options.FlattenAndSort(opts.DefaultCatalogers)
91+
selectCatalogers := options.FlattenAndSort(opts.SelectCatalogers)
9292
selectedTaskGroups, selectionEvidence, err := task.SelectInGroups(
9393
allTaskGroups,
9494
cataloging.NewSelectionRequest().

cmd/syft/internal/options/catalog.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,10 @@ func (cfg *Catalog) PostLoad() error {
284284

285285
cfg.From = Flatten(cfg.From)
286286

287-
cfg.Catalogers = Flatten(cfg.Catalogers)
288-
cfg.DefaultCatalogers = Flatten(cfg.DefaultCatalogers)
289-
cfg.SelectCatalogers = Flatten(cfg.SelectCatalogers)
290-
cfg.Enrich = Flatten(cfg.Enrich)
287+
cfg.Catalogers = FlattenAndSort(cfg.Catalogers)
288+
cfg.DefaultCatalogers = FlattenAndSort(cfg.DefaultCatalogers)
289+
cfg.SelectCatalogers = FlattenAndSort(cfg.SelectCatalogers)
290+
cfg.Enrich = FlattenAndSort(cfg.Enrich)
291291

292292
// for backwards compatibility
293293
cfg.DefaultCatalogers = append(cfg.DefaultCatalogers, cfg.Catalogers...)
@@ -312,6 +312,11 @@ func Flatten(commaSeparatedEntries []string) []string {
312312
out = append(out, strings.TrimSpace(s))
313313
}
314314
}
315+
return out
316+
}
317+
318+
func FlattenAndSort(commaSeparatedEntries []string) []string {
319+
out := Flatten(commaSeparatedEntries)
315320
sort.Strings(out)
316321
return out
317322
}

cmd/syft/internal/options/catalog_test.go

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,98 @@ func TestCatalog_PostLoad(t *testing.T) {
7979
}
8080
}
8181

82+
func TestFlatten(t *testing.T) {
83+
tests := []struct {
84+
name string
85+
input []string
86+
expected []string
87+
}{
88+
{
89+
name: "preserves order of comma-separated values",
90+
input: []string{"registry,docker,oci-dir"},
91+
expected: []string{"registry", "docker", "oci-dir"},
92+
},
93+
{
94+
name: "preserves order across multiple entries",
95+
input: []string{"registry,docker", "oci-dir"},
96+
expected: []string{"registry", "docker", "oci-dir"},
97+
},
98+
{
99+
name: "trims whitespace",
100+
input: []string{" registry , docker ", " oci-dir "},
101+
expected: []string{"registry", "docker", "oci-dir"},
102+
},
103+
{
104+
name: "handles single value",
105+
input: []string{"registry"},
106+
expected: []string{"registry"},
107+
},
108+
{
109+
name: "handles empty input",
110+
input: []string{},
111+
expected: nil,
112+
},
113+
{
114+
name: "preserves reverse alphabetical order",
115+
input: []string{"zebra,yankee,xray"},
116+
expected: []string{"zebra", "yankee", "xray"},
117+
},
118+
}
119+
120+
for _, tt := range tests {
121+
t.Run(tt.name, func(t *testing.T) {
122+
got := Flatten(tt.input)
123+
assert.Equal(t, tt.expected, got)
124+
})
125+
}
126+
}
127+
128+
func TestFlattenAndSort(t *testing.T) {
129+
tests := []struct {
130+
name string
131+
input []string
132+
expected []string
133+
}{
134+
{
135+
name: "sorts comma-separated values",
136+
input: []string{"registry,docker,oci-dir"},
137+
expected: []string{"docker", "oci-dir", "registry"},
138+
},
139+
{
140+
name: "sorts across multiple entries",
141+
input: []string{"registry,docker", "oci-dir"},
142+
expected: []string{"docker", "oci-dir", "registry"},
143+
},
144+
{
145+
name: "trims whitespace and sorts",
146+
input: []string{" registry , docker ", " oci-dir "},
147+
expected: []string{"docker", "oci-dir", "registry"},
148+
},
149+
{
150+
name: "handles single value",
151+
input: []string{"registry"},
152+
expected: []string{"registry"},
153+
},
154+
{
155+
name: "handles empty input",
156+
input: []string{},
157+
expected: nil,
158+
},
159+
{
160+
name: "sorts reverse alphabetical order",
161+
input: []string{"zebra,yankee,xray"},
162+
expected: []string{"xray", "yankee", "zebra"},
163+
},
164+
}
165+
166+
for _, tt := range tests {
167+
t.Run(tt.name, func(t *testing.T) {
168+
got := FlattenAndSort(tt.input)
169+
assert.Equal(t, tt.expected, got)
170+
})
171+
}
172+
}
173+
82174
func Test_enrichmentEnabled(t *testing.T) {
83175
tests := []struct {
84176
directives string
@@ -139,7 +231,7 @@ func Test_enrichmentEnabled(t *testing.T) {
139231

140232
for _, test := range tests {
141233
t.Run(test.directives, func(t *testing.T) {
142-
got := enrichmentEnabled(Flatten([]string{test.directives}), test.test)
234+
got := enrichmentEnabled(FlattenAndSort([]string{test.directives}), test.test)
143235
assert.Equal(t, test.expected, got)
144236
})
145237
}

0 commit comments

Comments
 (0)