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