44 "context"
55 "fmt"
66 "io"
7+ "maps"
78 "os"
9+ "slices"
810 "strings"
911 "text/tabwriter"
1012 "time"
@@ -14,12 +16,12 @@ import (
1416 "github.com/docker/cli/cli"
1517 "github.com/docker/cli/cli/command"
1618 "github.com/docker/cli/opts"
17- "github.com/docker/docker/api/types/filters"
1819 "github.com/docker/go-units"
1920 "github.com/moby/buildkit/client"
2021 gateway "github.com/moby/buildkit/frontend/gateway/client"
2122 pb "github.com/moby/buildkit/solver/pb"
2223 "github.com/moby/buildkit/util/apicaps"
24+ dclient "github.com/moby/moby/client"
2325 "github.com/pkg/errors"
2426 "github.com/spf13/cobra"
2527 "golang.org/x/sync/errgroup"
@@ -42,9 +44,7 @@ const (
4244)
4345
4446func runPrune (ctx context.Context , dockerCli command.Cli , opts pruneOptions ) error {
45- pruneFilters := opts .filter .Value ()
46- pruneFilters = command .PruneFilters (dockerCli , pruneFilters )
47-
47+ pruneFilters := command .PruneFilters (dockerCli , opts .filter .Value ())
4848 pi , err := toBuildkitPruneInfo (pruneFilters )
4949 if err != nil {
5050 return err
@@ -189,20 +189,22 @@ func pruneCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
189189 return cmd
190190}
191191
192- func toBuildkitPruneInfo ( f filters. Args ) ( * client. PruneInfo , error ) {
193- var until time. Duration
194- untilValues := f . Get ( "until" ) // canonical
195- unusedForValues := f . Get ( "unused-for" ) // deprecated synonym for "until" filter
192+ // getFilter returns the list of values associated with the key
193+ func getFilter ( f dclient. Filters , key string ) [] string {
194+ return slices . Collect ( maps . Keys ( f [ key ]))
195+ }
196196
197- if len (untilValues ) > 0 && len (unusedForValues ) > 0 {
198- return nil , errors .Errorf ("conflicting filters %q and %q" , "until" , "unused-for" )
197+ func toBuildkitPruneInfo (pruneFilters dclient.Filters ) (* client.PruneInfo , error ) {
198+ var until time.Duration
199+ if len (pruneFilters ["until" ]) > 0 && len (pruneFilters ["unused-for" ]) > 0 {
200+ return nil , errors .New (`conflicting filters "until" and "unused-for"` )
199201 }
200202 untilKey := "until"
201- if len (unusedForValues ) > 0 {
202- untilKey = "unused-for"
203+ if len (pruneFilters [ "unused-for" ] ) > 0 {
204+ untilKey = "unused-for" // deprecated synonym for "until" filter
203205 }
204- untilValues = append (untilValues , unusedForValues ... )
205206
207+ untilValues := getFilter (pruneFilters , untilKey )
206208 switch len (untilValues ) {
207209 case 0 :
208210 // nothing to do
@@ -213,16 +215,16 @@ func toBuildkitPruneInfo(f filters.Args) (*client.PruneInfo, error) {
213215 return nil , errors .Wrapf (err , "%q filter expects a duration (e.g., '24h')" , untilKey )
214216 }
215217 default :
216- return nil , errors .Errorf ("filters expect only one value" )
218+ return nil , errors .Errorf ("%q filter expects only one value" , untilKey )
217219 }
218220
219- filters := make ([]string , 0 , f . Len ( ))
220- for _ , filterKey := range f . Keys () {
221+ filters := make ([]string , 0 , len ( pruneFilters ))
222+ for filterKey := range pruneFilters {
221223 if filterKey == untilKey {
222224 continue
223225 }
224226
225- values := f . Get ( filterKey )
227+ values := getFilter ( pruneFilters , filterKey )
226228 switch len (values ) {
227229 case 0 :
228230 filters = append (filters , filterKey )
@@ -235,7 +237,7 @@ func toBuildkitPruneInfo(f filters.Args) (*client.PruneInfo, error) {
235237 filters = append (filters , filterKey + "==" + values [0 ])
236238 }
237239 default :
238- return nil , errors .Errorf ("filters expect only one value" )
240+ return nil , errors .Errorf ("%q filter expects only one value" , filterKey )
239241 }
240242 }
241243 return & client.PruneInfo {
0 commit comments