diff --git a/api/types/jsonstream/progress.go b/api/types/jsonstream/progress.go new file mode 100644 index 0000000000..5c38b3b5ef --- /dev/null +++ b/api/types/jsonstream/progress.go @@ -0,0 +1,10 @@ +package jsonstream + +// Progress describes a progress message in a JSON stream. +type Progress struct { + Current int64 `json:"current,omitempty"` // Current is the current status and value of the progress made towards Total. + Total int64 `json:"total,omitempty"` // Total is the end value describing when we made 100% progress for an operation. + Start int64 `json:"start,omitempty"` // Start is the initial value for the operation. + HideCounts bool `json:"hidecounts,omitempty"` // HideCounts. if true, hides the progress count indicator (xB/yB). + Units string `json:"units,omitempty"` // Units is the unit to print for progress. It defaults to "bytes" if empty. +} diff --git a/pkg/jsonmessage/jsonmessage.go b/pkg/jsonmessage/jsonmessage.go index 5b09d9ba72..4ca5692d7a 100644 --- a/pkg/jsonmessage/jsonmessage.go +++ b/pkg/jsonmessage/jsonmessage.go @@ -18,16 +18,7 @@ const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00" // JSONProgress describes a progress message in a JSON stream. type JSONProgress struct { - // Current is the current status and value of the progress made towards Total. - Current int64 `json:"current,omitempty"` - // Total is the end value describing when we made 100% progress for an operation. - Total int64 `json:"total,omitempty"` - // Start is the initial value for the operation. - Start int64 `json:"start,omitempty"` - // HideCounts. if true, hides the progress count indicator (xB/yB). - HideCounts bool `json:"hidecounts,omitempty"` - // Units is the unit to print for progress. It defaults to "bytes" if empty. - Units string `json:"units,omitempty"` + jsonstream.Progress // terminalFd is the fd of the current terminal, if any. It is used // to get the terminal width. diff --git a/pkg/jsonmessage/jsonmessage_test.go b/pkg/jsonmessage/jsonmessage_test.go index 70fde7d52f..aa88a2c70c 100644 --- a/pkg/jsonmessage/jsonmessage_test.go +++ b/pkg/jsonmessage/jsonmessage_test.go @@ -40,15 +40,17 @@ func TestProgressString(t *testing.T) { }, { name: "progress 1", - progress: JSONProgress{Current: 1}, + progress: JSONProgress{Progress: jsonstream.Progress{Current: 1}}, expected: shortAndLong(" 1B", " 1B"), }, { name: "some progress with a start time", progress: JSONProgress{ - Current: 20, - Total: 100, - Start: start.Unix(), + Progress: jsonstream.Progress{ + Current: 20, + Total: 100, + Start: start.Unix(), + }, nowFunc: timeAfter(time.Second), }, expected: shortAndLong( @@ -58,7 +60,7 @@ func TestProgressString(t *testing.T) { }, { name: "some progress without a start time", - progress: JSONProgress{Current: 50, Total: 100}, + progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 100}}, expected: shortAndLong( " 50B/100B", "[=========================> ] 50B/100B", @@ -66,7 +68,7 @@ func TestProgressString(t *testing.T) { }, { name: "current more than total is not negative gh#7136", - progress: JSONProgress{Current: 50, Total: 40}, + progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 40}}, expected: shortAndLong( " 50B", "[==================================================>] 50B", @@ -74,7 +76,7 @@ func TestProgressString(t *testing.T) { }, { name: "with units", - progress: JSONProgress{Current: 50, Total: 100, Units: "units"}, + progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 100, Units: "units"}}, expected: shortAndLong( "50/100 units", "[=========================> ] 50/100 units", @@ -82,7 +84,7 @@ func TestProgressString(t *testing.T) { }, { name: "current more than total with units is not negative ", - progress: JSONProgress{Current: 50, Total: 40, Units: "units"}, + progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 40, Units: "units"}}, expected: shortAndLong( "50 units", "[==================================================>] 50 units", @@ -90,7 +92,7 @@ func TestProgressString(t *testing.T) { }, { name: "hide counts", - progress: JSONProgress{Current: 50, Total: 100, HideCounts: true}, + progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 100, HideCounts: true}}, expected: shortAndLong( "", "[=========================> ] ", @@ -172,7 +174,7 @@ func TestJSONMessageDisplay(t *testing.T) { { Status: "status", Stream: "", - Progress: &JSONProgress{Current: 1}, + Progress: &JSONProgress{Progress: jsonstream.Progress{Current: 1}}, }: { "", fmt.Sprintf("%c[2K\rstatus 1B\r", 27), diff --git a/pkg/streamformatter/streamformatter.go b/pkg/streamformatter/streamformatter.go index dc30728430..95d2b50c96 100644 --- a/pkg/streamformatter/streamformatter.go +++ b/pkg/streamformatter/streamformatter.go @@ -120,7 +120,14 @@ func (out *progressOutput) WriteProgress(prog progress.Progress) error { if prog.Message != "" { formatted = out.sf.formatStatus(prog.ID, prog.Message) } else { - jsonProgress := jsonmessage.JSONProgress{Current: prog.Current, Total: prog.Total, HideCounts: prog.HideCounts, Units: prog.Units} + jsonProgress := jsonmessage.JSONProgress{ + Progress: jsonstream.Progress{ + Current: prog.Current, + Total: prog.Total, + HideCounts: prog.HideCounts, + Units: prog.Units, + }, + } formatted = out.sf.formatProgress(prog.ID, prog.Action, &jsonProgress, prog.Aux) } diff --git a/pkg/streamformatter/streamformatter_test.go b/pkg/streamformatter/streamformatter_test.go index e97bacf791..316b078b9a 100644 --- a/pkg/streamformatter/streamformatter_test.go +++ b/pkg/streamformatter/streamformatter_test.go @@ -24,9 +24,11 @@ func TestRawProgressFormatterFormatStatus(t *testing.T) { func TestRawProgressFormatterFormatProgress(t *testing.T) { sf := rawProgressFormatter{} jsonProgress := &jsonmessage.JSONProgress{ - Current: 15, - Total: 30, - Start: 1, + Progress: jsonstream.Progress{ + Current: 15, + Total: 30, + Start: 1, + }, } res := sf.formatProgress("id", "action", jsonProgress, nil) out := string(res) @@ -57,9 +59,11 @@ func TestFormatJSONError(t *testing.T) { func TestJsonProgressFormatterFormatProgress(t *testing.T) { sf := &jsonProgressFormatter{} jsonProgress := &jsonmessage.JSONProgress{ - Current: 15, - Total: 30, - Start: 1, + Progress: jsonstream.Progress{ + Current: 15, + Total: 30, + Start: 1, + }, } aux := "aux message" res := sf.formatProgress("id", "action", jsonProgress, aux) diff --git a/vendor/github.com/moby/moby/api/types/jsonstream/progress.go b/vendor/github.com/moby/moby/api/types/jsonstream/progress.go new file mode 100644 index 0000000000..5c38b3b5ef --- /dev/null +++ b/vendor/github.com/moby/moby/api/types/jsonstream/progress.go @@ -0,0 +1,10 @@ +package jsonstream + +// Progress describes a progress message in a JSON stream. +type Progress struct { + Current int64 `json:"current,omitempty"` // Current is the current status and value of the progress made towards Total. + Total int64 `json:"total,omitempty"` // Total is the end value describing when we made 100% progress for an operation. + Start int64 `json:"start,omitempty"` // Start is the initial value for the operation. + HideCounts bool `json:"hidecounts,omitempty"` // HideCounts. if true, hides the progress count indicator (xB/yB). + Units string `json:"units,omitempty"` // Units is the unit to print for progress. It defaults to "bytes" if empty. +}