list.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. 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. func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) {
  65. options := &types.ContainerListOptions{
  66. All: opts.all,
  67. Limit: opts.last,
  68. Size: opts.size,
  69. Filters: opts.filter.Value(),
  70. }
  71. if opts.nLatest && opts.last == -1 {
  72. options.Limit = 1
  73. }
  74. tmpl, err := templates.Parse(opts.format)
  75. if err != nil {
  76. return nil, err
  77. }
  78. optionsProcessor := listOptionsProcessor{}
  79. // This shouldn't error out but swallowing the error makes it harder
  80. // to track down if preProcessor issues come up. Ref #24696
  81. if err := tmpl.Execute(ioutil.Discard, optionsProcessor); err != nil {
  82. return nil, err
  83. }
  84. // At the moment all we need is to capture .Size for preprocessor
  85. options.Size = opts.size || optionsProcessor["size"]
  86. return options, nil
  87. }
  88. func runPs(dockerCli *command.DockerCli, opts *psOptions) error {
  89. ctx := context.Background()
  90. listOptions, err := buildContainerListOptions(opts)
  91. if err != nil {
  92. return err
  93. }
  94. containers, err := dockerCli.Client().ContainerList(ctx, *listOptions)
  95. if err != nil {
  96. return err
  97. }
  98. format := opts.format
  99. if len(format) == 0 {
  100. if len(dockerCli.ConfigFile().PsFormat) > 0 && !opts.quiet {
  101. format = dockerCli.ConfigFile().PsFormat
  102. } else {
  103. format = formatter.TableFormatKey
  104. }
  105. }
  106. containerCtx := formatter.Context{
  107. Output: dockerCli.Out(),
  108. Format: formatter.NewContainerFormat(format, opts.quiet, listOptions.Size),
  109. Trunc: !opts.noTrunc,
  110. }
  111. return formatter.ContainerWrite(containerCtx, containers)
  112. }