writer.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 RunWithLog(ctx context.Context, pf progressFunc, out *streams.Out, logConsumer api.LogConsumer) error {
  54. dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
  55. if !ok {
  56. dryRun = false
  57. }
  58. w := NewMixedWriter(out, logConsumer, dryRun)
  59. eg, _ := errgroup.WithContext(ctx)
  60. eg.Go(func() error {
  61. return w.Start(context.Background())
  62. })
  63. eg.Go(func() error {
  64. defer w.Stop()
  65. ctx = WithContextWriter(ctx, w)
  66. err := pf(ctx)
  67. return err
  68. })
  69. return eg.Wait()
  70. }
  71. func RunWithTitle(ctx context.Context, pf progressFunc, out *streams.Out, progressTitle string) error {
  72. _, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
  73. return "", pf(ctx)
  74. }, out, progressTitle)
  75. return err
  76. }
  77. // RunWithStatus will run a writer and the progress function in parallel and return a status
  78. func RunWithStatus(ctx context.Context, pf progressFuncWithStatus, out *streams.Out, progressTitle string) (string, error) {
  79. eg, _ := errgroup.WithContext(ctx)
  80. w, err := NewWriter(ctx, out, progressTitle)
  81. var result string
  82. if err != nil {
  83. return "", err
  84. }
  85. eg.Go(func() error {
  86. return w.Start(context.Background())
  87. })
  88. ctx = WithContextWriter(ctx, w)
  89. eg.Go(func() error {
  90. defer w.Stop()
  91. s, err := pf(ctx)
  92. if err == nil {
  93. result = s
  94. }
  95. return err
  96. })
  97. err = eg.Wait()
  98. return result, err
  99. }
  100. const (
  101. // ModeAuto detect console capabilities
  102. ModeAuto = "auto"
  103. // ModeTTY use terminal capability for advanced rendering
  104. ModeTTY = "tty"
  105. // ModePlain dump raw events to output
  106. ModePlain = "plain"
  107. // ModeQuiet don't display events
  108. ModeQuiet = "quiet"
  109. // ModeJSON outputs a machine-readable JSON stream
  110. ModeJSON = "json"
  111. )
  112. // Mode define how progress should be rendered, either as ModePlain or ModeTTY
  113. var Mode = ModeAuto
  114. // NewWriter returns a new multi-progress writer
  115. func NewWriter(ctx context.Context, out *streams.Out, progressTitle string) (Writer, error) {
  116. isTerminal := out.IsTerminal()
  117. dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
  118. if !ok {
  119. dryRun = false
  120. }
  121. switch Mode {
  122. case ModeQuiet:
  123. return quiet{}, nil
  124. case ModeJSON:
  125. return &jsonWriter{
  126. out: out,
  127. done: make(chan bool),
  128. dryRun: dryRun,
  129. }, nil
  130. case ModeTTY:
  131. return newTTYWriter(out, dryRun, progressTitle)
  132. case ModeAuto, "":
  133. if isTerminal {
  134. return newTTYWriter(out, dryRun, progressTitle)
  135. }
  136. fallthrough
  137. case ModePlain:
  138. return &plainWriter{
  139. out: out,
  140. done: make(chan bool),
  141. dryRun: dryRun,
  142. }, nil
  143. }
  144. return nil, fmt.Errorf("unknown progress mode: %s", Mode)
  145. }
  146. func newTTYWriter(out io.Writer, dryRun bool, progressTitle string) (Writer, error) {
  147. return &ttyWriter{
  148. out: out,
  149. eventIDs: []string{},
  150. events: map[string]Event{},
  151. repeated: false,
  152. done: make(chan bool),
  153. mtx: &sync.Mutex{},
  154. dryRun: dryRun,
  155. progressTitle: progressTitle,
  156. }, nil
  157. }