writer.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 progress
  14. import (
  15. "context"
  16. "fmt"
  17. "io"
  18. "sync"
  19. "github.com/docker/cli/cli/streams"
  20. "golang.org/x/sync/errgroup"
  21. "github.com/docker/compose/v2/pkg/api"
  22. )
  23. // Writer can write multiple progress events
  24. type Writer interface {
  25. Start(context.Context) error
  26. Stop()
  27. Event(Event)
  28. Events([]Event)
  29. TailMsgf(string, ...interface{})
  30. }
  31. type writerKey struct{}
  32. // WithContextWriter adds the writer to the context
  33. func WithContextWriter(ctx context.Context, writer Writer) context.Context {
  34. return context.WithValue(ctx, writerKey{}, writer)
  35. }
  36. // ContextWriter returns the writer from the context
  37. func ContextWriter(ctx context.Context) Writer {
  38. s, ok := ctx.Value(writerKey{}).(Writer)
  39. if !ok {
  40. return &noopWriter{}
  41. }
  42. return s
  43. }
  44. type progressFunc func(context.Context) error
  45. type progressFuncWithStatus func(context.Context) (string, error)
  46. // Run will run a writer and the progress function in parallel
  47. func Run(ctx context.Context, pf progressFunc, out *streams.Out) error {
  48. _, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
  49. return "", pf(ctx)
  50. }, out, "Running")
  51. return err
  52. }
  53. func RunWithTitle(ctx context.Context, pf progressFunc, out *streams.Out, progressTitle string) error {
  54. _, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
  55. return "", pf(ctx)
  56. }, out, progressTitle)
  57. return err
  58. }
  59. // RunWithStatus will run a writer and the progress function in parallel and return a status
  60. func RunWithStatus(ctx context.Context, pf progressFuncWithStatus, out *streams.Out, progressTitle string) (string, error) {
  61. eg, _ := errgroup.WithContext(ctx)
  62. w, err := NewWriter(ctx, out, progressTitle)
  63. var result string
  64. if err != nil {
  65. return "", err
  66. }
  67. eg.Go(func() error {
  68. return w.Start(context.Background())
  69. })
  70. ctx = WithContextWriter(ctx, w)
  71. eg.Go(func() error {
  72. defer w.Stop()
  73. s, err := pf(ctx)
  74. if err == nil {
  75. result = s
  76. }
  77. return err
  78. })
  79. err = eg.Wait()
  80. return result, err
  81. }
  82. const (
  83. // ModeAuto detect console capabilities
  84. ModeAuto = "auto"
  85. // ModeTTY use terminal capability for advanced rendering
  86. ModeTTY = "tty"
  87. // ModePlain dump raw events to output
  88. ModePlain = "plain"
  89. // ModeQuiet don't display events
  90. ModeQuiet = "quiet"
  91. // ModeJSON outputs a machine-readable JSON stream
  92. ModeJSON = "json"
  93. )
  94. // Mode define how progress should be rendered, either as ModePlain or ModeTTY
  95. var Mode = ModeAuto
  96. // NewWriter returns a new multi-progress writer
  97. func NewWriter(ctx context.Context, out *streams.Out, progressTitle string) (Writer, error) {
  98. isTerminal := out.IsTerminal()
  99. dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
  100. if !ok {
  101. dryRun = false
  102. }
  103. switch Mode {
  104. case ModeQuiet:
  105. return quiet{}, nil
  106. case ModeJSON:
  107. return &jsonWriter{
  108. out: out,
  109. done: make(chan bool),
  110. dryRun: dryRun,
  111. }, nil
  112. case ModeTTY:
  113. return newTTYWriter(out, dryRun, progressTitle)
  114. case ModeAuto, "":
  115. if isTerminal {
  116. return newTTYWriter(out, dryRun, progressTitle)
  117. }
  118. fallthrough
  119. case ModePlain:
  120. return &plainWriter{
  121. out: out,
  122. done: make(chan bool),
  123. dryRun: dryRun,
  124. }, nil
  125. }
  126. return nil, fmt.Errorf("unknown progress mode: %s", Mode)
  127. }
  128. func newTTYWriter(out io.Writer, dryRun bool, progressTitle string) (Writer, error) {
  129. return &ttyWriter{
  130. out: out,
  131. eventIDs: []string{},
  132. events: map[string]Event{},
  133. repeated: false,
  134. done: make(chan bool),
  135. mtx: &sync.Mutex{},
  136. dryRun: dryRun,
  137. progressTitle: progressTitle,
  138. }, nil
  139. }