jsonmessage.go 5.2 KB

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