Skip to content

Commit

Permalink
Fix psFormat's Size handling in config file
Browse files Browse the repository at this point in the history
- do an early check if a custom format is specified either through the
  command-line, or through the cli's configuration, before adjusting
  the options (to add "size" if needed).
- also removes a redundant `options.Size = opts.size` line, as this value is
  already copied at the start of buildContainerListOptions()
- Update NewContainerFormat to use "table" format as a default if no format
  was given.

Co-authored-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Phong Tran <tran.pho@northeastern.edu>
  • Loading branch information
photra and thaJeztah committed Jun 7, 2022
1 parent 7294e7c commit 0929bed
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
19 changes: 7 additions & 12 deletions cli/command/container/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, er
options.Limit = 1
}

options.Size = opts.size
if !options.Size && len(opts.format) > 0 {
if !opts.quiet && !options.Size && len(opts.format) > 0 {
// The --size option isn't set, but .Size may be used in the template.
// Parse and execute the given template to detect if the .Size field is
// used. If it is, then automatically enable the --size option. See #24696
Expand Down Expand Up @@ -109,6 +108,11 @@ func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, er
func runPs(dockerCli command.Cli, options *psOptions) error {
ctx := context.Background()

if len(options.format) == 0 {
// load custom psFormat from CLI config (if any)
options.format = dockerCli.ConfigFile().PsFormat
}

listOptions, err := buildContainerListOptions(options)
if err != nil {
return err
Expand All @@ -119,18 +123,9 @@ func runPs(dockerCli command.Cli, options *psOptions) error {
return err
}

format := options.format
if len(format) == 0 {
if len(dockerCli.ConfigFile().PsFormat) > 0 && !options.quiet {
format = dockerCli.ConfigFile().PsFormat
} else {
format = formatter.TableFormatKey
}
}

containerCtx := formatter.Context{
Output: dockerCli.Out(),
Format: formatter.NewContainerFormat(format, options.quiet, listOptions.Size),
Format: formatter.NewContainerFormat(options.format, options.quiet, listOptions.Size),
Trunc: !options.noTrunc,
}
return formatter.ContainerWrite(containerCtx, containers)
Expand Down
6 changes: 3 additions & 3 deletions cli/command/container/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,13 @@ func TestContainerListWithConfigFormat(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{
containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) {
return []types.Container{
*Container("c1", WithLabel("some.label", "value")),
*Container("c2", WithName("foo/bar"), WithLabel("foo", "bar")),
*Container("c1", WithLabel("some.label", "value"), WithSize(10700000)),
*Container("c2", WithName("foo/bar"), WithLabel("foo", "bar"), WithSize(3200000)),
}, nil
},
})
cli.SetConfigFile(&configfile.ConfigFile{
PsFormat: "{{ .Names }} {{ .Image }} {{ .Labels }}",
PsFormat: "{{ .Names }} {{ .Image }} {{ .Labels }} {{ .Size}}",
})
cmd := newListCommand(cli)
assert.NilError(t, cmd.Execute())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
c1 busybox:latest some.label=value
c2 busybox:latest foo=bar
c1 busybox:latest some.label=value 10.7MB
c2 busybox:latest foo=bar 3.2MB
5 changes: 3 additions & 2 deletions cli/command/formatter/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// NewContainerFormat returns a Format for rendering using a Context
func NewContainerFormat(source string, quiet bool, size bool) Format {
switch source {
case TableFormatKey:
case TableFormatKey, "": // table formatting is the default if none is set.
if quiet {
return DefaultQuietFormat
}
Expand All @@ -54,8 +54,9 @@ ports: {{- pad .Ports 1 0}}
format += `size: {{.Size}}\n`
}
return Format(format)
default: // custom format
return Format(source)
}
return Format(source)
}

// ContainerWrite renders the context for a list of containers
Expand Down
9 changes: 9 additions & 0 deletions internal/test/builders/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ func WithPort(privateport, publicport uint16, builders ...func(*types.Port)) fun
}
}

// WithSize adds size in bytes to the container
func WithSize(size int64) func(*types.Container) {
return func(c *types.Container) {
if size >= 0 {
c.SizeRw = size
}
}
}

// IP sets the ip of the port
func IP(ip string) func(*types.Port) {
return func(p *types.Port) {
Expand Down

0 comments on commit 0929bed

Please sign in to comment.