list.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/compose"
  23. "github.com/docker/compose-cli/api/context/store"
  24. "github.com/docker/compose-cli/cli/formatter"
  25. )
  26. type lsOptions struct {
  27. Format string
  28. Quiet bool
  29. All bool
  30. Filter opts.FilterOpt
  31. }
  32. func listCommand(contextType string, backend compose.Service) *cobra.Command {
  33. opts := lsOptions{Filter: opts.NewFilterOpt()}
  34. lsCmd := &cobra.Command{
  35. Use: "ls",
  36. Short: "List running compose projects",
  37. RunE: Adapt(func(ctx context.Context, args []string) error {
  38. return runList(ctx, backend, opts)
  39. }),
  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. if contextType == store.DefaultContextType {
  45. lsCmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all stopped Compose projects")
  46. }
  47. return lsCmd
  48. }
  49. var acceptedListFilters = map[string]bool{
  50. "name": true,
  51. }
  52. func runList(ctx context.Context, backend compose.Service, opts lsOptions) error {
  53. filters := opts.Filter.Value()
  54. err := filters.Validate(acceptedListFilters)
  55. if err != nil {
  56. return err
  57. }
  58. stackList, err := backend.List(ctx, compose.ListOptions{All: opts.All})
  59. if err != nil {
  60. return err
  61. }
  62. if opts.Quiet {
  63. for _, s := range stackList {
  64. fmt.Println(s.Name)
  65. }
  66. return nil
  67. }
  68. if filters.Len() > 0 {
  69. var filtered []compose.Stack
  70. for _, s := range stackList {
  71. if filters.Contains("name") && !filters.Match("name", s.Name) {
  72. continue
  73. }
  74. filtered = append(filtered, s)
  75. }
  76. stackList = filtered
  77. }
  78. view := viewFromStackList(stackList)
  79. return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) {
  80. for _, stack := range view {
  81. _, _ = fmt.Fprintf(w, "%s\t%s\n", stack.Name, stack.Status)
  82. }
  83. }, "NAME", "STATUS")
  84. }
  85. type stackView struct {
  86. Name string
  87. Status string
  88. }
  89. func viewFromStackList(stackList []compose.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. }
  96. }
  97. return retList
  98. }