Fix psFormat's Size handling in config file

- 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>
This commit is contained in:
Phong Tran 2022-05-31 08:33:19 +07:00 committed by Sebastiaan van Stijn
parent 7294e7c701
commit 0929bed42a
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
5 changed files with 24 additions and 19 deletions

View File

@ -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
@ -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
@ -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)

View File

@ -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())

View File

@ -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

View File

@ -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
}
@ -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

View File

@ -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) {