event.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. "time"
  16. )
  17. // EventStatus indicates the status of an action
  18. type EventStatus int
  19. func (s EventStatus) colorFn() colorFunc {
  20. switch s {
  21. case Done:
  22. return SuccessColor
  23. case Warning:
  24. return WarningColor
  25. case Error:
  26. return ErrorColor
  27. default:
  28. return nocolor
  29. }
  30. }
  31. const (
  32. // Working means that the current task is working
  33. Working EventStatus = iota
  34. // Done means that the current task is done
  35. Done
  36. // Warning means that the current task has warning
  37. Warning
  38. // Error means that the current task has errored
  39. Error
  40. )
  41. // Event represents a progress event.
  42. type Event struct {
  43. ID string
  44. ParentID string
  45. Text string
  46. Status EventStatus
  47. StatusText string
  48. Current int64
  49. Percent int
  50. Total int64
  51. startTime time.Time
  52. endTime time.Time
  53. spinner *spinner
  54. }
  55. // ErrorMessageEvent creates a new Error Event with message
  56. func ErrorMessageEvent(id string, msg string) Event {
  57. return NewEvent(id, Error, msg)
  58. }
  59. // ErrorEvent creates a new Error Event
  60. func ErrorEvent(id string) Event {
  61. return NewEvent(id, Error, "Error")
  62. }
  63. // CreatingEvent creates a new Create in progress Event
  64. func CreatingEvent(id string) Event {
  65. return NewEvent(id, Working, "Creating")
  66. }
  67. // StartingEvent creates a new Starting in progress Event
  68. func StartingEvent(id string) Event {
  69. return NewEvent(id, Working, "Starting")
  70. }
  71. // StartedEvent creates a new Started in progress Event
  72. func StartedEvent(id string) Event {
  73. return NewEvent(id, Done, "Started")
  74. }
  75. // Waiting creates a new waiting event
  76. func Waiting(id string) Event {
  77. return NewEvent(id, Working, "Waiting")
  78. }
  79. // Healthy creates a new healthy event
  80. func Healthy(id string) Event {
  81. return NewEvent(id, Done, "Healthy")
  82. }
  83. // Exited creates a new exited event
  84. func Exited(id string) Event {
  85. return NewEvent(id, Done, "Exited")
  86. }
  87. // RestartingEvent creates a new Restarting in progress Event
  88. func RestartingEvent(id string) Event {
  89. return NewEvent(id, Working, "Restarting")
  90. }
  91. // RestartedEvent creates a new Restarted in progress Event
  92. func RestartedEvent(id string) Event {
  93. return NewEvent(id, Done, "Restarted")
  94. }
  95. // RunningEvent creates a new Running in progress Event
  96. func RunningEvent(id string) Event {
  97. return NewEvent(id, Done, "Running")
  98. }
  99. // CreatedEvent creates a new Created (done) Event
  100. func CreatedEvent(id string) Event {
  101. return NewEvent(id, Done, "Created")
  102. }
  103. // StoppingEvent creates a new Stopping in progress Event
  104. func StoppingEvent(id string) Event {
  105. return NewEvent(id, Working, "Stopping")
  106. }
  107. // StoppedEvent creates a new Stopping in progress Event
  108. func StoppedEvent(id string) Event {
  109. return NewEvent(id, Done, "Stopped")
  110. }
  111. // KillingEvent creates a new Killing in progress Event
  112. func KillingEvent(id string) Event {
  113. return NewEvent(id, Working, "Killing")
  114. }
  115. // KilledEvent creates a new Killed in progress Event
  116. func KilledEvent(id string) Event {
  117. return NewEvent(id, Done, "Killed")
  118. }
  119. // RemovingEvent creates a new Removing in progress Event
  120. func RemovingEvent(id string) Event {
  121. return NewEvent(id, Working, "Removing")
  122. }
  123. // RemovedEvent creates a new removed (done) Event
  124. func RemovedEvent(id string) Event {
  125. return NewEvent(id, Done, "Removed")
  126. }
  127. // SkippedEvent creates a new Skipped Event
  128. func SkippedEvent(id string, reason string) Event {
  129. return Event{
  130. ID: id,
  131. Status: Warning,
  132. StatusText: "Skipped: " + reason,
  133. }
  134. }
  135. // NewEvent new event
  136. func NewEvent(id string, status EventStatus, statusText string) Event {
  137. return Event{
  138. ID: id,
  139. Status: status,
  140. StatusText: statusText,
  141. }
  142. }
  143. func (e *Event) stop() {
  144. e.endTime = time.Now()
  145. e.spinner.Stop()
  146. }
  147. var (
  148. spinnerDone = "✔"
  149. spinnerWarning = "!"
  150. spinnerError = "✘"
  151. )
  152. func (e *Event) Spinner() any {
  153. switch e.Status {
  154. case Done:
  155. return SuccessColor(spinnerDone)
  156. case Warning:
  157. return WarningColor(spinnerWarning)
  158. case Error:
  159. return ErrorColor(spinnerError)
  160. default:
  161. return e.spinner.String()
  162. }
  163. }