github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/client/ps.go (about) 1 package client 2 3 import ( 4 "golang.org/x/net/context" 5 6 "github.com/docker/docker/api/client/formatter" 7 Cli "github.com/docker/docker/cli" 8 "github.com/docker/docker/opts" 9 flag "github.com/docker/docker/pkg/mflag" 10 "github.com/docker/engine-api/types" 11 "github.com/docker/engine-api/types/filters" 12 ) 13 14 // CmdPs outputs a list of Docker containers. 15 // 16 // Usage: docker ps [OPTIONS] 17 func (cli *DockerCli) CmdPs(args ...string) error { 18 var ( 19 err error 20 21 psFilterArgs = filters.NewArgs() 22 23 cmd = Cli.Subcmd("ps", nil, Cli.DockerCommands["ps"].Description, true) 24 quiet = cmd.Bool([]string{"q", "-quiet"}, false, "Only display numeric IDs") 25 size = cmd.Bool([]string{"s", "-size"}, false, "Display total file sizes") 26 all = cmd.Bool([]string{"a", "-all"}, false, "Show all containers (default shows just running)") 27 noTrunc = cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output") 28 nLatest = cmd.Bool([]string{"l", "-latest"}, false, "Show the latest created container (includes all states)") 29 since = cmd.String([]string{"#-since"}, "", "Show containers created since Id or Name (includes all states)") 30 before = cmd.String([]string{"#-before"}, "", "Only show containers created before Id or Name") 31 last = cmd.Int([]string{"n"}, -1, "Show n last created containers (includes all states)") 32 format = cmd.String([]string{"-format"}, "", "Pretty-print containers using a Go template") 33 flFilter = opts.NewListOpts(nil) 34 ) 35 cmd.Require(flag.Exact, 0) 36 37 cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided") 38 39 cmd.ParseFlags(args, true) 40 if *last == -1 && *nLatest { 41 *last = 1 42 } 43 44 // Consolidate all filter flags, and sanity check them. 45 // They'll get processed in the daemon/server. 46 for _, f := range flFilter.GetAll() { 47 if psFilterArgs, err = filters.ParseFlag(f, psFilterArgs); err != nil { 48 return err 49 } 50 } 51 52 options := types.ContainerListOptions{ 53 All: *all, 54 Limit: *last, 55 Since: *since, 56 Before: *before, 57 Size: *size, 58 Filter: psFilterArgs, 59 } 60 61 containers, err := cli.client.ContainerList(context.Background(), options) 62 if err != nil { 63 return err 64 } 65 66 f := *format 67 if len(f) == 0 { 68 if len(cli.PsFormat()) > 0 && !*quiet { 69 f = cli.PsFormat() 70 } else { 71 f = "table" 72 } 73 } 74 75 psCtx := formatter.ContainerContext{ 76 Context: formatter.Context{ 77 Output: cli.out, 78 Format: f, 79 Quiet: *quiet, 80 Trunc: !*noTrunc, 81 }, 82 Size: *size, 83 Containers: containers, 84 } 85 86 psCtx.Write() 87 88 return nil 89 }