progress.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package progress // import "github.com/docker/docker/pkg/progress"
  2. import (
  3. "fmt"
  4. )
  5. // Progress represents the progress of a transfer.
  6. type Progress struct {
  7. ID string
  8. // Progress contains a Message or...
  9. Message string
  10. // ...progress of an action
  11. Action string
  12. Current int64
  13. Total int64
  14. // If true, don't show xB/yB
  15. HideCounts bool
  16. // If not empty, use units instead of bytes for counts
  17. Units string
  18. // Aux contains extra information not presented to the user, such as
  19. // digests for push signing.
  20. Aux interface{}
  21. LastUpdate bool
  22. }
  23. // Output is an interface for writing progress information. It's
  24. // like a writer for progress, but we don't call it Writer because
  25. // that would be confusing next to ProgressReader (also, because it
  26. // doesn't implement the io.Writer interface).
  27. type Output interface {
  28. WriteProgress(Progress) error
  29. }
  30. type chanOutput chan<- Progress
  31. func (out chanOutput) WriteProgress(p Progress) error {
  32. // FIXME: workaround for panic in #37735
  33. defer func() {
  34. recover()
  35. }()
  36. out <- p
  37. return nil
  38. }
  39. // ChanOutput returns an Output that writes progress updates to the
  40. // supplied channel.
  41. func ChanOutput(progressChan chan<- Progress) Output {
  42. return chanOutput(progressChan)
  43. }
  44. type discardOutput struct{}
  45. func (discardOutput) WriteProgress(Progress) error {
  46. return nil
  47. }
  48. // DiscardOutput returns an Output that discards progress
  49. func DiscardOutput() Output {
  50. return discardOutput{}
  51. }
  52. // Update is a convenience function to write a progress update to the channel.
  53. func Update(out Output, id, action string) {
  54. out.WriteProgress(Progress{ID: id, Action: action})
  55. }
  56. // Updatef is a convenience function to write a printf-formatted progress update
  57. // to the channel.
  58. func Updatef(out Output, id, format string, a ...interface{}) {
  59. Update(out, id, fmt.Sprintf(format, a...))
  60. }
  61. // Message is a convenience function to write a progress message to the channel.
  62. func Message(out Output, id, message string) {
  63. out.WriteProgress(Progress{ID: id, Message: message})
  64. }
  65. // Messagef is a convenience function to write a printf-formatted progress
  66. // message to the channel.
  67. func Messagef(out Output, id, format string, a ...interface{}) {
  68. Message(out, id, fmt.Sprintf(format, a...))
  69. }
  70. // Aux sends auxiliary information over a progress interface, which will not be
  71. // formatted for the UI. This is used for things such as push signing.
  72. func Aux(out Output, a interface{}) {
  73. out.WriteProgress(Progress{Aux: a})
  74. }