2018-02-05 16:05:59 -05:00
|
|
|
package jsonmessage // import "github.com/docker/docker/pkg/jsonmessage"
|
2013-11-28 15:16:57 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-11-28 15:37:07 -05:00
|
|
|
"strings"
|
2013-11-28 15:16:57 -05:00
|
|
|
"time"
|
2014-05-12 19:40:19 -04:00
|
|
|
|
2019-08-05 10:37:47 -04:00
|
|
|
units "github.com/docker/go-units"
|
2020-04-16 05:31:08 -04:00
|
|
|
"github.com/moby/term"
|
2018-12-19 13:33:53 -05:00
|
|
|
"github.com/morikuni/aec"
|
2013-11-28 15:16:57 -05:00
|
|
|
)
|
|
|
|
|
2017-09-22 14:40:10 -04:00
|
|
|
// RFC3339NanoFixed is time.RFC3339Nano with nanoseconds padded using zeros to
|
|
|
|
// ensure the formatted time isalways the same number of characters.
|
|
|
|
const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
|
|
|
|
|
2015-08-07 23:28:22 -04:00
|
|
|
// JSONError wraps a concrete Code and Message, `Code` is
|
2016-05-07 21:36:10 -04:00
|
|
|
// is an integer error code, `Message` is the error message.
|
2013-11-28 15:16:57 -05:00
|
|
|
type JSONError struct {
|
|
|
|
Code int `json:"code,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *JSONError) Error() string {
|
|
|
|
return e.Message
|
|
|
|
}
|
|
|
|
|
2015-08-07 23:28:22 -04: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 15:16:57 -05:00
|
|
|
type JSONProgress struct {
|
2013-12-02 14:53:04 -05:00
|
|
|
terminalFd uintptr
|
2015-07-23 17:19:58 -04:00
|
|
|
Current int64 `json:"current,omitempty"`
|
|
|
|
Total int64 `json:"total,omitempty"`
|
2013-12-02 14:53:04 -05:00
|
|
|
Start int64 `json:"start,omitempty"`
|
2017-02-16 20:05:36 -05:00
|
|
|
// If true, don't show xB/yB
|
2017-05-03 00:22:56 -04:00
|
|
|
HideCounts bool `json:"hidecounts,omitempty"`
|
|
|
|
Units string `json:"units,omitempty"`
|
2017-12-29 15:13:13 -05:00
|
|
|
nowFunc func() time.Time
|
|
|
|
winSize int
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *JSONProgress) String() string {
|
2013-12-02 14:53:04 -05:00
|
|
|
var (
|
2017-12-29 15:13:13 -05:00
|
|
|
width = p.width()
|
2013-12-02 14:53:04 -05:00
|
|
|
pbBox string
|
|
|
|
numbersBox string
|
|
|
|
timeLeftBox string
|
|
|
|
)
|
2013-12-04 14:57:18 -05:00
|
|
|
if p.Current <= 0 && p.Total <= 0 {
|
2013-11-28 15:16:57 -05:00
|
|
|
return ""
|
|
|
|
}
|
2013-12-04 14:57:18 -05:00
|
|
|
if p.Total <= 0 {
|
2017-05-03 00:22:56 -04:00
|
|
|
switch p.Units {
|
|
|
|
case "":
|
|
|
|
current := units.HumanSize(float64(p.Current))
|
|
|
|
return fmt.Sprintf("%8v", current)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%d %s", p.Current, p.Units)
|
|
|
|
}
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2017-05-03 00:22:56 -04:00
|
|
|
|
2013-11-28 15:37:07 -05:00
|
|
|
percentage := int(float64(p.Current)/float64(p.Total)*100) / 2
|
2014-12-16 19:57:37 -05:00
|
|
|
if percentage > 50 {
|
|
|
|
percentage = 50
|
|
|
|
}
|
2013-12-02 14:53:04 -05:00
|
|
|
if width > 110 {
|
2015-12-13 11:00:39 -05:00
|
|
|
// this number can't be negative gh#7136
|
2014-07-28 12:01:43 -04:00
|
|
|
numSpaces := 0
|
|
|
|
if 50-percentage > 0 {
|
|
|
|
numSpaces = 50 - percentage
|
|
|
|
}
|
|
|
|
pbBox = fmt.Sprintf("[%s>%s] ", strings.Repeat("=", percentage), strings.Repeat(" ", numSpaces))
|
2013-12-02 14:53:04 -05:00
|
|
|
}
|
2015-08-11 16:47:08 -04:00
|
|
|
|
2017-05-03 00:22:56 -04:00
|
|
|
switch {
|
|
|
|
case p.HideCounts:
|
|
|
|
case p.Units == "": // no units, use bytes
|
|
|
|
current := units.HumanSize(float64(p.Current))
|
|
|
|
total := units.HumanSize(float64(p.Total))
|
|
|
|
|
2017-02-16 20:05:36 -05:00
|
|
|
numbersBox = fmt.Sprintf("%8v/%v", current, total)
|
2013-11-28 15:37:07 -05:00
|
|
|
|
2017-02-16 20:05:36 -05:00
|
|
|
if p.Current > p.Total {
|
|
|
|
// remove total display if the reported current is wonky.
|
|
|
|
numbersBox = fmt.Sprintf("%8v", current)
|
|
|
|
}
|
2017-05-03 00:22:56 -04:00
|
|
|
default:
|
|
|
|
numbersBox = fmt.Sprintf("%d/%d %s", p.Current, p.Total, p.Units)
|
|
|
|
|
|
|
|
if p.Current > p.Total {
|
|
|
|
// remove total display if the reported current is wonky.
|
|
|
|
numbersBox = fmt.Sprintf("%d %s", p.Current, p.Units)
|
|
|
|
}
|
2015-08-11 16:47:08 -04:00
|
|
|
}
|
|
|
|
|
2014-02-25 03:01:46 -05:00
|
|
|
if p.Current > 0 && p.Start > 0 && percentage < 50 {
|
2017-12-29 15:13:13 -05:00
|
|
|
fromStart := p.now().Sub(time.Unix(p.Start, 0))
|
2013-12-02 14:53:04 -05: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 15:16:57 -05:00
|
|
|
}
|
|
|
|
|
2017-12-29 15:13:13 -05:00
|
|
|
// shim for testing
|
|
|
|
func (p *JSONProgress) now() time.Time {
|
|
|
|
if p.nowFunc == nil {
|
|
|
|
p.nowFunc = func() time.Time {
|
|
|
|
return time.Now().UTC()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p.nowFunc()
|
|
|
|
}
|
|
|
|
|
|
|
|
// shim for testing
|
|
|
|
func (p *JSONProgress) width() int {
|
|
|
|
if p.winSize != 0 {
|
|
|
|
return p.winSize
|
|
|
|
}
|
|
|
|
ws, err := term.GetWinsize(p.terminalFd)
|
|
|
|
if err == nil {
|
|
|
|
return int(ws.Width)
|
|
|
|
}
|
|
|
|
return 200
|
|
|
|
}
|
|
|
|
|
2015-08-07 23:28:22 -04: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 15:16:57 -05:00
|
|
|
type JSONMessage struct {
|
2013-12-06 14:55:56 -05:00
|
|
|
Stream string `json:"stream,omitempty"`
|
2013-11-28 15:16:57 -05:00
|
|
|
Status string `json:"status,omitempty"`
|
|
|
|
Progress *JSONProgress `json:"progressDetail,omitempty"`
|
2019-10-12 11:35:13 -04:00
|
|
|
ProgressMessage string `json:"progress,omitempty"` // deprecated
|
2013-11-28 15:16:57 -05:00
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
From string `json:"from,omitempty"`
|
|
|
|
Time int64 `json:"time,omitempty"`
|
2015-09-15 15:33:11 -04:00
|
|
|
TimeNano int64 `json:"timeNano,omitempty"`
|
2013-11-28 15:16:57 -05:00
|
|
|
Error *JSONError `json:"errorDetail,omitempty"`
|
2019-10-12 11:35:13 -04:00
|
|
|
ErrorMessage string `json:"error,omitempty"` // deprecated
|
2017-04-06 08:33:56 -04:00
|
|
|
// Aux contains out-of-band data, such as digests for push signing and image id after building.
|
2015-12-21 18:02:44 -05:00
|
|
|
Aux *json.RawMessage `json:"aux,omitempty"`
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:33:53 -05:00
|
|
|
func clearLine(out io.Writer) {
|
|
|
|
eraseMode := aec.EraseModes.All
|
|
|
|
cl := aec.EraseLine(eraseMode)
|
|
|
|
fmt.Fprint(out, cl)
|
2016-11-09 12:55:11 -05:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:33:53 -05:00
|
|
|
func cursorUp(out io.Writer, l uint) {
|
|
|
|
fmt.Fprint(out, aec.Up(l))
|
2016-11-09 12:55:11 -05:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:33:53 -05:00
|
|
|
func cursorDown(out io.Writer, l uint) {
|
|
|
|
fmt.Fprint(out, aec.Down(l))
|
2016-11-09 12:55:11 -05:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:33:53 -05:00
|
|
|
// Display displays the JSONMessage to `out`. If `isTerminal` is true, it will erase the
|
|
|
|
// entire current line when displaying the progressbar.
|
|
|
|
func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
|
2013-11-28 15:16:57 -05:00
|
|
|
if jm.Error != nil {
|
|
|
|
if jm.Error.Code == 401 {
|
2017-08-17 15:16:30 -04:00
|
|
|
return fmt.Errorf("authentication is required")
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
return jm.Error
|
|
|
|
}
|
2013-12-02 14:49:11 -05:00
|
|
|
var endl string
|
2018-12-19 13:33:53 -05:00
|
|
|
if isTerminal && jm.Stream == "" && jm.Progress != nil {
|
|
|
|
clearLine(out)
|
2013-12-06 14:55:56 -05:00
|
|
|
endl = "\r"
|
2019-10-12 11:35:13 -04:00
|
|
|
fmt.Fprint(out, endl)
|
2019-11-27 09:40:39 -05:00
|
|
|
} else if jm.Progress != nil && jm.Progress.String() != "" { // disable progressbar in non-terminal
|
2013-12-06 16:56:09 -05:00
|
|
|
return nil
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2015-09-15 15:33:11 -04:00
|
|
|
if jm.TimeNano != 0 {
|
2017-09-22 14:40:10 -04:00
|
|
|
fmt.Fprintf(out, "%s ", time.Unix(0, jm.TimeNano).Format(RFC3339NanoFixed))
|
2015-09-15 15:33:11 -04:00
|
|
|
} else if jm.Time != 0 {
|
2017-09-22 14:40:10 -04:00
|
|
|
fmt.Fprintf(out, "%s ", time.Unix(jm.Time, 0).Format(RFC3339NanoFixed))
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
if jm.ID != "" {
|
|
|
|
fmt.Fprintf(out, "%s: ", jm.ID)
|
|
|
|
}
|
|
|
|
if jm.From != "" {
|
|
|
|
fmt.Fprintf(out, "(from %s) ", jm.From)
|
|
|
|
}
|
2018-12-19 13:33:53 -05:00
|
|
|
if jm.Progress != nil && isTerminal {
|
2013-11-28 15:16:57 -05:00
|
|
|
fmt.Fprintf(out, "%s %s%s", jm.Status, jm.Progress.String(), endl)
|
2019-10-12 11:35:13 -04:00
|
|
|
} else if jm.ProgressMessage != "" { // deprecated
|
2013-11-28 15:16:57 -05:00
|
|
|
fmt.Fprintf(out, "%s %s%s", jm.Status, jm.ProgressMessage, endl)
|
2013-12-06 14:55:56 -05:00
|
|
|
} else if jm.Stream != "" {
|
|
|
|
fmt.Fprintf(out, "%s%s", jm.Stream, endl)
|
2013-11-28 15:16:57 -05:00
|
|
|
} else {
|
2013-12-06 14:55:56 -05:00
|
|
|
fmt.Fprintf(out, "%s%s\n", jm.Status, endl)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-07 23:28:22 -04: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.
|
2018-05-14 18:47:50 -04:00
|
|
|
func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool, auxCallback func(JSONMessage)) error {
|
2013-12-02 14:49:11 -05:00
|
|
|
var (
|
2015-12-07 20:01:47 -05:00
|
|
|
dec = json.NewDecoder(in)
|
2018-12-19 13:33:53 -05:00
|
|
|
ids = make(map[string]uint)
|
2013-12-02 14:49:11 -05:00
|
|
|
)
|
2016-11-09 12:55:11 -05:00
|
|
|
|
2013-11-28 15:16:57 -05:00
|
|
|
for {
|
2018-12-19 13:33:53 -05:00
|
|
|
var diff uint
|
2013-12-02 14:49:11 -05:00
|
|
|
var jm JSONMessage
|
|
|
|
if err := dec.Decode(&jm); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2013-11-28 15:16:57 -05:00
|
|
|
return err
|
|
|
|
}
|
2013-12-02 14:53:04 -05:00
|
|
|
|
2015-12-21 18:02:44 -05:00
|
|
|
if jm.Aux != nil {
|
|
|
|
if auxCallback != nil {
|
2018-05-14 18:47:50 -04:00
|
|
|
auxCallback(jm)
|
2015-12-21 18:02:44 -05:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2013-12-02 14:53:04 -05:00
|
|
|
if jm.Progress != nil {
|
|
|
|
jm.Progress.terminalFd = terminalFd
|
|
|
|
}
|
2014-03-31 15:21:57 -04:00
|
|
|
if jm.ID != "" && (jm.Progress != nil || jm.ProgressMessage != "") {
|
2013-11-28 15:16:57 -05:00
|
|
|
line, ok := ids[jm.ID]
|
|
|
|
if !ok {
|
2015-12-07 19:04:42 -05: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.
|
2018-12-19 13:33:53 -05:00
|
|
|
line = uint(len(ids))
|
2013-11-28 15:16:57 -05:00
|
|
|
ids[jm.ID] = line
|
2018-12-19 13:33:53 -05:00
|
|
|
if isTerminal {
|
2014-09-06 00:16:24 -04:00
|
|
|
fmt.Fprintf(out, "\n")
|
|
|
|
}
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2018-12-19 13:33:53 -05:00
|
|
|
diff = uint(len(ids)) - line
|
|
|
|
if isTerminal {
|
|
|
|
cursorUp(out, diff)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2015-12-07 19:04:42 -05: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).
|
2018-12-19 13:33:53 -05:00
|
|
|
ids = make(map[string]uint)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
2018-12-19 13:33:53 -05:00
|
|
|
err := jm.Display(out, isTerminal)
|
|
|
|
if jm.ID != "" && isTerminal {
|
|
|
|
cursorDown(out, diff)
|
2013-11-28 15:16:57 -05:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-08-29 11:11:29 -04:00
|
|
|
|
2022-08-29 06:43:13 -04:00
|
|
|
// Stream is an io.Writer for output with utilities to get the output's file
|
|
|
|
// descriptor and to detect wether it's a terminal.
|
|
|
|
//
|
|
|
|
// it is subset of the streams.Out type in
|
|
|
|
// https://pkg.go.dev/github.com/docker/cli@v20.10.17+incompatible/cli/streams#Out
|
|
|
|
type Stream interface {
|
2016-08-29 11:11:29 -04:00
|
|
|
io.Writer
|
|
|
|
FD() uintptr
|
|
|
|
IsTerminal() bool
|
|
|
|
}
|
|
|
|
|
2022-08-29 06:43:13 -04:00
|
|
|
// DisplayJSONMessagesToStream prints json messages to the output Stream. It is
|
|
|
|
// used by the Docker CLI to print JSONMessage streams.
|
|
|
|
func DisplayJSONMessagesToStream(in io.Reader, stream Stream, auxCallback func(JSONMessage)) error {
|
2016-08-29 11:11:29 -04:00
|
|
|
return DisplayJSONMessagesStream(in, stream, stream.FD(), stream.IsTerminal(), auxCallback)
|
|
|
|
}
|