jsonmessage.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package jsonmessage
  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/timeutils"
  10. "github.com/docker/docker/pkg/units"
  11. )
  12. // JSONError wraps a concrete Code and Message, `Code` is
  13. // is a integer error code, `Message` is the error message.
  14. type JSONError struct {
  15. Code int `json:"code,omitempty"`
  16. Message string `json:"message,omitempty"`
  17. }
  18. func (e *JSONError) Error() string {
  19. return e.Message
  20. }
  21. // JSONProgress describes a Progress. terminalFd is the fd of the current terminal,
  22. // Start is the initial value for the operation. Current is the current status and
  23. // value of the progress made towards Total. Total is the end value describing when
  24. // we made 100% progress for an operation.
  25. type JSONProgress struct {
  26. terminalFd uintptr
  27. Current int64 `json:"current,omitempty"`
  28. Total int64 `json:"total,omitempty"`
  29. Start int64 `json:"start,omitempty"`
  30. }
  31. func (p *JSONProgress) String() string {
  32. var (
  33. width = 200
  34. pbBox string
  35. numbersBox string
  36. timeLeftBox string
  37. )
  38. ws, err := term.GetWinsize(p.terminalFd)
  39. if err == nil {
  40. width = int(ws.Width)
  41. }
  42. if p.Current <= 0 && p.Total <= 0 {
  43. return ""
  44. }
  45. current := units.HumanSize(float64(p.Current))
  46. if p.Total <= 0 {
  47. return fmt.Sprintf("%8v", current)
  48. }
  49. total := units.HumanSize(float64(p.Total))
  50. percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
  51. if percentage > 50 {
  52. percentage = 50
  53. }
  54. if width > 110 {
  55. // this number can't be negetive gh#7136
  56. numSpaces := 0
  57. if 50-percentage > 0 {
  58. numSpaces = 50 - percentage
  59. }
  60. pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", numSpaces))
  61. }
  62. numbersBox = fmt.Sprintf("%8v/%v", current, total)
  63. if p.Current > p.Total {
  64. // remove total display if the reported current is wonky.
  65. numbersBox = fmt.Sprintf("%8v", current)
  66. }
  67. if p.Current > 0 && p.Start > 0 && percentage < 50 {
  68. fromStart := time.Now().UTC().Sub(time.Unix(p.Start, 0))
  69. perEntry := fromStart / time.Duration(p.Current)
  70. left := time.Duration(p.Total-p.Current) * perEntry
  71. left = (left / time.Second) * time.Second
  72. if width > 50 {
  73. timeLeftBox = " " + left.String()
  74. }
  75. }
  76. return pbBox + numbersBox + timeLeftBox
  77. }
  78. // JSONMessage defines a message struct. It describes
  79. // the created time, where it from, status, ID of the
  80. // message. It's used for docker events.
  81. type JSONMessage struct {
  82. RequestID string `json:"reqid,omitempty"`
  83. Stream string `json:"stream,omitempty"`
  84. Status string `json:"status,omitempty"`
  85. Progress *JSONProgress `json:"progressDetail,omitempty"`
  86. ProgressMessage string `json:"progress,omitempty"` //deprecated
  87. ID string `json:"id,omitempty"`
  88. From string `json:"from,omitempty"`
  89. Time int64 `json:"time,omitempty"`
  90. TimeNano int64 `json:"timeNano,omitempty"`
  91. Error *JSONError `json:"errorDetail,omitempty"`
  92. ErrorMessage string `json:"error,omitempty"` //deprecated
  93. }
  94. // Display displays the JSONMessage to `out`. `isTerminal` describes if `out`
  95. // is a terminal. If this is the case, it will erase the entire current line
  96. // when dislaying the progressbar.
  97. func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
  98. if jm.Error != nil {
  99. if jm.Error.Code == 401 {
  100. return fmt.Errorf("Authentication is required.")
  101. }
  102. return jm.Error
  103. }
  104. var endl string
  105. if isTerminal && jm.Stream == "" && jm.Progress != nil {
  106. // <ESC>[2K = erase entire current line
  107. fmt.Fprintf(out, "%c[2K\r", 27)
  108. endl = "\r"
  109. } else if jm.Progress != nil && jm.Progress.String() != "" { //disable progressbar in non-terminal
  110. return nil
  111. }
  112. if jm.TimeNano != 0 {
  113. fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(timeutils.RFC3339NanoFixed))
  114. } else if jm.Time != 0 {
  115. fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(timeutils.RFC3339NanoFixed))
  116. }
  117. if jm.RequestID != "" {
  118. fmt.Fprintf(out, "[reqid: %s] ", jm.RequestID)
  119. }
  120. if jm.ID != "" {
  121. fmt.Fprintf(out, "%s: ", jm.ID)
  122. }
  123. if jm.From != "" {
  124. fmt.Fprintf(out, "(from %s) ", jm.From)
  125. }
  126. if jm.Progress != nil && isTerminal {
  127. fmt.Fprintf(out, "%s %s%s", jm.Status, jm.Progress.String(), endl)
  128. } else if jm.ProgressMessage != "" { //deprecated
  129. fmt.Fprintf(out, "%s %s%s", jm.Status, jm.ProgressMessage, endl)
  130. } else if jm.Stream != "" {
  131. fmt.Fprintf(out, "%s%s", jm.Stream, endl)
  132. } else {
  133. fmt.Fprintf(out, "%s%s\n", jm.Status, endl)
  134. }
  135. return nil
  136. }
  137. // DisplayJSONMessagesStream displays a json message stream from `in` to `out`, `isTerminal`
  138. // describes if `out` is a terminal. If this is the case, it will print `\n` at the end of
  139. // each line and move the cursor while displaying.
  140. func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool) error {
  141. var (
  142. dec = json.NewDecoder(in)
  143. ids = make(map[string]int)
  144. diff = 0
  145. )
  146. for {
  147. var jm JSONMessage
  148. if err := dec.Decode(&jm); err != nil {
  149. if err == io.EOF {
  150. break
  151. }
  152. return err
  153. }
  154. if jm.Progress != nil {
  155. jm.Progress.terminalFd = terminalFd
  156. }
  157. if jm.ID != "" && (jm.Progress != nil || jm.ProgressMessage != "") {
  158. line, ok := ids[jm.ID]
  159. if !ok {
  160. line = len(ids)
  161. ids[jm.ID] = line
  162. if isTerminal {
  163. fmt.Fprintf(out, "\n")
  164. }
  165. diff = 0
  166. } else {
  167. diff = len(ids) - line
  168. }
  169. if jm.ID != "" && isTerminal {
  170. // <ESC>[{diff}A = move cursor up diff rows
  171. fmt.Fprintf(out, "%c[%dA", 27, diff)
  172. }
  173. }
  174. err := jm.Display(out, isTerminal)
  175. if jm.ID != "" && isTerminal {
  176. // <ESC>[{diff}B = move cursor down diff rows
  177. fmt.Fprintf(out, "%c[%dB", 27, diff)
  178. }
  179. if err != nil {
  180. return err
  181. }
  182. }
  183. return nil
  184. }