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