ps.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 compose
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "os"
  19. "sort"
  20. "strings"
  21. "github.com/spf13/cobra"
  22. "github.com/docker/compose-cli/api/client"
  23. "github.com/docker/compose-cli/api/compose"
  24. "github.com/docker/compose-cli/cli/formatter"
  25. "github.com/docker/compose-cli/utils"
  26. )
  27. type psOptions struct {
  28. *projectOptions
  29. Format string
  30. All bool
  31. Quiet bool
  32. Services bool
  33. }
  34. func psCommand(p *projectOptions) *cobra.Command {
  35. opts := psOptions{
  36. projectOptions: p,
  37. }
  38. psCmd := &cobra.Command{
  39. Use: "ps",
  40. Short: "List containers",
  41. RunE: func(cmd *cobra.Command, args []string) error {
  42. return runPs(cmd.Context(), opts)
  43. },
  44. }
  45. psCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
  46. psCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
  47. psCmd.Flags().BoolVar(&opts.Services, "services", false, "Display services")
  48. psCmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all stopped containers (including those created by the run command)")
  49. return psCmd
  50. }
  51. func runPs(ctx context.Context, opts psOptions) error {
  52. c, err := client.New(ctx)
  53. if err != nil {
  54. return err
  55. }
  56. projectName, err := opts.toProjectName()
  57. if err != nil {
  58. return err
  59. }
  60. containers, err := c.ComposeService().Ps(ctx, projectName, compose.PsOptions{
  61. All: opts.All,
  62. })
  63. if err != nil {
  64. return err
  65. }
  66. if opts.Services {
  67. services := []string{}
  68. for _, s := range containers {
  69. if !utils.StringContains(services, s.Service) {
  70. services = append(services, s.Service)
  71. }
  72. }
  73. fmt.Println(strings.Join(services, "\n"))
  74. return nil
  75. }
  76. if opts.Quiet {
  77. for _, s := range containers {
  78. fmt.Println(s.ID)
  79. }
  80. return nil
  81. }
  82. sort.Slice(containers, func(i, j int) bool {
  83. return containers[i].Name < containers[j].Name
  84. })
  85. return formatter.Print(containers, opts.Format, os.Stdout,
  86. func(w io.Writer) {
  87. for _, container := range containers {
  88. var ports []string
  89. for _, p := range container.Publishers {
  90. if p.URL == "" {
  91. ports = append(ports, fmt.Sprintf("%d/%s", p.TargetPort, p.Protocol))
  92. } else {
  93. ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol))
  94. }
  95. }
  96. status := container.State
  97. if container.Health != "" {
  98. status = fmt.Sprintf("%s (%s)", container.State, container.Health)
  99. }
  100. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", container.Name, container.Service, status, strings.Join(ports, ", "))
  101. }
  102. },
  103. "NAME", "SERVICE", "STATUS", "PORTS")
  104. }