event.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. // RunningEvent creates a new Running in progress Event
  57. func RunningEvent(ID string) Event {
  58. return NewEvent(ID, Done, "Running")
  59. }
  60. // CreatedEvent creates a new Created (done) Event
  61. func CreatedEvent(ID string) Event {
  62. return NewEvent(ID, Done, "Created")
  63. }
  64. // StoppingEvent stops a new Removing in progress Event
  65. func StoppingEvent(ID string) Event {
  66. return NewEvent(ID, Working, "Stopping")
  67. }
  68. // RemovingEvent creates a new Removing in progress Event
  69. func RemovingEvent(ID string) Event {
  70. return NewEvent(ID, Working, "Removing")
  71. }
  72. // RemovedEvent creates a new removed (done) Event
  73. func RemovedEvent(ID string) Event {
  74. return NewEvent(ID, Done, "Removed")
  75. }
  76. // NewEvent new event
  77. func NewEvent(ID string, status EventStatus, statusText string) Event {
  78. return Event{
  79. ID: ID,
  80. Status: status,
  81. StatusText: statusText,
  82. }
  83. }
  84. func (e *Event) stop() {
  85. e.endTime = time.Now()
  86. e.spinner.Stop()
  87. }