writer.go 2.8 KB

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