ps.go 2.2 KB

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