list.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/context/store"
  23. "github.com/docker/compose-cli/cli/formatter"
  24. "github.com/docker/compose-cli/pkg/api"
  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 api.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. ValidArgsFunction: noCompletion(),
  41. }
  42. lsCmd.Flags().StringVar(&opts.Format, "format", "pretty", "Format the output. Values: [pretty | json].")
  43. lsCmd.Flags().BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs.")
  44. lsCmd.Flags().Var(&opts.Filter, "filter", "Filter output based on conditions provided.")
  45. if contextType == store.DefaultContextType {
  46. lsCmd.Flags().BoolVarP(&opts.All, "all", "a", false, "Show all stopped Compose projects")
  47. }
  48. return lsCmd
  49. }
  50. var acceptedListFilters = map[string]bool{
  51. "name": true,
  52. }
  53. func runList(ctx context.Context, backend api.Service, opts lsOptions) error {
  54. filters := opts.Filter.Value()
  55. err := filters.Validate(acceptedListFilters)
  56. if err != nil {
  57. return err
  58. }
  59. stackList, err := backend.List(ctx, api.ListOptions{All: opts.All})
  60. if err != nil {
  61. return err
  62. }
  63. if opts.Quiet {
  64. for _, s := range stackList {
  65. fmt.Println(s.Name)
  66. }
  67. return nil
  68. }
  69. if filters.Len() > 0 {
  70. var filtered []api.Stack
  71. for _, s := range stackList {
  72. if filters.Contains("name") && !filters.Match("name", s.Name) {
  73. continue
  74. }
  75. filtered = append(filtered, s)
  76. }
  77. stackList = filtered
  78. }
  79. view := viewFromStackList(stackList)
  80. return formatter.Print(view, opts.Format, os.Stdout, func(w io.Writer) {
  81. for _, stack := range view {
  82. _, _ = fmt.Fprintf(w, "%s\t%s\n", stack.Name, stack.Status)
  83. }
  84. }, "NAME", "STATUS")
  85. }
  86. type stackView struct {
  87. Name string
  88. Status string
  89. }
  90. func viewFromStackList(stackList []api.Stack) []stackView {
  91. retList := make([]stackView, len(stackList))
  92. for i, s := range stackList {
  93. retList[i] = stackView{
  94. Name: s.Name,
  95. Status: strings.TrimSpace(fmt.Sprintf("%s %s", s.Status, s.Reason)),
  96. }
  97. }
  98. return retList
  99. }