2015-03-18 02:18:41 +00:00
|
|
|
package jsonmessage
|
2013-11-28 20:16:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-11-09 17:55:11 +00:00
|
|
|
"os"
|
2013-11-28 20:37:07 +00:00
|
|
|
"strings"
|
2013-11-28 20:16:57 +00:00
|
|
|
"time"
|
2014-05-12 23:40:19 +00:00
|
|
|
|
2016-11-09 17:55:11 +00:00
|
|
|
"github.com/Nvveen/Gotty"
|
|
|
|
|
2015-12-15 19:49:41 +00:00
|
|
|
"github.com/docker/docker/pkg/jsonlog"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2015-12-16 17:26:49 +00:00
|
|
|
"github.com/docker/go-units"
|
2013-11-28 20:16:57 +00:00
|
|
|
)
|
|
|
|
|
2015-08-08 03:28:22 +00:00
|
|
|
// JSONError wraps a concrete Code and Message, `Code` is
|
2016-05-08 01:36:10 +00:00
|
|
|
// is an integer error code, `Message` is the error message.
|
2013-11-28 20:16:57 +00:00
|
|
|
type JSONError struct {
|
|
|
|
Code int `json:"code,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *JSONError) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
2015-08-08 03:28:22 +00:00
|
|
|
// JSONProgress describes a Progress. terminalFd is the fd of the current terminal,
|
|
|
|
// Start is the initial value for the operation. Current is the current status and
|
|
|
|
// value of the progress made towards Total. Total is the end value describing when
|
|
|
|
// we made 100% progress for an operation.
|
2013-11-28 20:16:57 +00:00
|
|
|
type JSONProgress struct {
|
2013-12-02 19:53:04 +00:00
|
|
|
terminalFd uintptr
|
2015-07-23 21:19:58 +00:00
|
|
|
Current int64 `json:"current,omitempty"`
|
|
|
|
Total int64 `json:"total,omitempty"`
|
2013-12-02 19:53:04 +00:00
|
|
|
Start int64 `json:"start,omitempty"`
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *JSONProgress) String() string {
|
2013-12-02 19:53:04 +00:00
|
|
|
var (
|
|
|
|
width = 200
|
|
|
|
pbBox string
|
|
|
|
numbersBox string
|
|
|
|
timeLeftBox string
|
|
|
|
)
|
|
|
|
|
|
|
|
ws, err := term.GetWinsize(p.terminalFd)
|
|
|
|
if err == nil {
|
|
|
|
width = int(ws.Width)
|
|
|
|
}
|
|
|
|
|
2013-12-04 19:57:18 +00:00
|
|
|
if p.Current <= 0 && p.Total <= 0 {
|
2013-11-28 20:16:57 +00:00
|
|
|
return ""
|
|
|
|
}
|
2014-11-25 08:02:47 +00:00
|
|
|
current := units.HumanSize(float64(p.Current))
|
2013-12-04 19:57:18 +00:00
|
|
|
if p.Total <= 0 {
|
2013-12-02 19:53:04 +00:00
|
|
|
return fmt.Sprintf("%8v", current)
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
2014-11-25 08:02:47 +00:00
|
|
|
total := units.HumanSize(float64(p.Total))
|
2013-11-28 20:37:07 +00:00
|
|
|
percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
|
2014-12-17 00:57:37 +00:00
|
|
|
if percentage > 50 {
|
|
|
|
percentage = 50
|
|
|
|
}
|
2013-12-02 19:53:04 +00:00
|
|
|
if width > 110 {
|
2015-12-13 16:00:39 +00:00
|
|
|
// this number can't be negative gh#7136
|
2014-07-28 16:01:43 +00:00
|
|
|
numSpaces := 0
|
|
|
|
if 50-percentage > 0 {
|
|
|
|
numSpaces = 50 - percentage
|
|
|
|
}
|
|
|
|
pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", numSpaces))
|
2013-12-02 19:53:04 +00:00
|
|
|
}
|
2015-08-11 20:47:08 +00:00
|
|
|
|
2013-12-02 19:53:04 +00:00
|
|
|
numbersBox = fmt.Sprintf("%8v/%v", current, total)
|
2013-11-28 20:37:07 +00:00
|
|
|
|
2015-08-11 20:47:08 +00:00
|
|
|
if p.Current > p.Total {
|
|
|
|
// remove total display if the reported current is wonky.
|
|
|
|
numbersBox = fmt.Sprintf("%8v", current)
|
|
|
|
}
|
|
|
|
|
2014-02-25 08:01:46 +00:00
|
|
|
if p.Current > 0 && p.Start > 0 && percentage < 50 {
|
2015-07-23 21:19:58 +00:00
|
|
|
fromStart := time.Now().UTC().Sub(time.Unix(p.Start, 0))
|
2013-12-02 19:53:04 +00:00
|
|
|
perEntry := fromStart / time.Duration(p.Current)
|
|
|
|
left := time.Duration(p.Total-p.Current) * perEntry
|
|
|
|
left = (left / time.Second) * time.Second
|
|
|
|
|
|
|
|
if width > 50 {
|
|
|
|
timeLeftBox = " " + left.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pbBox + numbersBox + timeLeftBox
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
|
|
|
|
2015-08-08 03:28:22 +00:00
|
|
|
// JSONMessage defines a message struct. It describes
|
|
|
|
// the created time, where it from, status, ID of the
|
|
|
|
// message. It's used for docker events.
|
2013-11-28 20:16:57 +00:00
|
|
|
type JSONMessage struct {
|
2013-12-06 19:55:56 +00:00
|
|
|
Stream string `json:"stream,omitempty"`
|
2013-11-28 20:16:57 +00:00
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
Progress *JSONProgress `json:"progressDetail,omitempty"`
|
|
|
|
ProgressMessage string `json:"progress,omitempty"` //deprecated
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
From string `json:"from,omitempty"`
|
|
|
|
Time int64 `json:"time,omitempty"`
|
2015-09-15 19:33:11 +00:00
|
|
|
TimeNano int64 `json:"timeNano,omitempty"`
|
2013-11-28 20:16:57 +00:00
|
|
|
Error *JSONError `json:"errorDetail,omitempty"`
|
|
|
|
ErrorMessage string `json:"error,omitempty"` //deprecated
|
2015-12-21 23:02:44 +00:00
|
|
|
// Aux contains out-of-band data, such as digests for push signing.
|
|
|
|
Aux *json.RawMessage `json:"aux,omitempty"`
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 17:55:11 +00:00
|
|
|
/* Satisfied by gotty.TermInfo as well as noTermInfo from below */
|
|
|
|
type termInfo interface {
|
|
|
|
Parse(attr string, params ...interface{}) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type noTermInfo struct{} // canary used when no terminfo.
|
|
|
|
|
|
|
|
func (ti *noTermInfo) Parse(attr string, params ...interface{}) (string, error) {
|
|
|
|
return "", fmt.Errorf("noTermInfo")
|
|
|
|
}
|
|
|
|
|
|
|
|
func clearLine(out io.Writer, ti termInfo) {
|
|
|
|
// el2 (clear whole line) is not exposed by terminfo.
|
|
|
|
|
|
|
|
// First clear line from beginning to cursor
|
|
|
|
if attr, err := ti.Parse("el1"); err == nil {
|
|
|
|
fmt.Fprintf(out, "%s", attr)
|
|
|
|
} else {
|
2016-11-11 10:59:40 +00:00
|
|
|
fmt.Fprintf(out, "\x1b[1K")
|
2016-11-09 17:55:11 +00:00
|
|
|
}
|
|
|
|
// Then clear line from cursor to end
|
|
|
|
if attr, err := ti.Parse("el"); err == nil {
|
|
|
|
fmt.Fprintf(out, "%s", attr)
|
|
|
|
} else {
|
2016-11-11 10:59:40 +00:00
|
|
|
fmt.Fprintf(out, "\x1b[K")
|
2016-11-09 17:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cursorUp(out io.Writer, ti termInfo, l int) {
|
|
|
|
if l == 0 { // Should never be the case, but be tolerant
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if attr, err := ti.Parse("cuu", l); err == nil {
|
|
|
|
fmt.Fprintf(out, "%s", attr)
|
|
|
|
} else {
|
2016-11-11 10:59:40 +00:00
|
|
|
fmt.Fprintf(out, "\x1b[%dA", l)
|
2016-11-09 17:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cursorDown(out io.Writer, ti termInfo, l int) {
|
|
|
|
if l == 0 { // Should never be the case, but be tolerant
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if attr, err := ti.Parse("cud", l); err == nil {
|
|
|
|
fmt.Fprintf(out, "%s", attr)
|
|
|
|
} else {
|
2016-11-11 10:59:40 +00:00
|
|
|
fmt.Fprintf(out, "\x1b[%dB", l)
|
2016-11-09 17:55:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display displays the JSONMessage to `out`. `termInfo` is non-nil if `out`
|
2015-08-08 03:28:22 +00:00
|
|
|
// is a terminal. If this is the case, it will erase the entire current line
|
2015-12-13 16:00:39 +00:00
|
|
|
// when displaying the progressbar.
|
2016-11-09 17:55:11 +00:00
|
|
|
func (jm *JSONMessage) Display(out io.Writer, termInfo termInfo) error {
|
2013-11-28 20:16:57 +00:00
|
|
|
if jm.Error != nil {
|
|
|
|
if jm.Error.Code == 401 {
|
|
|
|
return fmt.Errorf("Authentication is required.")
|
|
|
|
}
|
|
|
|
return jm.Error
|
|
|
|
}
|
2013-12-02 19:49:11 +00:00
|
|
|
var endl string
|
2016-11-09 17:55:11 +00:00
|
|
|
if termInfo != nil && jm.Stream == "" && jm.Progress != nil {
|
|
|
|
clearLine(out, termInfo)
|
2013-12-06 19:55:56 +00:00
|
|
|
endl = "\r"
|
2016-11-09 17:55:11 +00:00
|
|
|
fmt.Fprintf(out, endl)
|
2014-09-24 07:26:53 +00:00
|
|
|
} else if jm.Progress != nil && jm.Progress.String() != "" { //disable progressbar in non-terminal
|
2013-12-06 21:56:09 +00:00
|
|
|
return nil
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
2015-09-15 19:33:11 +00:00
|
|
|
if jm.TimeNano != 0 {
|
2015-12-15 19:49:41 +00:00
|
|
|
fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(jsonlog.RFC3339NanoFixed))
|
2015-09-15 19:33:11 +00:00
|
|
|
} else if jm.Time != 0 {
|
2015-12-15 19:49:41 +00:00
|
|
|
fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(jsonlog.RFC3339NanoFixed))
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
|
|
|
if jm.ID != "" {
|
|
|
|
fmt.Fprintf(out, "%s: ", jm.ID)
|
|
|
|
}
|
|
|
|
if jm.From != "" {
|
|
|
|
fmt.Fprintf(out, "(from %s) ", jm.From)
|
|
|
|
}
|
2016-11-09 17:55:11 +00:00
|
|
|
if jm.Progress != nil && termInfo != nil {
|
2013-11-28 20:16:57 +00:00
|
|
|
fmt.Fprintf(out, "%s %s%s", jm.Status, jm.Progress.String(), endl)
|
|
|
|
} else if jm.ProgressMessage != "" { //deprecated
|
|
|
|
fmt.Fprintf(out, "%s %s%s", jm.Status, jm.ProgressMessage, endl)
|
2013-12-06 19:55:56 +00:00
|
|
|
} else if jm.Stream != "" {
|
|
|
|
fmt.Fprintf(out, "%s%s", jm.Stream, endl)
|
2013-11-28 20:16:57 +00:00
|
|
|
} else {
|
2013-12-06 19:55:56 +00:00
|
|
|
fmt.Fprintf(out, "%s%s\n", jm.Status, endl)
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-08 03:28:22 +00:00
|
|
|
// DisplayJSONMessagesStream displays a json message stream from `in` to `out`, `isTerminal`
|
|
|
|
// describes if `out` is a terminal. If this is the case, it will print `\n` at the end of
|
|
|
|
// each line and move the cursor while displaying.
|
2015-12-21 23:02:44 +00:00
|
|
|
func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool, auxCallback func(*json.RawMessage)) error {
|
2013-12-02 19:49:11 +00:00
|
|
|
var (
|
2015-12-08 01:01:47 +00:00
|
|
|
dec = json.NewDecoder(in)
|
|
|
|
ids = make(map[string]int)
|
2013-12-02 19:49:11 +00:00
|
|
|
)
|
2016-11-09 17:55:11 +00:00
|
|
|
|
|
|
|
var termInfo termInfo
|
|
|
|
|
|
|
|
if isTerminal {
|
|
|
|
term := os.Getenv("TERM")
|
|
|
|
if term == "" {
|
|
|
|
term = "vt102"
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if termInfo, err = gotty.OpenTermInfo(term); err != nil {
|
|
|
|
termInfo = &noTermInfo{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-28 20:16:57 +00:00
|
|
|
for {
|
2015-12-08 01:01:47 +00:00
|
|
|
diff := 0
|
2013-12-02 19:49:11 +00:00
|
|
|
var jm JSONMessage
|
|
|
|
if err := dec.Decode(&jm); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2013-11-28 20:16:57 +00:00
|
|
|
return err
|
|
|
|
}
|
2013-12-02 19:53:04 +00:00
|
|
|
|
2015-12-21 23:02:44 +00:00
|
|
|
if jm.Aux != nil {
|
|
|
|
if auxCallback != nil {
|
|
|
|
auxCallback(jm.Aux)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-12-02 19:53:04 +00:00
|
|
|
if jm.Progress != nil {
|
|
|
|
jm.Progress.terminalFd = terminalFd
|
|
|
|
}
|
2014-03-31 19:21:57 +00:00
|
|
|
if jm.ID != "" && (jm.Progress != nil || jm.ProgressMessage != "") {
|
2013-11-28 20:16:57 +00:00
|
|
|
line, ok := ids[jm.ID]
|
|
|
|
if !ok {
|
2015-12-08 00:04:42 +00:00
|
|
|
// NOTE: This approach of using len(id) to
|
|
|
|
// figure out the number of lines of history
|
|
|
|
// only works as long as we clear the history
|
|
|
|
// when we output something that's not
|
|
|
|
// accounted for in the map, such as a line
|
|
|
|
// with no ID.
|
2013-11-28 20:16:57 +00:00
|
|
|
line = len(ids)
|
|
|
|
ids[jm.ID] = line
|
2016-11-09 17:55:11 +00:00
|
|
|
if termInfo != nil {
|
2014-09-06 04:16:24 +00:00
|
|
|
fmt.Fprintf(out, "\n")
|
|
|
|
}
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
2016-11-09 16:51:18 +00:00
|
|
|
diff = len(ids) - line
|
2016-11-09 17:55:11 +00:00
|
|
|
if termInfo != nil {
|
|
|
|
cursorUp(out, termInfo, diff)
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
2015-12-08 00:04:42 +00:00
|
|
|
} else {
|
|
|
|
// When outputting something that isn't progress
|
|
|
|
// output, clear the history of previous lines. We
|
|
|
|
// don't want progress entries from some previous
|
|
|
|
// operation to be updated (for example, pull -a
|
|
|
|
// with multiple tags).
|
|
|
|
ids = make(map[string]int)
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
2016-11-09 17:55:11 +00:00
|
|
|
err := jm.Display(out, termInfo)
|
|
|
|
if jm.ID != "" && termInfo != nil {
|
|
|
|
cursorDown(out, termInfo, diff)
|
2013-11-28 20:16:57 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-29 15:11:29 +00:00
|
|
|
|
|
|
|
type stream interface {
|
|
|
|
io.Writer
|
|
|
|
FD() uintptr
|
|
|
|
IsTerminal() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisplayJSONMessagesToStream prints json messages to the output stream
|
|
|
|
func DisplayJSONMessagesToStream(in io.Reader, stream stream, auxCallback func(*json.RawMessage)) error {
|
|
|
|
return DisplayJSONMessagesStream(in, stream, stream.FD(), stream.IsTerminal(), auxCallback)
|
|
|
|
}
|