writer.go 4.1 KB

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