writer.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. )
  91. // Mode define how progress should be rendered, either as ModePlain or ModeTTY
  92. var Mode = ModeAuto
  93. // NewWriter returns a new multi-progress writer
  94. func NewWriter(ctx context.Context, out io.Writer, progressTitle string) (Writer, error) {
  95. _, isTerminal := term.GetFdInfo(out)
  96. dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
  97. if !ok {
  98. dryRun = false
  99. }
  100. f, isConsole := out.(console.File) // see https://github.com/docker/compose/issues/10560
  101. if Mode == ModeAuto && isTerminal && isConsole {
  102. return newTTYWriter(f, dryRun, progressTitle)
  103. }
  104. if Mode == ModeTTY {
  105. if !isConsole {
  106. logrus.Warn("Terminal is not a POSIX console")
  107. } else {
  108. return newTTYWriter(f, dryRun, progressTitle)
  109. }
  110. }
  111. return &plainWriter{
  112. out: out,
  113. done: make(chan bool),
  114. dryRun: dryRun,
  115. }, nil
  116. }
  117. func newTTYWriter(out console.File, dryRun bool, progressTitle string) (Writer, error) {
  118. con, err := console.ConsoleFromFile(out)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return &ttyWriter{
  123. out: con,
  124. eventIDs: []string{},
  125. events: map[string]Event{},
  126. repeated: false,
  127. done: make(chan bool),
  128. mtx: &sync.Mutex{},
  129. dryRun: dryRun,
  130. progressTitle: progressTitle,
  131. }, nil
  132. }