event.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. Text string
  29. Status EventStatus
  30. StatusText string
  31. startTime time.Time
  32. endTime time.Time
  33. spinner *spinner
  34. }
  35. // ErrorMessageEvent creates a new Error Event with message
  36. func ErrorMessageEvent(ID string, msg string) Event {
  37. return NewEvent(ID, Error, msg)
  38. }
  39. // ErrorEvent creates a new Error Event
  40. func ErrorEvent(ID string) Event {
  41. return NewEvent(ID, Error, "Error")
  42. }
  43. // CreatingEvent creates a new Create in progress Event
  44. func CreatingEvent(ID string) Event {
  45. return NewEvent(ID, Working, "Creating")
  46. }
  47. // CreatedEvent creates a new Created (done) Event
  48. func CreatedEvent(ID string) Event {
  49. return NewEvent(ID, Done, "Created")
  50. }
  51. // RemovingEvent creates a new Removing in progress Event
  52. func RemovingEvent(ID string) Event {
  53. return NewEvent(ID, Working, "Removing")
  54. }
  55. // RemovedEvent creates a new removed (done) Event
  56. func RemovedEvent(ID string) Event {
  57. return NewEvent(ID, Done, "Removed")
  58. }
  59. // NewEvent new event
  60. func NewEvent(ID string, status EventStatus, statusText string) Event {
  61. return Event{
  62. ID: ID,
  63. Status: status,
  64. StatusText: statusText,
  65. }
  66. }
  67. func (e *Event) stop() {
  68. e.endTime = time.Now()
  69. e.spinner.Stop()
  70. }