writer.go 3.9 KB

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