mirror of https://github.com/docker/docs.git
Merge pull request #23247 from thaJeztah/carry-22033-docker-ps-format-output
[Carry 22033] Add a check for size field in custom format string
This commit is contained in:
commit
1a1083ae75
|
|
@ -114,35 +114,27 @@ type ImageContext struct {
|
||||||
func (ctx ContainerContext) Write() {
|
func (ctx ContainerContext) Write() {
|
||||||
switch ctx.Format {
|
switch ctx.Format {
|
||||||
case tableFormatKey:
|
case tableFormatKey:
|
||||||
ctx.Format = defaultContainerTableFormat
|
|
||||||
if ctx.Quiet {
|
if ctx.Quiet {
|
||||||
ctx.Format = defaultQuietFormat
|
ctx.Format = defaultQuietFormat
|
||||||
|
} else {
|
||||||
|
ctx.Format = defaultContainerTableFormat
|
||||||
|
if ctx.Size {
|
||||||
|
ctx.Format += `\t{{.Size}}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case rawFormatKey:
|
case rawFormatKey:
|
||||||
if ctx.Quiet {
|
if ctx.Quiet {
|
||||||
ctx.Format = `container_id: {{.ID}}`
|
ctx.Format = `container_id: {{.ID}}`
|
||||||
} else {
|
} else {
|
||||||
ctx.Format = `container_id: {{.ID}}
|
ctx.Format = `container_id: {{.ID}}\nimage: {{.Image}}\ncommand: {{.Command}}\ncreated_at: {{.CreatedAt}}\nstatus: {{.Status}}\nnames: {{.Names}}\nlabels: {{.Labels}}\nports: {{.Ports}}\n`
|
||||||
image: {{.Image}}
|
|
||||||
command: {{.Command}}
|
|
||||||
created_at: {{.CreatedAt}}
|
|
||||||
status: {{.Status}}
|
|
||||||
names: {{.Names}}
|
|
||||||
labels: {{.Labels}}
|
|
||||||
ports: {{.Ports}}
|
|
||||||
`
|
|
||||||
if ctx.Size {
|
if ctx.Size {
|
||||||
ctx.Format += `size: {{.Size}}
|
ctx.Format += `size: {{.Size}}\n`
|
||||||
`
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.buffer = bytes.NewBufferString("")
|
ctx.buffer = bytes.NewBufferString("")
|
||||||
ctx.preformat()
|
ctx.preformat()
|
||||||
if ctx.table && ctx.Size {
|
|
||||||
ctx.finalFormat += "\t{{.Size}}"
|
|
||||||
}
|
|
||||||
|
|
||||||
tmpl, err := ctx.parseFormat()
|
tmpl, err := ctx.parseFormat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -63,7 +63,7 @@ containerID2 ubuntu "" 24 hours ago
|
||||||
},
|
},
|
||||||
Size: true,
|
Size: true,
|
||||||
},
|
},
|
||||||
"IMAGE SIZE\nubuntu 0 B\nubuntu 0 B\n",
|
"IMAGE\nubuntu\nubuntu\n",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
ContainerContext{
|
ContainerContext{
|
||||||
|
|
@ -230,6 +230,25 @@ func TestContainerContextWriteWithNoContainers(t *testing.T) {
|
||||||
},
|
},
|
||||||
Size: true,
|
Size: true,
|
||||||
},
|
},
|
||||||
|
"IMAGE\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ContainerContext{
|
||||||
|
Context: Context{
|
||||||
|
Format: "table {{.Image}}\t{{.Size}}",
|
||||||
|
Output: out,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"IMAGE SIZE\n",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ContainerContext{
|
||||||
|
Context: Context{
|
||||||
|
Format: "table {{.Image}}\t{{.Size}}",
|
||||||
|
Output: out,
|
||||||
|
},
|
||||||
|
Size: true,
|
||||||
|
},
|
||||||
"IMAGE SIZE\n",
|
"IMAGE SIZE\n",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,27 @@ package client
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/docker/docker/api/client/formatter"
|
"github.com/docker/docker/api/client/formatter"
|
||||||
Cli "github.com/docker/docker/cli"
|
Cli "github.com/docker/docker/cli"
|
||||||
"github.com/docker/docker/opts"
|
"github.com/docker/docker/opts"
|
||||||
flag "github.com/docker/docker/pkg/mflag"
|
flag "github.com/docker/docker/pkg/mflag"
|
||||||
|
"github.com/docker/docker/utils/templates"
|
||||||
"github.com/docker/engine-api/types"
|
"github.com/docker/engine-api/types"
|
||||||
"github.com/docker/engine-api/types/filters"
|
"github.com/docker/engine-api/types/filters"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type preProcessor struct {
|
||||||
|
opts *types.ContainerListOptions
|
||||||
|
}
|
||||||
|
|
||||||
|
// Size sets the size option when called by a template execution.
|
||||||
|
func (p *preProcessor) Size() bool {
|
||||||
|
p.opts.Size = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// CmdPs outputs a list of Docker containers.
|
// CmdPs outputs a list of Docker containers.
|
||||||
//
|
//
|
||||||
// Usage: docker ps [OPTIONS]
|
// Usage: docker ps [OPTIONS]
|
||||||
|
|
@ -54,6 +66,14 @@ func (cli *DockerCli) CmdPs(args ...string) error {
|
||||||
Filter: psFilterArgs,
|
Filter: psFilterArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pre := &preProcessor{opts: &options}
|
||||||
|
tmpl, err := templates.Parse(*format)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = tmpl.Execute(ioutil.Discard, pre)
|
||||||
|
|
||||||
containers, err := cli.client.ContainerList(context.Background(), options)
|
containers, err := cli.client.ContainerList(context.Background(), options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -787,3 +787,20 @@ func (s *DockerSuite) TestPsShowMounts(c *check.C) {
|
||||||
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+prefix+slash+"this-path-was-never-mounted")
|
out, _ = dockerCmd(c, "ps", "--format", "{{.Names}} {{.Mounts}}", "--filter", "volume="+prefix+slash+"this-path-was-never-mounted")
|
||||||
c.Assert(strings.TrimSpace(string(out)), checker.HasLen, 0)
|
c.Assert(strings.TrimSpace(string(out)), checker.HasLen, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *DockerSuite) TestPsFormatSize(c *check.C) {
|
||||||
|
testRequires(c, DaemonIsLinux)
|
||||||
|
runSleepingContainer(c)
|
||||||
|
|
||||||
|
out, _ := dockerCmd(c, "ps", "--format", "table {{.Size}}")
|
||||||
|
lines := strings.Split(out, "\n")
|
||||||
|
c.Assert(lines[1], checker.Not(checker.Equals), "0 B", check.Commentf("Should not display a size of 0 B"))
|
||||||
|
|
||||||
|
out, _ = dockerCmd(c, "ps", "--size", "--format", "table {{.Size}}")
|
||||||
|
lines = strings.Split(out, "\n")
|
||||||
|
c.Assert(lines[0], checker.Equals, "SIZE", check.Commentf("Should only have one size column"))
|
||||||
|
|
||||||
|
out, _ = dockerCmd(c, "ps", "--size", "--format", "raw")
|
||||||
|
lines = strings.Split(out, "\n")
|
||||||
|
c.Assert(lines[8], checker.HasPrefix, "size:", check.Commentf("Size should be appended on a newline"))
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue