writer.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. "os"
  17. "sync"
  18. "github.com/docker/compose/v2/pkg/api"
  19. "github.com/containerd/console"
  20. "github.com/moby/term"
  21. "golang.org/x/sync/errgroup"
  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) error {
  48. _, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
  49. return "", pf(ctx)
  50. })
  51. return err
  52. }
  53. // RunWithStatus will run a writer and the progress function in parallel and return a status
  54. func RunWithStatus(ctx context.Context, pf progressFuncWithStatus) (string, error) {
  55. eg, _ := errgroup.WithContext(ctx)
  56. w, err := NewWriter(ctx, os.Stderr)
  57. var result string
  58. if err != nil {
  59. return "", err
  60. }
  61. eg.Go(func() error {
  62. return w.Start(context.Background())
  63. })
  64. ctx = WithContextWriter(ctx, w)
  65. eg.Go(func() error {
  66. defer w.Stop()
  67. s, err := pf(ctx)
  68. if err == nil {
  69. result = s
  70. }
  71. return err
  72. })
  73. err = eg.Wait()
  74. return result, err
  75. }
  76. const (
  77. // ModeAuto detect console capabilities
  78. ModeAuto = "auto"
  79. // ModeTTY use terminal capability for advanced rendering
  80. ModeTTY = "tty"
  81. // ModePlain dump raw events to output
  82. ModePlain = "plain"
  83. )
  84. // Mode define how progress should be rendered, either as ModePlain or ModeTTY
  85. var Mode = ModeAuto
  86. // NewWriter returns a new multi-progress writer
  87. func NewWriter(ctx context.Context, out console.File) (Writer, error) {
  88. _, isTerminal := term.GetFdInfo(out)
  89. dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
  90. if !ok {
  91. dryRun = false
  92. }
  93. if Mode == ModeAuto && isTerminal {
  94. return newTTYWriter(out, dryRun)
  95. }
  96. if Mode == ModeTTY {
  97. return newTTYWriter(out, dryRun)
  98. }
  99. return &plainWriter{
  100. out: out,
  101. done: make(chan bool),
  102. dryRun: dryRun,
  103. }, nil
  104. }
  105. func newTTYWriter(out console.File, dryRun bool) (Writer, error) {
  106. con, err := console.ConsoleFromFile(out)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return &ttyWriter{
  111. out: con,
  112. eventIDs: []string{},
  113. events: map[string]Event{},
  114. repeated: false,
  115. done: make(chan bool),
  116. mtx: &sync.Mutex{},
  117. dryRun: dryRun,
  118. }, nil
  119. }