progress.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 compose
  14. import (
  15. "context"
  16. "fmt"
  17. "github.com/docker/compose/v5/pkg/api"
  18. )
  19. type progressFunc func(context.Context) error
  20. func Run(ctx context.Context, pf progressFunc, operation string, bus api.EventProcessor) error {
  21. bus.Start(ctx, operation)
  22. err := pf(ctx)
  23. bus.Done(operation, err != nil)
  24. return err
  25. }
  26. // errorEvent creates a new Error Resource with message
  27. func errorEvent(id string, msg string) api.Resource {
  28. return api.Resource{
  29. ID: id,
  30. Status: api.Error,
  31. Text: api.StatusError,
  32. Details: msg,
  33. }
  34. }
  35. // errorEventf creates a new Error Resource with format message
  36. func errorEventf(id string, msg string, args ...any) api.Resource {
  37. return errorEvent(id, fmt.Sprintf(msg, args...))
  38. }
  39. // creatingEvent creates a new Create in progress Resource
  40. func creatingEvent(id string) api.Resource {
  41. return newEvent(id, api.Working, api.StatusCreating)
  42. }
  43. // startingEvent creates a new Starting in progress Resource
  44. func startingEvent(id string) api.Resource {
  45. return newEvent(id, api.Working, api.StatusStarting)
  46. }
  47. // startedEvent creates a new Started in progress Resource
  48. func startedEvent(id string) api.Resource {
  49. return newEvent(id, api.Done, api.StatusStarted)
  50. }
  51. // waiting creates a new waiting event
  52. func waiting(id string) api.Resource {
  53. return newEvent(id, api.Working, api.StatusWaiting)
  54. }
  55. // healthy creates a new healthy event
  56. func healthy(id string) api.Resource {
  57. return newEvent(id, api.Done, api.StatusHealthy)
  58. }
  59. // exited creates a new exited event
  60. func exited(id string) api.Resource {
  61. return newEvent(id, api.Done, api.StatusExited)
  62. }
  63. // restartingEvent creates a new Restarting in progress Resource
  64. func restartingEvent(id string) api.Resource {
  65. return newEvent(id, api.Working, api.StatusRestarting)
  66. }
  67. // runningEvent creates a new Running in progress Resource
  68. func runningEvent(id string) api.Resource {
  69. return newEvent(id, api.Done, api.StatusRunning)
  70. }
  71. // createdEvent creates a new Created (done) Resource
  72. func createdEvent(id string) api.Resource {
  73. return newEvent(id, api.Done, api.StatusCreated)
  74. }
  75. // stoppingEvent creates a new Stopping in progress Resource
  76. func stoppingEvent(id string) api.Resource {
  77. return newEvent(id, api.Working, api.StatusStopping)
  78. }
  79. // stoppedEvent creates a new Stopping in progress Resource
  80. func stoppedEvent(id string) api.Resource {
  81. return newEvent(id, api.Done, api.StatusStopped)
  82. }
  83. // killingEvent creates a new Killing in progress Resource
  84. func killingEvent(id string) api.Resource {
  85. return newEvent(id, api.Working, api.StatusKilling)
  86. }
  87. // killedEvent creates a new Killed in progress Resource
  88. func killedEvent(id string) api.Resource {
  89. return newEvent(id, api.Done, api.StatusKilled)
  90. }
  91. // removingEvent creates a new Removing in progress Resource
  92. func removingEvent(id string) api.Resource {
  93. return newEvent(id, api.Working, api.StatusRemoving)
  94. }
  95. // removedEvent creates a new removed (done) Resource
  96. func removedEvent(id string) api.Resource {
  97. return newEvent(id, api.Done, api.StatusRemoved)
  98. }
  99. // buildingEvent creates a new Building in progress Resource
  100. func buildingEvent(id string) api.Resource {
  101. return newEvent("Image "+id, api.Working, api.StatusBuilding)
  102. }
  103. // builtEvent creates a new built (done) Resource
  104. func builtEvent(id string) api.Resource {
  105. return newEvent("Image "+id, api.Done, api.StatusBuilt)
  106. }
  107. // pullingEvent creates a new pulling (in progress) Resource
  108. func pullingEvent(id string) api.Resource {
  109. return newEvent("Image "+id, api.Working, api.StatusPulling)
  110. }
  111. // pulledEvent creates a new pulled (done) Resource
  112. func pulledEvent(id string) api.Resource {
  113. return newEvent("Image "+id, api.Done, api.StatusPulled)
  114. }
  115. // skippedEvent creates a new Skipped Resource
  116. func skippedEvent(id string, reason string) api.Resource {
  117. return api.Resource{
  118. ID: id,
  119. Status: api.Warning,
  120. Text: "Skipped: " + reason,
  121. }
  122. }
  123. // newEvent new event
  124. func newEvent(id string, status api.EventStatus, text string, reason ...string) api.Resource {
  125. r := api.Resource{
  126. ID: id,
  127. Status: status,
  128. Text: text,
  129. }
  130. if len(reason) > 0 {
  131. r.Details = reason[0]
  132. }
  133. return r
  134. }
  135. type ignore struct{}
  136. func (q *ignore) Start(_ context.Context, _ string) {
  137. }
  138. func (q *ignore) Done(_ string, _ bool) {
  139. }
  140. func (q *ignore) On(_ ...api.Resource) {
  141. }