ps.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/compose"
  23. "github.com/docker/compose-cli/cli/formatter"
  24. "github.com/docker/compose-cli/utils"
  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, backend compose.Service) *cobra.Command {
  34. opts := psOptions{
  35. projectOptions: p,
  36. }
  37. psCmd := &cobra.Command{
  38. Use: "ps",
  39. Short: "List containers",
  40. RunE: Adapt(func(ctx context.Context, args []string) error {
  41. return runPs(ctx, backend, args, 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, backend compose.Service, services []string, opts psOptions) error {
  51. projectName, err := opts.toProjectName()
  52. if err != nil {
  53. return err
  54. }
  55. containers, err := backend.Ps(ctx, projectName, compose.PsOptions{
  56. All: opts.All,
  57. Services: services,
  58. })
  59. if err != nil {
  60. return err
  61. }
  62. if opts.Services {
  63. services := []string{}
  64. for _, s := range containers {
  65. if !utils.StringContains(services, s.Service) {
  66. services = append(services, s.Service)
  67. }
  68. }
  69. fmt.Println(strings.Join(services, "\n"))
  70. return nil
  71. }
  72. if opts.Quiet {
  73. for _, s := range containers {
  74. fmt.Println(s.ID)
  75. }
  76. return nil
  77. }
  78. sort.Slice(containers, func(i, j int) bool {
  79. return containers[i].Name < containers[j].Name
  80. })
  81. return formatter.Print(containers, opts.Format, os.Stdout,
  82. func(w io.Writer) {
  83. for _, container := range containers {
  84. var ports []string
  85. for _, p := range container.Publishers {
  86. if p.URL == "" {
  87. ports = append(ports, fmt.Sprintf("%d/%s", p.TargetPort, p.Protocol))
  88. } else {
  89. ports = append(ports, fmt.Sprintf("%s->%d/%s", p.URL, p.TargetPort, p.Protocol))
  90. }
  91. }
  92. status := container.State
  93. if container.Health != "" {
  94. status = fmt.Sprintf("%s (%s)", container.State, container.Health)
  95. }
  96. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", container.Name, container.Service, status, strings.Join(ports, ", "))
  97. }
  98. },
  99. "NAME", "SERVICE", "STATUS", "PORTS")
  100. }