ps.go 3.1 KB

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