writer.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. const (
  24. DRYRUN_PREFIX = " DRY-RUN MODE - "
  25. )
  26. // Writer can write multiple progress events
  27. type Writer interface {
  28. Start(context.Context) error
  29. Stop()
  30. Event(Event)
  31. Events([]Event)
  32. TailMsgf(string, ...interface{})
  33. }
  34. type writerKey struct{}
  35. // WithContextWriter adds the writer to the context
  36. func WithContextWriter(ctx context.Context, writer Writer) context.Context {
  37. return context.WithValue(ctx, writerKey{}, writer)
  38. }
  39. // ContextWriter returns the writer from the context
  40. func ContextWriter(ctx context.Context) Writer {
  41. s, ok := ctx.Value(writerKey{}).(Writer)
  42. if !ok {
  43. return &noopWriter{}
  44. }
  45. return s
  46. }
  47. type progressFunc func(context.Context) error
  48. type progressFuncWithStatus func(context.Context) (string, error)
  49. // Run will run a writer and the progress function in parallel
  50. func Run(ctx context.Context, pf progressFunc) error {
  51. _, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
  52. return "", pf(ctx)
  53. })
  54. return err
  55. }
  56. // RunWithStatus will run a writer and the progress function in parallel and return a status
  57. func RunWithStatus(ctx context.Context, pf progressFuncWithStatus) (string, error) {
  58. eg, _ := errgroup.WithContext(ctx)
  59. w, err := NewWriter(ctx, os.Stderr)
  60. var result string
  61. if err != nil {
  62. return "", err
  63. }
  64. eg.Go(func() error {
  65. return w.Start(context.Background())
  66. })
  67. ctx = WithContextWriter(ctx, w)
  68. eg.Go(func() error {
  69. defer w.Stop()
  70. s, err := pf(ctx)
  71. if err == nil {
  72. result = s
  73. }
  74. return err
  75. })
  76. err = eg.Wait()
  77. return result, err
  78. }
  79. const (
  80. // ModeAuto detect console capabilities
  81. ModeAuto = "auto"
  82. // ModeTTY use terminal capability for advanced rendering
  83. ModeTTY = "tty"
  84. // ModePlain dump raw events to output
  85. ModePlain = "plain"
  86. )
  87. // Mode define how progress should be rendered, either as ModePlain or ModeTTY
  88. var Mode = ModeAuto
  89. // NewWriter returns a new multi-progress writer
  90. func NewWriter(ctx context.Context, out console.File) (Writer, error) {
  91. _, isTerminal := term.GetFdInfo(out)
  92. dryRun, ok := ctx.Value(api.DryRunKey{}).(bool)
  93. if !ok {
  94. dryRun = false
  95. }
  96. if Mode == ModeAuto && isTerminal {
  97. return newTTYWriter(out, dryRun)
  98. }
  99. if Mode == ModeTTY {
  100. return newTTYWriter(out, dryRun)
  101. }
  102. return &plainWriter{
  103. out: out,
  104. done: make(chan bool),
  105. dryRun: dryRun,
  106. }, nil
  107. }
  108. func newTTYWriter(out console.File, dryRun bool) (Writer, error) {
  109. con, err := console.ConsoleFromFile(out)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return &ttyWriter{
  114. out: con,
  115. eventIDs: []string{},
  116. events: map[string]Event{},
  117. repeated: false,
  118. done: make(chan bool),
  119. mtx: &sync.Mutex{},
  120. dryRun: dryRun,
  121. }, nil
  122. }