ps.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cmd
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "os"
  19. "strings"
  20. formatter3 "github.com/docker/compose-cli/cmd/formatter"
  21. "github.com/pkg/errors"
  22. "github.com/spf13/cobra"
  23. "github.com/docker/compose-cli/api/client"
  24. "github.com/docker/compose-cli/api/containers"
  25. "github.com/docker/compose-cli/utils/formatter"
  26. )
  27. type psOpts struct {
  28. all bool
  29. quiet bool
  30. json bool
  31. format string
  32. }
  33. // PsCommand lists containers
  34. func PsCommand() *cobra.Command {
  35. var opts psOpts
  36. cmd := &cobra.Command{
  37. Use: "ps",
  38. Short: "List containers",
  39. RunE: func(cmd *cobra.Command, args []string) error {
  40. return runPs(cmd.Context(), opts)
  41. },
  42. }
  43. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
  44. cmd.Flags().BoolVarP(&opts.all, "all", "a", false, "Show all containers (default shows just running)")
  45. cmd.Flags().BoolVar(&opts.json, "json", false, "Format output as JSON")
  46. cmd.Flags().StringVar(&opts.format, "format", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
  47. _ = cmd.Flags().MarkHidden("json") // Legacy. This is used by VSCode Docker extension
  48. return cmd
  49. }
  50. func (o psOpts) validate() error {
  51. if o.quiet && o.json {
  52. return errors.New(`cannot combine "quiet" and "json" options`)
  53. }
  54. return nil
  55. }
  56. func runPs(ctx context.Context, opts psOpts) error {
  57. err := opts.validate()
  58. if err != nil {
  59. return err
  60. }
  61. c, err := client.New(ctx)
  62. if err != nil {
  63. return errors.Wrap(err, "cannot connect to backend")
  64. }
  65. containerList, err := c.ContainerService().List(ctx, opts.all)
  66. if err != nil {
  67. return errors.Wrap(err, "fetch containers")
  68. }
  69. if opts.quiet {
  70. for _, c := range containerList {
  71. fmt.Println(c.ID)
  72. }
  73. return nil
  74. }
  75. if opts.json {
  76. opts.format = formatter3.JSON
  77. }
  78. view := viewFromContainerList(containerList)
  79. return formatter3.Print(view, opts.format, os.Stdout, func(w io.Writer) {
  80. for _, c := range view {
  81. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", c.ID, c.Image, c.Command, c.Status,
  82. strings.Join(c.Ports, ", "))
  83. }
  84. }, "CONTAINER ID", "IMAGE", "COMMAND", "STATUS", "PORTS")
  85. }
  86. func fqdn(container containers.Container) string {
  87. fqdn := ""
  88. if container.Config != nil {
  89. fqdn = container.Config.FQDN
  90. }
  91. return fqdn
  92. }
  93. type containerView struct {
  94. ID string
  95. Image string
  96. Status string
  97. Command string
  98. Ports []string
  99. }
  100. func viewFromContainerList(containerList []containers.Container) []containerView {
  101. retList := make([]containerView, len(containerList))
  102. for i, c := range containerList {
  103. retList[i] = containerView{
  104. ID: c.ID,
  105. Image: c.Image,
  106. Status: c.Status,
  107. Command: c.Command,
  108. Ports: formatter.PortsToStrings(c.Ports, fqdn(c)),
  109. }
  110. }
  111. return retList
  112. }