ps.go 2.4 KB

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