1
0

list.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "strings"
  20. "github.com/docker/cli/opts"
  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/api/context/store"
  25. "github.com/docker/compose-cli/cli/formatter"
  26. )
  27. type lsOptions struct {
  28. Format string
  29. Quiet bool
  30. All bool
  31. Filter opts.FilterOpt
  32. }
  33. func listCommand(contextType string) *cobra.Command {
  34. opts := lsOptions{Filter: opts.NewFilterOpt()}
  35. lsCmd := &cobra.Command{
  36. Use: "ls",
  37. Short: "List running compose projects",
  38. RunE: func(cmd *cobra.Command, args []string) error {
  39. return runList(cmd.Context(), opts)
  40. },
  41. }
  42. lsCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
  43. lsCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs.")
  44. lsCmd.Flags().Var(&opts.Filter, "filter", "Filter output based on conditions provided.")
  45. if contextType == store.DefaultContextType {
  46. lsCmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all stopped Compose projects")
  47. }
  48. return lsCmd
  49. }
  50. var acceptedListFilters = map[string]bool{
  51. "name": true,
  52. }
  53. func runList(ctx context.Context, opts lsOptions) error {
  54. filters := opts.Filter.Value()
  55. err := filters.Validate(acceptedListFilters)
  56. if err != nil {
  57. return err
  58. }
  59. c, err := client.New(ctx)
  60. if err != nil {
  61. return err
  62. }
  63. stackList, err := c.ComposeService().List(ctx, compose.ListOptions{All: opts.All})
  64. if err != nil {
  65. return err
  66. }
  67. if opts.Quiet {
  68. for _, s := range stackList {
  69. fmt.Println(s.Name)
  70. }
  71. return nil
  72. }
  73. if filters.Len() > 0 {
  74. var filtered []compose.Stack
  75. for _, s := range stackList {
  76. if filters.Contains("name") && !filters.Match("name", s.Name) {
  77. continue
  78. }
  79. filtered = append(filtered, s)
  80. }
  81. stackList = filtered
  82. }
  83. view := viewFromStackList(stackList)
  84. return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) {
  85. for _, stack := range view {
  86. _, _ = fmt.Fprintf(w, "%s\t%s\n", stack.Name, stack.Status)
  87. }
  88. }, "NAME", "STATUS")
  89. }
  90. type stackView struct {
  91. Name string
  92. Status string
  93. }
  94. func viewFromStackList(stackList []compose.Stack) []stackView {
  95. retList := make([]stackView, len(stackList))
  96. for i, s := range stackList {
  97. retList[i] = stackView{
  98. Name: s.Name,
  99. Status: strings.TrimSpace(fmt.Sprintf("%s %s", s.Status, s.Reason)),
  100. }
  101. }
  102. return retList
  103. }