jsonmessage.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/pkg/term"
  9. "github.com/docker/docker/pkg/units"
  10. )
  11. type JSONError struct {
  12. Code int `json:"code,omitempty"`
  13. Message string `json:"message,omitempty"`
  14. }
  15. func (e *JSONError) Error() string {
  16. return e.Message
  17. }
  18. type JSONProgress struct {
  19. terminalFd uintptr
  20. Current int `json:"current,omitempty"`
  21. Total int `json:"total,omitempty"`
  22. Start int64 `json:"start,omitempty"`
  23. }
  24. func (p *JSONProgress) String() string {
  25. var (
  26. width = 200
  27. pbBox string
  28. numbersBox string
  29. timeLeftBox string
  30. )
  31. ws, err := term.GetWinsize(p.terminalFd)
  32. if err == nil {
  33. width = int(ws.Width)
  34. }
  35. if p.Current <= 0 && p.Total <= 0 {
  36. return ""
  37. }
  38. current := units.HumanSize(int64(p.Current))
  39. if p.Total <= 0 {
  40. return fmt.Sprintf("%8v", current)
  41. }
  42. total := units.HumanSize(int64(p.Total))
  43. percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
  44. if width > 110 {
  45. // this number can't be negetive gh#7136
  46. numSpaces := 0
  47. if 50-percentage > 0 {
  48. numSpaces = 50 - percentage
  49. }
  50. pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", numSpaces))
  51. }
  52. numbersBox = fmt.Sprintf("%8v/%v", current, total)
  53. if p.Current > 0 && p.Start > 0 && percentage < 50 {
  54. fromStart := time.Now().UTC().Sub(time.Unix(int64(p.Start), 0))
  55. perEntry := fromStart / time.Duration(p.Current)
  56. left := time.Duration(p.Total-p.Current) * perEntry
  57. left = (left / time.Second) * time.Second
  58. if width > 50 {
  59. timeLeftBox = " " + left.String()
  60. }
  61. }
  62. return pbBox + numbersBox + timeLeftBox
  63. }
  64. type JSONMessage struct {
  65. Stream string `json:"stream,omitempty"`
  66. Status string `json:"status,omitempty"`
  67. Progress *JSONProgress `json:"progressDetail,omitempty"`
  68. ProgressMessage string `json:"progress,omitempty"` //deprecated
  69. ID string `json:"id,omitempty"`
  70. From string `json:"from,omitempty"`
  71. Time int64 `json:"time,omitempty"`
  72. Error *JSONError `json:"errorDetail,omitempty"`
  73. ErrorMessage string `json:"error,omitempty"` //deprecated
  74. }
  75. func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
  76. if jm.Error != nil {
  77. if jm.Error.Code == 401 {
  78. return fmt.Errorf("Authentication is required.")
  79. }
  80. return jm.Error
  81. }
  82. var endl string
  83. if isTerminal && jm.Stream == "" && jm.Progress != nil {
  84. // <ESC>[2K = erase entire current line
  85. fmt.Fprintf(out, "%c[2K\r", 27)
  86. endl = "\r"
  87. } else if jm.Progress != nil { //disable progressbar in non-terminal
  88. return nil
  89. }
  90. if jm.Time != 0 {
  91. fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(time.RFC3339Nano))
  92. }
  93. if jm.ID != "" {
  94. fmt.Fprintf(out, "%s: ", jm.ID)
  95. }
  96. if jm.From != "" {
  97. fmt.Fprintf(out, "(from %s) ", jm.From)
  98. }
  99. if jm.Progress != nil {
  100. fmt.Fprintf(out, "%s %s%s", jm.Status, jm.Progress.String(), endl)
  101. } else if jm.ProgressMessage != "" { //deprecated
  102. fmt.Fprintf(out, "%s %s%s", jm.Status, jm.ProgressMessage, endl)
  103. } else if jm.Stream != "" {
  104. fmt.Fprintf(out, "%s%s", jm.Stream, endl)
  105. } else {
  106. fmt.Fprintf(out, "%s%s\n", jm.Status, endl)
  107. }
  108. return nil
  109. }
  110. func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool) error {
  111. var (
  112. dec = json.NewDecoder(in)
  113. ids = make(map[string]int)
  114. diff = 0
  115. )
  116. for {
  117. var jm JSONMessage
  118. if err := dec.Decode(&jm); err != nil {
  119. if err == io.EOF {
  120. break
  121. }
  122. return err
  123. }
  124. if jm.Progress != nil {
  125. jm.Progress.terminalFd = terminalFd
  126. }
  127. if jm.ID != "" && (jm.Progress != nil || jm.ProgressMessage != "") {
  128. line, ok := ids[jm.ID]
  129. if !ok {
  130. line = len(ids)
  131. ids[jm.ID] = line
  132. fmt.Fprintf(out, "\n")
  133. diff = 0
  134. } else {
  135. diff = len(ids) - line
  136. }
  137. if jm.ID != "" && isTerminal {
  138. // <ESC>[{diff}A = move cursor up diff rows
  139. fmt.Fprintf(out, "%c[%dA", 27, diff)
  140. }
  141. }
  142. err := jm.Display(out, isTerminal)
  143. if jm.ID != "" && isTerminal {
  144. // <ESC>[{diff}B = move cursor down diff rows
  145. fmt.Fprintf(out, "%c[%dB", 27, diff)
  146. }
  147. if err != nil {
  148. return err
  149. }
  150. }
  151. return nil
  152. }