list.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package container
  2. import (
  3. "io/ioutil"
  4. "github.com/docker/docker/api/types"
  5. "github.com/docker/docker/cli"
  6. "github.com/docker/docker/cli/command"
  7. "github.com/docker/docker/cli/command/formatter"
  8. "github.com/docker/docker/opts"
  9. "github.com/docker/docker/pkg/templates"
  10. "github.com/spf13/cobra"
  11. "golang.org/x/net/context"
  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. func newListCommand(dockerCli *command.DockerCli) *cobra.Command {
  46. cmd := *NewPsCommand(dockerCli)
  47. cmd.Aliases = []string{"ps", "list"}
  48. cmd.Use = "ls [OPTIONS]"
  49. return &cmd
  50. }
  51. // listOptionsProcessor is used to set any container list options which may only
  52. // be embedded in the format template.
  53. // This is passed directly into tmpl.Execute in order to allow the preprocessor
  54. // to set any list options that were not provided by flags (e.g. `.Size`).
  55. // It is using a `map[string]bool` so that unknown fields passed into the
  56. // template format do not cause errors. These errors will get picked up when
  57. // running through the actual template processor.
  58. type listOptionsProcessor map[string]bool
  59. // Size sets the size of the map when called by a template execution.
  60. func (o listOptionsProcessor) Size() bool {
  61. o["size"] = true
  62. return true
  63. }
  64. // Label is needed here as it allows the correct pre-processing
  65. // because Label() is a method with arguments
  66. func (o listOptionsProcessor) Label(name string) string {
  67. return ""
  68. }
  69. func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) {
  70. options := &types.ContainerListOptions{
  71. All: opts.all,
  72. Limit: opts.last,
  73. Size: opts.size,
  74. Filters: opts.filter.Value(),
  75. }
  76. if opts.nLatest && opts.last == -1 {
  77. options.Limit = 1
  78. }
  79. tmpl, err := templates.Parse(opts.format)
  80. if err != nil {
  81. return nil, err
  82. }
  83. optionsProcessor := listOptionsProcessor{}
  84. // This shouldn't error out but swallowing the error makes it harder
  85. // to track down if preProcessor issues come up. Ref #24696
  86. if err := tmpl.Execute(ioutil.Discard, optionsProcessor); err != nil {
  87. return nil, err
  88. }
  89. // At the moment all we need is to capture .Size for preprocessor
  90. options.Size = opts.size || optionsProcessor["size"]
  91. return options, nil
  92. }
  93. func runPs(dockerCli *command.DockerCli, opts *psOptions) error {
  94. ctx := context.Background()
  95. listOptions, err := buildContainerListOptions(opts)
  96. if err != nil {
  97. return err
  98. }
  99. containers, err := dockerCli.Client().ContainerList(ctx, *listOptions)
  100. if err != nil {
  101. return err
  102. }
  103. format := opts.format
  104. if len(format) == 0 {
  105. if len(dockerCli.ConfigFile().PsFormat) > 0 && !opts.quiet {
  106. format = dockerCli.ConfigFile().PsFormat
  107. } else {
  108. format = formatter.TableFormatKey
  109. }
  110. }
  111. containerCtx := formatter.Context{
  112. Output: dockerCli.Out(),
  113. Format: formatter.NewContainerFormat(format, opts.quiet, listOptions.Size),
  114. Trunc: !opts.noTrunc,
  115. }
  116. return formatter.ContainerWrite(containerCtx, containers)
  117. }