mirror of https://github.com/docker/cli.git
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:
parent
7294e7c701
commit
0929bed42a
|
@ -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)
|
||||||
|
|
|
@ -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())
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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,8 +54,9 @@ 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
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
Loading…
Reference in New Issue