Skip to content

Commit 6458c79

Browse files
feat!: respenct component selector ordering
Signed-off-by: vincent-onebrief <[email protected]>
1 parent 8e57b52 commit 6458c79

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

site/src/content/docs/ref/components.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ If you have any `default` components in a package definition you can also exclud
313313
$ zarf package deploy ./path/to/package.tar.zst --components=optional-component-1,-default-component-1
314314
```
315315

316+
For components that match multiple selectors, the last selector takes priority.
317+
318+
```bash
319+
# exclude all components except component-1
320+
$ zarf package deploy ./path/to/package.tar.zst --components=-*,component-1
321+
```
322+
316323
:::
317324

318325
## Extensions (Removed)

src/pkg/packager/filters/utils.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,30 @@ const (
1818
)
1919

2020
func includedOrExcluded(componentName string, requestedComponentNames []string) (selectState, string, error) {
21-
// Check if the component has a leading dash indicating it should be excluded - this is done first so that exclusions precede inclusions
22-
for _, requestedComponent := range requestedComponentNames {
23-
if strings.HasPrefix(requestedComponent, "-") {
24-
// If the component glob matches one of the requested components, then return true
25-
// This supports globbing with "path" in order to have the same behavior across OSes (if we ever allow namespaced components with /)
26-
matched, err := path.Match(strings.TrimPrefix(requestedComponent, "-"), componentName)
27-
if err != nil {
28-
return unknown, "", err
29-
}
30-
if matched {
31-
return excluded, requestedComponent, nil
32-
}
33-
}
34-
}
35-
// Check if the component matches a glob pattern and should be included
21+
// In unmatched cases we don't know if we should include or exclude yet
22+
var match string
23+
var matchType = unknown
24+
25+
// Check every component request, so last match get's priority
26+
// Order is assumed to match user input
3627
for _, requestedComponent := range requestedComponentNames {
3728
// If the component glob matches one of the requested components, then return true
3829
// This supports globbing with "path" in order to have the same behavior across OSes (if we ever allow namespaced components with /)
39-
matched, err := path.Match(requestedComponent, componentName)
30+
matched, err := path.Match(strings.TrimPrefix(requestedComponent, "-"), componentName)
4031
if err != nil {
4132
return unknown, "", err
4233
}
34+
4335
if matched {
44-
return included, requestedComponent, nil
36+
match = requestedComponent
37+
// Exclusions are requests with the leading "-"
38+
if strings.HasPrefix(requestedComponent, "-") {
39+
matchType = excluded
40+
} else {
41+
matchType = included
42+
}
4543
}
4644
}
4745

48-
// All other cases we don't know if we should include or exclude yet
49-
return unknown, "", nil
46+
return matchType, match, nil
5047
}

src/pkg/packager/filters/utils_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,9 @@ func Test_includedOrExcluded(t *testing.T) {
4545
name: "Test when component is excluded and included",
4646
componentName: "example",
4747
requestedComponentNames: []string{"-example", "example"},
48-
wantState: excluded,
49-
wantRequestedComponent: "-example",
48+
wantState: included,
49+
wantRequestedComponent: "example",
5050
},
51-
// interesting case, excluded wins
5251
{
5352
name: "Test when component is included and excluded",
5453
componentName: "example",

0 commit comments

Comments
 (0)