list.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/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(backend api.Service) *cobra.Command {
  32. opts := lsOptions{Filter: opts.NewFilterOpt()}
  33. lsCmd := &cobra.Command{
  34. Use: "ls",
  35. Short: "List running compose projects",
  36. RunE: Adapt(func(ctx context.Context, args []string) error {
  37. return runList(ctx, backend, opts)
  38. }),
  39. ValidArgsFunction: noCompletion(),
  40. }
  41. lsCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
  42. lsCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs.")
  43. lsCmd.Flags().Var(&opts.Filter, "filter", "Filter output based on conditions provided.")
  44. lsCmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all stopped Compose projects")
  45. return lsCmd
  46. }
  47. var acceptedListFilters = map[string]bool{
  48. "name": true,
  49. }
  50. func runList(ctx context.Context, backend api.Service, opts lsOptions) error {
  51. filters := opts.Filter.Value()
  52. err := filters.Validate(acceptedListFilters)
  53. if err != nil {
  54. return err
  55. }
  56. stackList, err := backend.List(ctx, api.ListOptions{All: opts.All})
  57. if err != nil {
  58. return err
  59. }
  60. if opts.Quiet {
  61. for _, s := range stackList {
  62. fmt.Println(s.Name)
  63. }
  64. return nil
  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. view := viewFromStackList(stackList)
  77. return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) {
  78. for _, stack := range view {
  79. _, _ = fmt.Fprintf(w, "%s\t%s\n", stack.Name, stack.Status)
  80. }
  81. }, "NAME", "STATUS")
  82. }
  83. type stackView struct {
  84. Name string
  85. Status string
  86. }
  87. func viewFromStackList(stackList []api.Stack) []stackView {
  88. retList := make([]stackView, len(stackList))
  89. for i, s := range stackList {
  90. retList[i] = stackView{
  91. Name: s.Name,
  92. Status: strings.TrimSpace(fmt.Sprintf("%s %s", s.Status, s.Reason)),
  93. }
  94. }
  95. return retList
  96. }