event.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. )
  25. // Event represents a progress event.
  26. type Event struct {
  27. ID string
  28. ParentID string
  29. Text string
  30. Status EventStatus
  31. StatusText string
  32. startTime time.Time
  33. endTime time.Time
  34. spinner *spinner
  35. }
  36. // ErrorMessageEvent creates a new Error Event with message
  37. func ErrorMessageEvent(ID string, msg string) Event {
  38. return NewEvent(ID, Error, msg)
  39. }
  40. // ErrorEvent creates a new Error Event
  41. func ErrorEvent(ID string) Event {
  42. return NewEvent(ID, Error, "Error")
  43. }
  44. // CreatingEvent creates a new Create in progress Event
  45. func CreatingEvent(ID string) Event {
  46. return NewEvent(ID, Working, "Creating")
  47. }
  48. // StartingEvent creates a new Starting in progress Event
  49. func StartingEvent(ID string) Event {
  50. return NewEvent(ID, Working, "Starting")
  51. }
  52. // StartedEvent creates a new Started in progress Event
  53. func StartedEvent(ID string) Event {
  54. return NewEvent(ID, Done, "Started")
  55. }
  56. // RestartingEvent creates a new Restarting in progress Event
  57. func RestartingEvent(ID string) Event {
  58. return NewEvent(ID, Working, "Restarting")
  59. }
  60. // RestartedEvent creates a new Restarted in progress Event
  61. func RestartedEvent(ID string) Event {
  62. return NewEvent(ID, Done, "Restarted")
  63. }
  64. // RunningEvent creates a new Running in progress Event
  65. func RunningEvent(ID string) Event {
  66. return NewEvent(ID, Done, "Running")
  67. }
  68. // CreatedEvent creates a new Created (done) Event
  69. func CreatedEvent(ID string) Event {
  70. return NewEvent(ID, Done, "Created")
  71. }
  72. // StoppingEvent creates a new Stopping in progress Event
  73. func StoppingEvent(ID string) Event {
  74. return NewEvent(ID, Working, "Stopping")
  75. }
  76. // StoppedEvent creates a new Stopping in progress Event
  77. func StoppedEvent(ID string) Event {
  78. return NewEvent(ID, Done, "Stopped")
  79. }
  80. // KillingEvent creates a new Killing in progress Event
  81. func KillingEvent(ID string) Event {
  82. return NewEvent(ID, Working, "Killing")
  83. }
  84. // KilledEvent creates a new Killed in progress Event
  85. func KilledEvent(ID string) Event {
  86. return NewEvent(ID, Done, "Killed")
  87. }
  88. // RemovingEvent creates a new Removing in progress Event
  89. func RemovingEvent(ID string) Event {
  90. return NewEvent(ID, Working, "Removing")
  91. }
  92. // RemovedEvent creates a new removed (done) Event
  93. func RemovedEvent(ID string) Event {
  94. return NewEvent(ID, Done, "Removed")
  95. }
  96. // NewEvent new event
  97. func NewEvent(ID string, status EventStatus, statusText string) Event {
  98. return Event{
  99. ID: ID,
  100. Status: status,
  101. StatusText: statusText,
  102. }
  103. }
  104. func (e *Event) stop() {
  105. e.endTime = time.Now()
  106. e.spinner.Stop()
  107. }