1+ // FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
2+ //go:build go1.23
3+
14package container
25
36import (
@@ -6,17 +9,18 @@ import (
69 "errors"
710 "fmt"
811 "io"
12+ "maps"
913 "strings"
1014 "sync"
1115 "time"
1216
17+ "github.com/containerd/errdefs"
1318 "github.com/docker/cli/cli"
1419 "github.com/docker/cli/cli/command"
1520 "github.com/docker/cli/cli/command/completion"
1621 "github.com/docker/cli/cli/command/formatter"
1722 flagsHelper "github.com/docker/cli/cli/flags"
1823 "github.com/moby/moby/api/types/events"
19- "github.com/moby/moby/api/types/filters"
2024 "github.com/moby/moby/client"
2125 "github.com/sirupsen/logrus"
2226 "github.com/spf13/cobra"
@@ -60,7 +64,7 @@ type StatsOptions struct {
6064 // filter options may be added in future (within the constraints described
6165 // above), but may require daemon-side validation as the list of accepted
6266 // filters can differ between daemon- and API versions.
63- Filters * filters. Args
67+ Filters client. Filters
6468}
6569
6670// newStatsCommand creates a new [cobra.Command] for "docker container stats".
@@ -123,12 +127,14 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
123127 started := make (chan struct {})
124128
125129 if options .Filters == nil {
126- f := filters .NewArgs ()
127- options .Filters = & f
130+ options .Filters = make (client.Filters )
128131 }
129132
130- if err := options .Filters .Validate (acceptedStatsFilters ); err != nil {
131- return err
133+ // FIXME(thaJeztah): any way we can (and should?) validate allowed filters?
134+ for filter := range options .Filters {
135+ if _ , ok := acceptedStatsFilters [filter ]; ! ok {
136+ return errdefs .ErrInvalidArgument .WithMessage ("invalid filter '" + filter + "'" )
137+ }
132138 }
133139
134140 eh := newEventHandler ()
@@ -163,7 +169,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
163169 // the original set of filters. Custom filters are used both
164170 // to list containers and to filter events, but the "type" filter
165171 // is not valid for filtering containers.
166- f := options .Filters . Clone ()
172+ f := maps . Clone ( options .Filters ) // FIXME(thaJeztah): is this correct?
167173 f .Add ("type" , string (events .ContainerEventType ))
168174 eventChan , errChan := apiClient .Events (ctx , client.EventsListOptions {
169175 Filters : f ,
@@ -199,7 +205,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
199205 // to refresh the list of containers.
200206 cs , err := apiClient .ContainerList (ctx , client.ContainerListOptions {
201207 All : options .All ,
202- Filters : * options .Filters ,
208+ Filters : options .Filters ,
203209 })
204210 if err != nil {
205211 return err
@@ -219,7 +225,7 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
219225 // only a single code-path is needed, and custom filters can be combined
220226 // with a list of container names/IDs.
221227
222- if options .Filters != nil && options . Filters . Len ( ) > 0 {
228+ if len ( options .Filters ) > 0 {
223229 return errors .New ("filtering is not supported when specifying a list of containers" )
224230 }
225231
0 commit comments