writer.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/containerd/console"
  19. "github.com/moby/term"
  20. "golang.org/x/sync/errgroup"
  21. )
  22. // Writer can write multiple progress events
  23. type Writer interface {
  24. Start(context.Context) error
  25. Stop()
  26. Event(Event)
  27. TailMsgf(string, ...interface{})
  28. }
  29. type writerKey struct{}
  30. // WithContextWriter adds the writer to the context
  31. func WithContextWriter(ctx context.Context, writer Writer) context.Context {
  32. return context.WithValue(ctx, writerKey{}, writer)
  33. }
  34. // ContextWriter returns the writer from the context
  35. func ContextWriter(ctx context.Context) Writer {
  36. s, ok := ctx.Value(writerKey{}).(Writer)
  37. if !ok {
  38. return &noopWriter{}
  39. }
  40. return s
  41. }
  42. type progressFunc func(context.Context) error
  43. type progressFuncWithStatus func(context.Context) (string, error)
  44. // Run will run a writer and the progress function in parallel
  45. func Run(ctx context.Context, pf progressFunc) error {
  46. _, err := RunWithStatus(ctx, func(ctx context.Context) (string, error) {
  47. return "", pf(ctx)
  48. })
  49. return err
  50. }
  51. // RunWithStatus will run a writer and the progress function in parallel and return a status
  52. func RunWithStatus(ctx context.Context, pf progressFuncWithStatus) (string, error) {
  53. eg, _ := errgroup.WithContext(ctx)
  54. w, err := NewWriter(os.Stderr)
  55. var result string
  56. if err != nil {
  57. return "", err
  58. }
  59. eg.Go(func() error {
  60. return w.Start(context.Background())
  61. })
  62. ctx = WithContextWriter(ctx, w)
  63. eg.Go(func() error {
  64. defer w.Stop()
  65. s, err := pf(ctx)
  66. if err == nil {
  67. result = s
  68. }
  69. return err
  70. })
  71. err = eg.Wait()
  72. return result, err
  73. }
  74. // NewWriter returns a new multi-progress writer
  75. func NewWriter(out console.File) (Writer, error) {
  76. _, isTerminal := term.GetFdInfo(out)
  77. if isTerminal {
  78. con, err := console.ConsoleFromFile(out)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return &ttyWriter{
  83. out: con,
  84. eventIDs: []string{},
  85. events: map[string]Event{},
  86. repeated: false,
  87. done: make(chan bool),
  88. mtx: &sync.Mutex{},
  89. }, nil
  90. }
  91. return &plainWriter{
  92. out: out,
  93. done: make(chan bool),
  94. }, nil
  95. }