ps.go 2.9 KB

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