Skip to content

Commit a2e1794

Browse files
authored
Merge pull request #5946 from thaJeztah/prunefilter_cleanup
cli/command: PruneFilters: slight cleanup
2 parents 2c3cf8d + d97f65c commit a2e1794

1 file changed

Lines changed: 21 additions & 12 deletions

File tree

cli/command/utils.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,33 +140,42 @@ func PromptForConfirmation(ctx context.Context, ins io.Reader, outs io.Writer, m
140140
}
141141
}
142142

143-
// PruneFilters returns consolidated prune filters obtained from config.json and cli
143+
// PruneFilters merges prune filters specified in config.json with those specified
144+
// as command-line flags.
145+
//
146+
// CLI label filters have precedence over those specified in config.json. If a
147+
// label filter specified as flag conflicts with a label defined in config.json
148+
// (i.e., "label=some-value" conflicts with "label!=some-value", and vice versa),
149+
// then the filter defined in config.json is omitted.
144150
func PruneFilters(dockerCLI config.Provider, pruneFilters filters.Args) filters.Args {
145151
cfg := dockerCLI.ConfigFile()
146152
if cfg == nil {
147153
return pruneFilters
148154
}
155+
156+
// Merge filters provided through the CLI with default filters defined
157+
// in the CLI-configfile.
149158
for _, f := range cfg.PruneFilters {
150159
k, v, ok := strings.Cut(f, "=")
151160
if !ok {
152161
continue
153162
}
154-
if k == "label" {
155-
// CLI label filter supersede config.json.
156-
// If CLI label filter conflict with config.json,
157-
// skip adding label! filter in config.json.
158-
if pruneFilters.Contains("label!") && pruneFilters.ExactMatch("label!", v) {
163+
switch k {
164+
case "label":
165+
// "label != some-value" conflicts with "label = some-value"
166+
if pruneFilters.ExactMatch("label!", v) {
159167
continue
160168
}
161-
} else if k == "label!" {
162-
// CLI label! filter supersede config.json.
163-
// If CLI label! filter conflict with config.json,
164-
// skip adding label filter in config.json.
165-
if pruneFilters.Contains("label") && pruneFilters.ExactMatch("label", v) {
169+
pruneFilters.Add(k, v)
170+
case "label!":
171+
// "label != some-value" conflicts with "label = some-value"
172+
if pruneFilters.ExactMatch("label", v) {
166173
continue
167174
}
175+
pruneFilters.Add(k, v)
176+
default:
177+
pruneFilters.Add(k, v)
168178
}
169-
pruneFilters.Add(k, v)
170179
}
171180

172181
return pruneFilters

0 commit comments

Comments
 (0)