Merge pull request #3640 from photra/3632-fix-ps-format

Fix psFormat's Size handling in config file
This commit is contained in:
Sebastiaan van Stijn 2022-06-07 13:08:43 +02:00 committed by GitHub
commit c59773f155
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.Limit = 1
} }
options.Size = opts.size if !opts.quiet && !options.Size && len(opts.format) > 0 {
if !options.Size && len(opts.format) > 0 {
// The --size option isn't set, but .Size may be used in the template. // 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 // 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 // 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 { func runPs(dockerCli command.Cli, options *psOptions) error {
ctx := context.Background() 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) listOptions, err := buildContainerListOptions(options)
if err != nil { if err != nil {
return err return err
@ -119,18 +123,9 @@ func runPs(dockerCli command.Cli, options *psOptions) error {
return err 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{ containerCtx := formatter.Context{
Output: dockerCli.Out(), Output: dockerCli.Out(),
Format: formatter.NewContainerFormat(format, options.quiet, listOptions.Size), Format: formatter.NewContainerFormat(options.format, options.quiet, listOptions.Size),
Trunc: !options.noTrunc, Trunc: !options.noTrunc,
} }
return formatter.ContainerWrite(containerCtx, containers) return formatter.ContainerWrite(containerCtx, containers)

View File

@ -246,13 +246,13 @@ func TestContainerListWithConfigFormat(t *testing.T) {
cli := test.NewFakeCli(&fakeClient{ cli := test.NewFakeCli(&fakeClient{
containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) { containerListFunc: func(_ types.ContainerListOptions) ([]types.Container, error) {
return []types.Container{ return []types.Container{
*Container("c1", WithLabel("some.label", "value")), *Container("c1", WithLabel("some.label", "value"), WithSize(10700000)),
*Container("c2", WithName("foo/bar"), WithLabel("foo", "bar")), *Container("c2", WithName("foo/bar"), WithLabel("foo", "bar"), WithSize(3200000)),
}, nil }, nil
}, },
}) })
cli.SetConfigFile(&configfile.ConfigFile{ cli.SetConfigFile(&configfile.ConfigFile{
PsFormat: "{{ .Names }} {{ .Image }} {{ .Labels }}", PsFormat: "{{ .Names }} {{ .Image }} {{ .Labels }} {{ .Size}}",
}) })
cmd := newListCommand(cli) cmd := newListCommand(cli)
assert.NilError(t, cmd.Execute()) assert.NilError(t, cmd.Execute())

View File

@ -1,2 +1,2 @@
c1 busybox:latest some.label=value c1 busybox:latest some.label=value 10.7MB
c2 busybox:latest foo=bar c2 busybox:latest foo=bar 3.2MB

View File

@ -27,7 +27,7 @@ const (
// NewContainerFormat returns a Format for rendering using a Context // NewContainerFormat returns a Format for rendering using a Context
func NewContainerFormat(source string, quiet bool, size bool) Format { func NewContainerFormat(source string, quiet bool, size bool) Format {
switch source { switch source {
case TableFormatKey: case TableFormatKey, "": // table formatting is the default if none is set.
if quiet { if quiet {
return DefaultQuietFormat return DefaultQuietFormat
} }
@ -54,9 +54,10 @@ ports: {{- pad .Ports 1 0}}
format += `size: {{.Size}}\n` format += `size: {{.Size}}\n`
} }
return Format(format) return Format(format)
} default: // custom format
return Format(source) return Format(source)
} }
}
// ContainerWrite renders the context for a list of containers // ContainerWrite renders the context for a list of containers
func ContainerWrite(ctx Context, containers []types.Container) error { func ContainerWrite(ctx Context, containers []types.Container) error {

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 // IP sets the ip of the port
func IP(ip string) func(*types.Port) { func IP(ip string) func(*types.Port) {
return func(p *types.Port) { return func(p *types.Port) {