@@ -17,14 +17,15 @@ import (
1717)
1818
1919type psOptions struct {
20- quiet bool
21- size bool
22- all bool
23- noTrunc bool
24- nLatest bool
25- last int
26- format string
27- filter opts.FilterOpt
20+ quiet bool
21+ size bool
22+ sizeChanged bool
23+ all bool
24+ noTrunc bool
25+ nLatest bool
26+ last int
27+ format string
28+ filter opts.FilterOpt
2829}
2930
3031// NewPsCommand creates a new cobra.Command for `docker ps`
@@ -36,6 +37,7 @@ func NewPsCommand(dockerCli command.Cli) *cobra.Command {
3637 Short : "List containers" ,
3738 Args : cli .NoArgs ,
3839 RunE : func (cmd * cobra.Command , args []string ) error {
40+ options .sizeChanged = cmd .Flags ().Changed ("size" )
3941 return runPs (dockerCli , & options )
4042 },
4143 Annotations : map [string ]string {
@@ -78,13 +80,8 @@ func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, er
7880 options .Limit = 1
7981 }
8082
81- if ! opts .quiet && ! options .Size && len (opts .format ) > 0 {
82- // The --size option isn't set, but .Size may be used in the template.
83- // Parse and execute the given template to detect if the .Size field is
84- // used. If it is, then automatically enable the --size option. See #24696
85- //
86- // Only requesting container size information when needed is an optimization,
87- // because calculating the size is a costly operation.
83+ // always validate template when `--format` is used, for consistency
84+ if len (opts .format ) > 0 {
8885 tmpl , err := templates .NewParse ("" , opts .format )
8986 if err != nil {
9087 return nil , errors .Wrap (err , "failed to parse template" )
@@ -98,8 +95,19 @@ func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, er
9895 return nil , errors .Wrap (err , "failed to execute template" )
9996 }
10097
101- if _ , ok := optionsProcessor .FieldsUsed ["Size" ]; ok {
102- options .Size = true
98+ // if `size` was not explicitly set to false (with `--size=false`)
99+ // and `--quiet` is not set, request size if the template requires it
100+ if ! opts .quiet && ! options .Size && ! opts .sizeChanged {
101+ // The --size option isn't set, but .Size may be used in the template.
102+ // Parse and execute the given template to detect if the .Size field is
103+ // used. If it is, then automatically enable the --size option. See #24696
104+ //
105+ // Only requesting container size information when needed is an optimization,
106+ // because calculating the size is a costly operation.
107+
108+ if _ , ok := optionsProcessor .FieldsUsed ["Size" ]; ok {
109+ options .Size = true
110+ }
103111 }
104112 }
105113
0 commit comments