list.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. type preProcessor struct {
  52. types.Container
  53. opts *types.ContainerListOptions
  54. // Fields that need to exist so the template doesn't error out
  55. // These are needed since they are available on the final object but are not
  56. // fields in types.Container
  57. // TODO(cpuguy83): this seems rather broken
  58. Networks, CreatedAt, RunningFor bool
  59. }
  60. // Size sets the size option when called by a template execution.
  61. func (p *preProcessor) Size() bool {
  62. p.opts.Size = true
  63. return true
  64. }
  65. func buildContainerListOptions(opts *psOptions) (*types.ContainerListOptions, error) {
  66. options := &types.ContainerListOptions{
  67. All: opts.all,
  68. Limit: opts.last,
  69. Size: opts.size,
  70. Filters: opts.filter.Value(),
  71. }
  72. if opts.nLatest && opts.last == -1 {
  73. options.Limit = 1
  74. }
  75. // Currently only used with Size, so we can determine if the user
  76. // put {{.Size}} in their format.
  77. pre := &preProcessor{opts: options}
  78. tmpl, err := templates.Parse(opts.format)
  79. if err != nil {
  80. return nil, err
  81. }
  82. // This shouldn't error out but swallowing the error makes it harder
  83. // to track down if preProcessor issues come up. Ref #24696
  84. if err := tmpl.Execute(ioutil.Discard, pre); err != nil {
  85. return nil, err
  86. }
  87. return options, nil
  88. }
  89. func runPs(dockerCli *command.DockerCli, opts *psOptions) error {
  90. ctx := context.Background()
  91. listOptions, err := buildContainerListOptions(opts)
  92. if err != nil {
  93. return err
  94. }
  95. containers, err := dockerCli.Client().ContainerList(ctx, *listOptions)
  96. if err != nil {
  97. return err
  98. }
  99. format := opts.format
  100. if len(format) == 0 {
  101. if len(dockerCli.ConfigFile().PsFormat) > 0 && !opts.quiet {
  102. format = dockerCli.ConfigFile().PsFormat
  103. } else {
  104. format = formatter.TableFormatKey
  105. }
  106. }
  107. containerCtx := formatter.Context{
  108. Output: dockerCli.Out(),
  109. Format: formatter.NewContainerFormat(format, opts.quiet, listOptions.Size),
  110. Trunc: !opts.noTrunc,
  111. }
  112. return formatter.ContainerWrite(containerCtx, containers)
  113. }