jsonmessage.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "strings"
  7. "time"
  8. "github.com/dotcloud/docker/pkg/term"
  9. "github.com/dotcloud/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. pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", 50-percentage))
  46. }
  47. numbersBox = fmt.Sprintf("%8v/%v", current, total)
  48. if p.Current > 0 && p.Start > 0 && percentage < 50 {
  49. fromStart := time.Now().UTC().Sub(time.Unix(int64(p.Start), 0))
  50. perEntry := fromStart / time.Duration(p.Current)
  51. left := time.Duration(p.Total-p.Current) * perEntry
  52. left = (left / time.Second) * time.Second
  53. if width > 50 {
  54. timeLeftBox = " " + left.String()
  55. }
  56. }
  57. return pbBox + numbersBox + timeLeftBox
  58. }
  59. type JSONMessage struct {
  60. Stream string `json:"stream,omitempty"`
  61. Status string `json:"status,omitempty"`
  62. Progress *JSONProgress `json:"progressDetail,omitempty"`
  63. ProgressMessage string `json:"progress,omitempty"` //deprecated
  64. ID string `json:"id,omitempty"`
  65. From string `json:"from,omitempty"`
  66. Time int64 `json:"time,omitempty"`
  67. Error *JSONError `json:"errorDetail,omitempty"`
  68. ErrorMessage string `json:"error,omitempty"` //deprecated
  69. }
  70. func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
  71. if jm.Error != nil {
  72. if jm.Error.Code == 401 {
  73. return fmt.Errorf("Authentication is required.")
  74. }
  75. return jm.Error
  76. }
  77. var endl string
  78. if isTerminal && jm.Stream == "" {
  79. // <ESC>[2K = erase entire current line
  80. fmt.Fprintf(out, "%c[2K\r", 27)
  81. endl = "\r"
  82. } else if jm.Progress != nil { //disable progressbar in non-terminal
  83. return nil
  84. }
  85. if jm.Time != 0 {
  86. fmt.Fprintf(out, "[%s] ", time.Unix(jm.Time, 0))
  87. }
  88. if jm.ID != "" {
  89. fmt.Fprintf(out, "%s: ", jm.ID)
  90. }
  91. if jm.From != "" {
  92. fmt.Fprintf(out, "(from %s) ", jm.From)
  93. }
  94. if jm.Progress != nil {
  95. fmt.Fprintf(out, "%s %s%s", jm.Status, jm.Progress.String(), endl)
  96. } else if jm.ProgressMessage != "" { //deprecated
  97. fmt.Fprintf(out, "%s %s%s", jm.Status, jm.ProgressMessage, endl)
  98. } else if jm.Stream != "" {
  99. fmt.Fprintf(out, "%s%s", jm.Stream, endl)
  100. } else {
  101. fmt.Fprintf(out, "%s%s\n", jm.Status, endl)
  102. }
  103. return nil
  104. }
  105. func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool) error {
  106. var (
  107. dec = json.NewDecoder(in)
  108. ids = make(map[string]int)
  109. diff = 0
  110. )
  111. for {
  112. var jm JSONMessage
  113. if err := dec.Decode(&jm); err != nil {
  114. if err == io.EOF {
  115. break
  116. }
  117. return err
  118. }
  119. if jm.Progress != nil {
  120. jm.Progress.terminalFd = terminalFd
  121. }
  122. if jm.ID != "" && (jm.Progress != nil || jm.ProgressMessage != "") {
  123. line, ok := ids[jm.ID]
  124. if !ok {
  125. line = len(ids)
  126. ids[jm.ID] = line
  127. fmt.Fprintf(out, "\n")
  128. diff = 0
  129. } else {
  130. diff = len(ids) - line
  131. }
  132. if jm.ID != "" && isTerminal {
  133. // <ESC>[{diff}A = move cursor up diff rows
  134. fmt.Fprintf(out, "%c[%dA", 27, diff)
  135. }
  136. }
  137. err := jm.Display(out, isTerminal)
  138. if jm.ID != "" && isTerminal {
  139. // <ESC>[{diff}B = move cursor down diff rows
  140. fmt.Fprintf(out, "%c[%dB", 27, diff)
  141. }
  142. if err != nil {
  143. return err
  144. }
  145. }
  146. return nil
  147. }