github.com/portworx/docker@v1.12.1/api/client/container/ps.go (about) 1 package container 2 3 import ( 4 "golang.org/x/net/context" 5 6 "github.com/docker/docker/api/client" 7 "github.com/docker/docker/api/client/formatter" 8 "github.com/docker/docker/cli" 9 "github.com/docker/engine-api/types" 10 "github.com/docker/engine-api/types/filters" 11 12 "github.com/docker/docker/utils/templates" 13 "github.com/spf13/cobra" 14 "io/ioutil" 15 ) 16 17 type psOptions struct { 18 quiet bool 19 size bool 20 all bool 21 noTrunc bool 22 nLatest bool 23 last int 24 format string 25 filter []string 26 } 27 28 type preProcessor struct { 29 opts *types.ContainerListOptions 30 } 31 32 // Size sets the size option when called by a template execution. 33 func (p *preProcessor) Size() bool { 34 p.opts.Size = true 35 return true 36 } 37 38 // NewPsCommand creates a new cobra.Command for `docker ps` 39 func NewPsCommand(dockerCli *client.DockerCli) *cobra.Command { 40 var opts psOptions 41 42 cmd := &cobra.Command{ 43 Use: "ps [OPTIONS]", 44 Short: "List containers", 45 Args: cli.NoArgs, 46 RunE: func(cmd *cobra.Command, args []string) error { 47 return runPs(dockerCli, &opts) 48 }, 49 } 50 51 flags := cmd.Flags() 52 53 flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display numeric IDs") 54 flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes") 55 flags.BoolVarP(&opts.all, "all", "a", false, "Show all containers (default shows just running)") 56 flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output") 57 flags.BoolVarP(&opts.nLatest, "latest", "l", false, "Show the latest created container (includes all states)") 58 flags.IntVarP(&opts.last, "last", "n", -1, "Show n last created containers (includes all states)") 59 flags.StringVarP(&opts.format, "format", "", "", "Pretty-print containers using a Go template") 60 flags.StringSliceVarP(&opts.filter, "filter", "f", []string{}, "Filter output based on conditions provided") 61 62 return cmd 63 } 64 65 func runPs(dockerCli *client.DockerCli, opts *psOptions) error { 66 ctx := context.Background() 67 68 if opts.nLatest && opts.last == -1 { 69 opts.last = 1 70 } 71 72 containerFilterArgs := filters.NewArgs() 73 for _, f := range opts.filter { 74 var err error 75 containerFilterArgs, err = filters.ParseFlag(f, containerFilterArgs) 76 if err != nil { 77 return err 78 } 79 } 80 81 options := types.ContainerListOptions{ 82 All: opts.all, 83 Limit: opts.last, 84 Size: opts.size, 85 Filter: containerFilterArgs, 86 } 87 88 pre := &preProcessor{opts: &options} 89 tmpl, err := templates.Parse(opts.format) 90 91 if err != nil { 92 return err 93 } 94 95 _ = tmpl.Execute(ioutil.Discard, pre) 96 97 containers, err := dockerCli.Client().ContainerList(ctx, options) 98 if err != nil { 99 return err 100 } 101 102 f := opts.format 103 if len(f) == 0 { 104 if len(dockerCli.PsFormat()) > 0 && !opts.quiet { 105 f = dockerCli.PsFormat() 106 } else { 107 f = "table" 108 } 109 } 110 111 psCtx := formatter.ContainerContext{ 112 Context: formatter.Context{ 113 Output: dockerCli.Out(), 114 Format: f, 115 Quiet: opts.quiet, 116 Trunc: !opts.noTrunc, 117 }, 118 Size: opts.size, 119 Containers: containers, 120 } 121 122 psCtx.Write() 123 124 return nil 125 }