Merge pull request #1252 from jedevc/prune-filter-until

Fix prune `--filter until=<duration>` option
pull/1257/head
CrazyMax 3 years ago committed by GitHub
commit 74b21258b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -155,9 +155,9 @@ func toBuildkitPruneInfo(f filters.Args) (*client.PruneInfo, error) {
if len(untilValues) > 0 && len(unusedForValues) > 0 { if len(untilValues) > 0 && len(unusedForValues) > 0 {
return nil, errors.Errorf("conflicting filters %q and %q", "until", "unused-for") return nil, errors.Errorf("conflicting filters %q and %q", "until", "unused-for")
} }
filterKey := "until" untilKey := "until"
if len(unusedForValues) > 0 { if len(unusedForValues) > 0 {
filterKey = "unused-for" untilKey = "unused-for"
} }
untilValues = append(untilValues, unusedForValues...) untilValues = append(untilValues, unusedForValues...)
@ -168,23 +168,27 @@ func toBuildkitPruneInfo(f filters.Args) (*client.PruneInfo, error) {
var err error var err error
until, err = time.ParseDuration(untilValues[0]) until, err = time.ParseDuration(untilValues[0])
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "%q filter expects a duration (e.g., '24h')", filterKey) return nil, errors.Wrapf(err, "%q filter expects a duration (e.g., '24h')", untilKey)
} }
default: default:
return nil, errors.Errorf("filters expect only one value") return nil, errors.Errorf("filters expect only one value")
} }
bkFilter := make([]string, 0, f.Len()) filters := make([]string, 0, f.Len())
for _, field := range f.Keys() { for _, filterKey := range f.Keys() {
values := f.Get(field) if filterKey == untilKey {
continue
}
values := f.Get(filterKey)
switch len(values) { switch len(values) {
case 0: case 0:
bkFilter = append(bkFilter, field) filters = append(filters, filterKey)
case 1: case 1:
if field == "id" { if filterKey == "id" {
bkFilter = append(bkFilter, field+"~="+values[0]) filters = append(filters, filterKey+"~="+values[0])
} else { } else {
bkFilter = append(bkFilter, field+"=="+values[0]) filters = append(filters, filterKey+"=="+values[0])
} }
default: default:
return nil, errors.Errorf("filters expect only one value") return nil, errors.Errorf("filters expect only one value")
@ -192,6 +196,6 @@ func toBuildkitPruneInfo(f filters.Args) (*client.PruneInfo, error) {
} }
return &client.PruneInfo{ return &client.PruneInfo{
KeepDuration: until, KeepDuration: until,
Filter: []string{strings.Join(bkFilter, ",")}, Filter: []string{strings.Join(filters, ",")},
}, nil }, nil
} }

Loading…
Cancel
Save