list.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. "errors"
  17. "fmt"
  18. "io"
  19. "regexp"
  20. "strings"
  21. "github.com/docker/cli/cli/command"
  22. "github.com/docker/cli/opts"
  23. "github.com/moby/moby/client"
  24. "github.com/spf13/cobra"
  25. "github.com/docker/compose/v5/cmd/formatter"
  26. "github.com/docker/compose/v5/pkg/api"
  27. "github.com/docker/compose/v5/pkg/compose"
  28. )
  29. type lsOptions struct {
  30. Format string
  31. Quiet bool
  32. All bool
  33. Filter opts.FilterOpt
  34. }
  35. func listCommand(dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  36. lsOpts := lsOptions{Filter: opts.NewFilterOpt()}
  37. lsCmd := &cobra.Command{
  38. Use: "ls [OPTIONS]",
  39. Short: "List running compose projects",
  40. RunE: Adapt(func(ctx context.Context, args []string) error {
  41. return runList(ctx, dockerCli, backendOptions, lsOpts)
  42. }),
  43. Args: cobra.NoArgs,
  44. ValidArgsFunction: noCompletion(),
  45. }
  46. lsCmd.Flags().StringVar(&lsOpts.Format, "format", "table", "Format the output. Values: [table | json]")
  47. lsCmd.Flags().BoolVarP(&lsOpts.Quiet, "quiet", "q", false, "Only display project names")
  48. lsCmd.Flags().Var(&lsOpts.Filter, "filter", "Filter output based on conditions provided")
  49. lsCmd.Flags().BoolVarP(&lsOpts.All, "all", "a", false, "Show all stopped Compose projects")
  50. return lsCmd
  51. }
  52. var acceptedListFilters = map[string]bool{
  53. "name": true,
  54. }
  55. // match returns true if any of the values at key match the source string
  56. func match(filters client.Filters, field, source string) bool {
  57. if f, ok := filters[field]; ok && f[source] {
  58. return true
  59. }
  60. fieldValues := filters[field]
  61. for name2match := range fieldValues {
  62. isMatch, err := regexp.MatchString(name2match, source)
  63. if err != nil {
  64. continue
  65. }
  66. if isMatch {
  67. return true
  68. }
  69. }
  70. return false
  71. }
  72. func runList(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, lsOpts lsOptions) error {
  73. filters := lsOpts.Filter.Value()
  74. for filter := range filters {
  75. if _, ok := acceptedListFilters[filter]; !ok {
  76. return errors.New("invalid filter '" + filter + "'")
  77. }
  78. }
  79. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  80. if err != nil {
  81. return err
  82. }
  83. stackList, err := backend.List(ctx, api.ListOptions{All: lsOpts.All})
  84. if err != nil {
  85. return err
  86. }
  87. if len(filters) > 0 {
  88. var filtered []api.Stack
  89. for _, s := range stackList {
  90. if match(filters, "name", s.Name) {
  91. filtered = append(filtered, s)
  92. }
  93. }
  94. stackList = filtered
  95. }
  96. if lsOpts.Quiet {
  97. for _, s := range stackList {
  98. _, _ = fmt.Fprintln(dockerCli.Out(), s.Name)
  99. }
  100. return nil
  101. }
  102. view := viewFromStackList(stackList)
  103. return formatter.Print(view, lsOpts.Format, dockerCli.Out(), func(w io.Writer) {
  104. for _, stack := range view {
  105. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", stack.Name, stack.Status, stack.ConfigFiles)
  106. }
  107. }, "NAME", "STATUS", "CONFIG FILES")
  108. }
  109. type stackView struct {
  110. Name string
  111. Status string
  112. ConfigFiles string
  113. }
  114. func viewFromStackList(stackList []api.Stack) []stackView {
  115. retList := make([]stackView, len(stackList))
  116. for i, s := range stackList {
  117. retList[i] = stackView{
  118. Name: s.Name,
  119. Status: strings.TrimSpace(fmt.Sprintf("%s %s", s.Status, s.Reason)),
  120. ConfigFiles: s.ConfigFiles,
  121. }
  122. }
  123. return retList
  124. }