list.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "strings"
  19. "github.com/docker/cli/cli/command"
  20. "github.com/docker/compose/v2/cmd/formatter"
  21. "github.com/docker/compose/v2/pkg/compose"
  22. "github.com/docker/cli/opts"
  23. "github.com/spf13/cobra"
  24. "github.com/docker/compose/v2/pkg/api"
  25. )
  26. type lsOptions struct {
  27. Format string
  28. Quiet bool
  29. All bool
  30. Filter opts.FilterOpt
  31. }
  32. func listCommand(dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {
  33. lsOpts := lsOptions{Filter: opts.NewFilterOpt()}
  34. lsCmd := &cobra.Command{
  35. Use: "ls [OPTIONS]",
  36. Short: "List running compose projects",
  37. RunE: Adapt(func(ctx context.Context, args []string) error {
  38. return runList(ctx, dockerCli, backendOptions, lsOpts)
  39. }),
  40. Args: cobra.NoArgs,
  41. ValidArgsFunction: noCompletion(),
  42. }
  43. lsCmd.Flags().StringVar(&lsOpts.Format, "format", "table", "Format the output. Values: [table | json]")
  44. lsCmd.Flags().BoolVarP(&lsOpts.Quiet, "quiet", "q", false, "Only display project names")
  45. lsCmd.Flags().Var(&lsOpts.Filter, "filter", "Filter output based on conditions provided")
  46. lsCmd.Flags().BoolVarP(&lsOpts.All, "all", "a", false, "Show all stopped Compose projects")
  47. return lsCmd
  48. }
  49. var acceptedListFilters = map[string]bool{
  50. "name": true,
  51. }
  52. func runList(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, lsOpts lsOptions) error {
  53. filters := lsOpts.Filter.Value()
  54. err := filters.Validate(acceptedListFilters)
  55. if err != nil {
  56. return err
  57. }
  58. backend, err := compose.NewComposeService(dockerCli, backendOptions.Options...)
  59. if err != nil {
  60. return err
  61. }
  62. stackList, err := backend.List(ctx, api.ListOptions{All: lsOpts.All})
  63. if err != nil {
  64. return err
  65. }
  66. if filters.Len() > 0 {
  67. var filtered []api.Stack
  68. for _, s := range stackList {
  69. if filters.Contains("name") && !filters.Match("name", s.Name) {
  70. continue
  71. }
  72. filtered = append(filtered, s)
  73. }
  74. stackList = filtered
  75. }
  76. if lsOpts.Quiet {
  77. for _, s := range stackList {
  78. _, _ = fmt.Fprintln(dockerCli.Out(), s.Name)
  79. }
  80. return nil
  81. }
  82. view := viewFromStackList(stackList)
  83. return formatter.Print(view, lsOpts.Format, dockerCli.Out(), func(w io.Writer) {
  84. for _, stack := range view {
  85. _, _ = fmt.Fprintf(w, "%s\t%s\t%s\n", stack.Name, stack.Status, stack.ConfigFiles)
  86. }
  87. }, "NAME", "STATUS", "CONFIG FILES")
  88. }
  89. type stackView struct {
  90. Name string
  91. Status string
  92. ConfigFiles string
  93. }
  94. func viewFromStackList(stackList []api.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. ConfigFiles: s.ConfigFiles,
  101. }
  102. }
  103. return retList
  104. }