writer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "time"
  19. "github.com/containerd/console"
  20. "github.com/moby/term"
  21. "golang.org/x/sync/errgroup"
  22. )
  23. // EventStatus indicates the status of an action
  24. type EventStatus int
  25. const (
  26. // Working means that the current task is working
  27. Working EventStatus = iota
  28. // Done means that the current task is done
  29. Done
  30. // Error means that the current task has errored
  31. Error
  32. )
  33. // Event reprensents a progress event
  34. type Event struct {
  35. ID string
  36. Text string
  37. Status EventStatus
  38. StatusText string
  39. Done bool
  40. startTime time.Time
  41. endTime time.Time
  42. spinner *spinner
  43. }
  44. func (e *Event) stop() {
  45. e.endTime = time.Now()
  46. e.spinner.Stop()
  47. }
  48. // Writer can write multiple progress events
  49. type Writer interface {
  50. Start(context.Context) error
  51. Stop()
  52. Event(Event)
  53. }
  54. type writerKey struct{}
  55. // WithContextWriter adds the writer to the context
  56. func WithContextWriter(ctx context.Context, writer Writer) context.Context {
  57. return context.WithValue(ctx, writerKey{}, writer)
  58. }
  59. // ContextWriter returns the writer from the context
  60. func ContextWriter(ctx context.Context) Writer {
  61. s, ok := ctx.Value(writerKey{}).(Writer)
  62. if !ok {
  63. return &noopWriter{}
  64. }
  65. return s
  66. }
  67. type progressFunc func(context.Context) (string, error)
  68. // Run will run a writer and the progress function
  69. // in parallel
  70. func Run(ctx context.Context, pf progressFunc) (string, error) {
  71. eg, _ := errgroup.WithContext(ctx)
  72. w, err := NewWriter(os.Stderr)
  73. var result string
  74. if err != nil {
  75. return "", err
  76. }
  77. eg.Go(func() error {
  78. return w.Start(context.Background())
  79. })
  80. ctx = WithContextWriter(ctx, w)
  81. eg.Go(func() error {
  82. defer w.Stop()
  83. s, err := pf(ctx)
  84. if err == nil {
  85. result = s
  86. }
  87. return err
  88. })
  89. err = eg.Wait()
  90. return result, err
  91. }
  92. // NewWriter returns a new multi-progress writer
  93. func NewWriter(out console.File) (Writer, error) {
  94. _, isTerminal := term.GetFdInfo(out)
  95. if isTerminal {
  96. con, err := console.ConsoleFromFile(out)
  97. if err != nil {
  98. return nil, err
  99. }
  100. return &ttyWriter{
  101. out: con,
  102. eventIDs: []string{},
  103. events: map[string]Event{},
  104. repeated: false,
  105. done: make(chan bool),
  106. mtx: &sync.RWMutex{},
  107. }, nil
  108. }
  109. return &plainWriter{
  110. out: out,
  111. done: make(chan bool),
  112. }, nil
  113. }