jsonmessage.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. Stream string `json:"stream,omitempty"`
  83. Status string `json:"status,omitempty"`
  84. Progress *JSONProgress `json:"progressDetail,omitempty"`
  85. ProgressMessage string `json:"progress,omitempty"` //deprecated
  86. ID string `json:"id,omitempty"`
  87. From string `json:"from,omitempty"`
  88. Time int64 `json:"time,omitempty"`
  89. Error *JSONError `json:"errorDetail,omitempty"`
  90. ErrorMessage string `json:"error,omitempty"` //deprecated
  91. }
  92. // Display displays the JSONMessage to `out`. `isTerminal` describes if `out`
  93. // is a terminal. If this is the case, it will erase the entire current line
  94. // when dislaying the progressbar.
  95. func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
  96. if jm.Error != nil {
  97. if jm.Error.Code == 401 {
  98. return fmt.Errorf("Authentication is required.")
  99. }
  100. return jm.Error
  101. }
  102. var endl string
  103. if isTerminal && jm.Stream == "" && jm.Progress != nil {
  104. // <ESC>[2K = erase entire current line
  105. fmt.Fprintf(out, "%c[2K\r", 27)
  106. endl = "\r"
  107. } else if jm.Progress != nil && jm.Progress.String() != "" { //disable progressbar in non-terminal
  108. return nil
  109. }
  110. if jm.Time != 0 {
  111. fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(timeutils.RFC3339NanoFixed))
  112. }
  113. if jm.ID != "" {
  114. fmt.Fprintf(out, "%s: ", jm.ID)
  115. }
  116. if jm.From != "" {
  117. fmt.Fprintf(out, "(from %s) ", jm.From)
  118. }
  119. if jm.Progress != nil && isTerminal {
  120. fmt.Fprintf(out, "%s %s%s", jm.Status, jm.Progress.String(), endl)
  121. } else if jm.ProgressMessage != "" { //deprecated
  122. fmt.Fprintf(out, "%s %s%s", jm.Status, jm.ProgressMessage, endl)
  123. } else if jm.Stream != "" {
  124. fmt.Fprintf(out, "%s%s", jm.Stream, endl)
  125. } else {
  126. fmt.Fprintf(out, "%s%s\n", jm.Status, endl)
  127. }
  128. return nil
  129. }
  130. // DisplayJSONMessagesStream displays a json message stream from `in` to `out`, `isTerminal`
  131. // describes if `out` is a terminal. If this is the case, it will print `\n` at the end of
  132. // each line and move the cursor while displaying.
  133. func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool) error {
  134. var (
  135. dec = json.NewDecoder(in)
  136. ids = make(map[string]int)
  137. diff = 0
  138. )
  139. for {
  140. var jm JSONMessage
  141. if err := dec.Decode(&jm); err != nil {
  142. if err == io.EOF {
  143. break
  144. }
  145. return err
  146. }
  147. if jm.Progress != nil {
  148. jm.Progress.terminalFd = terminalFd
  149. }
  150. if jm.ID != "" && (jm.Progress != nil || jm.ProgressMessage != "") {
  151. line, ok := ids[jm.ID]
  152. if !ok {
  153. line = len(ids)
  154. ids[jm.ID] = line
  155. if isTerminal {
  156. fmt.Fprintf(out, "\n")
  157. }
  158. diff = 0
  159. } else {
  160. diff = len(ids) - line
  161. }
  162. if jm.ID != "" && isTerminal {
  163. // <ESC>[{diff}A = move cursor up diff rows
  164. fmt.Fprintf(out, "%c[%dA", 27, diff)
  165. }
  166. }
  167. err := jm.Display(out, isTerminal)
  168. if jm.ID != "" && isTerminal {
  169. // <ESC>[{diff}B = move cursor down diff rows
  170. fmt.Fprintf(out, "%c[%dB", 27, diff)
  171. }
  172. if err != nil {
  173. return err
  174. }
  175. }
  176. return nil
  177. }