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

Fix prune `--filter until=<duration>` option
This commit is contained in:
CrazyMax 2022-08-08 12:00:55 +02:00 committed by GitHub
commit 74b21258b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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