event.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "time"
  15. // EventStatus indicates the status of an action
  16. type EventStatus int
  17. const (
  18. // Working means that the current task is working
  19. Working EventStatus = iota
  20. // Done means that the current task is done
  21. Done
  22. // Error means that the current task has errored
  23. Error
  24. // Warning means that the current task has warning
  25. Warning
  26. )
  27. // Event represents a progress event.
  28. type Event struct {
  29. ID string
  30. ParentID string
  31. Text string
  32. Status EventStatus
  33. StatusText string
  34. startTime time.Time
  35. endTime time.Time
  36. spinner *spinner
  37. }
  38. // ErrorMessageEvent creates a new Error Event with message
  39. func ErrorMessageEvent(id string, msg string) Event {
  40. return NewEvent(id, Error, msg)
  41. }
  42. // ErrorEvent creates a new Error Event
  43. func ErrorEvent(id string) Event {
  44. return NewEvent(id, Error, "Error")
  45. }
  46. // CreatingEvent creates a new Create in progress Event
  47. func CreatingEvent(id string) Event {
  48. return NewEvent(id, Working, "Creating")
  49. }
  50. // StartingEvent creates a new Starting in progress Event
  51. func StartingEvent(id string) Event {
  52. return NewEvent(id, Working, "Starting")
  53. }
  54. // StartedEvent creates a new Started in progress Event
  55. func StartedEvent(id string) Event {
  56. return NewEvent(id, Done, "Started")
  57. }
  58. // Waiting creates a new waiting event
  59. func Waiting(id string) Event {
  60. return NewEvent(id, Working, "Waiting")
  61. }
  62. // Healthy creates a new healthy event
  63. func Healthy(id string) Event {
  64. return NewEvent(id, Done, "Healthy")
  65. }
  66. // Exited creates a new exited event
  67. func Exited(id string) Event {
  68. return NewEvent(id, Done, "Exited")
  69. }
  70. // RestartingEvent creates a new Restarting in progress Event
  71. func RestartingEvent(id string) Event {
  72. return NewEvent(id, Working, "Restarting")
  73. }
  74. // RestartedEvent creates a new Restarted in progress Event
  75. func RestartedEvent(id string) Event {
  76. return NewEvent(id, Done, "Restarted")
  77. }
  78. // RunningEvent creates a new Running in progress Event
  79. func RunningEvent(id string) Event {
  80. return NewEvent(id, Done, "Running")
  81. }
  82. // CreatedEvent creates a new Created (done) Event
  83. func CreatedEvent(id string) Event {
  84. return NewEvent(id, Done, "Created")
  85. }
  86. // StoppingEvent creates a new Stopping in progress Event
  87. func StoppingEvent(id string) Event {
  88. return NewEvent(id, Working, "Stopping")
  89. }
  90. // StoppedEvent creates a new Stopping in progress Event
  91. func StoppedEvent(id string) Event {
  92. return NewEvent(id, Done, "Stopped")
  93. }
  94. // KillingEvent creates a new Killing in progress Event
  95. func KillingEvent(id string) Event {
  96. return NewEvent(id, Working, "Killing")
  97. }
  98. // KilledEvent creates a new Killed in progress Event
  99. func KilledEvent(id string) Event {
  100. return NewEvent(id, Done, "Killed")
  101. }
  102. // RemovingEvent creates a new Removing in progress Event
  103. func RemovingEvent(id string) Event {
  104. return NewEvent(id, Working, "Removing")
  105. }
  106. // RemovedEvent creates a new removed (done) Event
  107. func RemovedEvent(id string) Event {
  108. return NewEvent(id, Done, "Removed")
  109. }
  110. // NewEvent new event
  111. func NewEvent(id string, status EventStatus, statusText string) Event {
  112. return Event{
  113. ID: id,
  114. Status: status,
  115. StatusText: statusText,
  116. }
  117. }
  118. func (e *Event) stop() {
  119. e.endTime = time.Now()
  120. e.spinner.Stop()
  121. }