list.go 3.1 KB

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