ps.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. "os"
  18. "strings"
  19. "text/tabwriter"
  20. "github.com/pkg/errors"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/compose-cli/api/client"
  23. "github.com/docker/compose-cli/api/containers"
  24. formatter2 "github.com/docker/compose-cli/formatter"
  25. "github.com/docker/compose-cli/utils/formatter"
  26. )
  27. type psOpts struct {
  28. all bool
  29. quiet bool
  30. json bool
  31. }
  32. func (o psOpts) validate() error {
  33. if o.quiet && o.json {
  34. return errors.New(`cannot combine "quiet" and "json" options`)
  35. }
  36. return nil
  37. }
  38. // PsCommand lists containers
  39. func PsCommand() *cobra.Command {
  40. var opts psOpts
  41. cmd := &cobra.Command{
  42. Use: "ps",
  43. Short: "List containers",
  44. RunE: func(cmd *cobra.Command, args []string) error {
  45. return runPs(cmd.Context(), opts)
  46. },
  47. }
  48. cmd.Flags().BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs")
  49. cmd.Flags().BoolVarP(&opts.all, "all", "a", false, "Show all containers (default shows just running)")
  50. cmd.Flags().BoolVar(&opts.json, "json", false, "Format output as JSON")
  51. return cmd
  52. }
  53. func runPs(ctx context.Context, opts psOpts) error {
  54. err := opts.validate()
  55. if err != nil {
  56. return err
  57. }
  58. c, err := client.New(ctx)
  59. if err != nil {
  60. return errors.Wrap(err, "cannot connect to backend")
  61. }
  62. containers, err := c.ContainerService().List(ctx, opts.all)
  63. if err != nil {
  64. return errors.Wrap(err, "fetch containers")
  65. }
  66. if opts.quiet {
  67. for _, c := range containers {
  68. fmt.Println(c.ID)
  69. }
  70. return nil
  71. }
  72. if opts.json {
  73. j, err := formatter2.ToStandardJSON(containers)
  74. if err != nil {
  75. return err
  76. }
  77. fmt.Println(j)
  78. return nil
  79. }
  80. w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
  81. fmt.Fprintf(w, "CONTAINER ID\tIMAGE\tCOMMAND\tSTATUS\tPORTS\n")
  82. format := "%s\t%s\t%s\t%s\t%s\n"
  83. for _, container := range containers {
  84. fmt.Fprintf(w, format, container.ID, container.Image, container.Command, container.Status, strings.Join(formatter.PortsToStrings(container.Ports, fqdn(container)), ", "))
  85. }
  86. return w.Flush()
  87. }
  88. func fqdn(container containers.Container) string {
  89. fqdn := ""
  90. if container.Config != nil {
  91. fqdn = container.Config.FQDN
  92. }
  93. return fqdn
  94. }