ps.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package container
  2. import (
  3. "io/ioutil"
  4. "golang.org/x/net/context"
  5. "github.com/docker/docker/api/types"
  6. "github.com/docker/docker/cli"
  7. "github.com/docker/docker/cli/command"
  8. "github.com/docker/docker/cli/command/formatter"
  9. "github.com/docker/docker/opts"
  10. "github.com/docker/docker/utils/templates"
  11. "github.com/spf13/cobra"
  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 opts.FilterOpt
  22. }
  23. // NewPsCommand creates a new cobra.Command for `docker ps`
  24. func NewPsCommand(dockerCli *command.DockerCli) *cobra.Command {
  25. opts := psOptions{filter: opts.NewFilterOpt()}
  26. cmd := &cobra.Command{
  27. Use: "ps [OPTIONS]",
  28. Short: "List containers",
  29. Args: cli.NoArgs,
  30. RunE: func(cmd *cobra.Command, args []string) error {
  31. return runPs(dockerCli, &opts)
  32. },
  33. }
  34. flags := cmd.Flags()
  35. flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display numeric IDs")
  36. flags.BoolVarP(&opts.size, "size", "s", false, "Display total file sizes")
  37. flags.BoolVarP(&opts.all, "all", "a", false, "Show all containers (default shows just running)")
  38. flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
  39. flags.BoolVarP(&opts.nLatest, "latest", "l", false, "Show the latest created container (includes all states)")
  40. flags.IntVarP(&opts.last, "last", "n", -1, "Show n last created containers (includes all states)")
  41. flags.StringVarP(&opts.format, "format", "", "", "Pretty-print containers using a Go template")
  42. flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided")
  43. return cmd
  44. }
  45. type preProcessor struct {
  46. types.Container
  47. opts *types.ContainerListOptions
  48. }
  49. // Size sets the size option when called by a template execution.
  50. func (p *preProcessor) Size() bool {
  51. p.opts.Size = true
  52. return true
  53. }
  54. func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) {
  55. options := &types.ContainerListOptions{
  56. All: opts.all,
  57. Limit: opts.last,
  58. Size: opts.size,
  59. Filter: opts.filter.Value(),
  60. }
  61. if opts.nLatest && opts.last == -1 {
  62. options.Limit = 1
  63. }
  64. // Currently only used with Size, so we can determine if the user
  65. // put {{.Size}} in their format.
  66. pre := &preProcessor{opts: options}
  67. tmpl, err := templates.Parse(opts.format)
  68. if err != nil {
  69. return nil, err
  70. }
  71. // This shouldn't error out but swallowing the error makes it harder
  72. // to track down if preProcessor issues come up. Ref #24696
  73. if err := tmpl.Execute(ioutil.Discard, pre); err != nil {
  74. return nil, err
  75. }
  76. return options, nil
  77. }
  78. func runPs(dockerCli *command.DockerCli, opts *psOptions) error {
  79. ctx := context.Background()
  80. listOptions, err := buildContainerListOptions(opts)
  81. if err != nil {
  82. return err
  83. }
  84. containers, err := dockerCli.Client().ContainerList(ctx, *listOptions)
  85. if err != nil {
  86. return err
  87. }
  88. f := opts.format
  89. if len(f) == 0 {
  90. if len(dockerCli.ConfigFile().PsFormat) > 0 && !opts.quiet {
  91. f = dockerCli.ConfigFile().PsFormat
  92. } else {
  93. f = "table"
  94. }
  95. }
  96. psCtx := formatter.ContainerContext{
  97. Context: formatter.Context{
  98. Output: dockerCli.Out(),
  99. Format: f,
  100. Quiet: opts.quiet,
  101. Trunc: !opts.noTrunc,
  102. },
  103. Size: listOptions.Size,
  104. Containers: containers,
  105. }
  106. psCtx.Write()
  107. return nil
  108. }