Skip to content

Commit f4127d7

Browse files
committed
pkg/jsonmessage: move JSONProgress to api/types/jsonstream
Move the type to the API, but embed it, so that we keep the methods on the struct in this package. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 0515e1c commit f4127d7

6 files changed

Lines changed: 51 additions & 27 deletions

File tree

api/types/jsonstream/progress.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package jsonstream
2+
3+
// Progress describes a progress message in a JSON stream.
4+
type Progress struct {
5+
Current int64 `json:"current,omitempty"` // Current is the current status and value of the progress made towards Total.
6+
Total int64 `json:"total,omitempty"` // Total is the end value describing when we made 100% progress for an operation.
7+
Start int64 `json:"start,omitempty"` // Start is the initial value for the operation.
8+
HideCounts bool `json:"hidecounts,omitempty"` // HideCounts. if true, hides the progress count indicator (xB/yB).
9+
Units string `json:"units,omitempty"` // Units is the unit to print for progress. It defaults to "bytes" if empty.
10+
}

pkg/jsonmessage/jsonmessage.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,7 @@ const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
1818

1919
// JSONProgress describes a progress message in a JSON stream.
2020
type JSONProgress struct {
21-
// Current is the current status and value of the progress made towards Total.
22-
Current int64 `json:"current,omitempty"`
23-
// Total is the end value describing when we made 100% progress for an operation.
24-
Total int64 `json:"total,omitempty"`
25-
// Start is the initial value for the operation.
26-
Start int64 `json:"start,omitempty"`
27-
// HideCounts. if true, hides the progress count indicator (xB/yB).
28-
HideCounts bool `json:"hidecounts,omitempty"`
29-
// Units is the unit to print for progress. It defaults to "bytes" if empty.
30-
Units string `json:"units,omitempty"`
21+
jsonstream.Progress
3122

3223
// terminalFd is the fd of the current terminal, if any. It is used
3324
// to get the terminal width.

pkg/jsonmessage/jsonmessage_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ func TestProgressString(t *testing.T) {
4040
},
4141
{
4242
name: "progress 1",
43-
progress: JSONProgress{Current: 1},
43+
progress: JSONProgress{Progress: jsonstream.Progress{Current: 1}},
4444
expected: shortAndLong(" 1B", " 1B"),
4545
},
4646
{
4747
name: "some progress with a start time",
4848
progress: JSONProgress{
49-
Current: 20,
50-
Total: 100,
51-
Start: start.Unix(),
49+
Progress: jsonstream.Progress{
50+
Current: 20,
51+
Total: 100,
52+
Start: start.Unix(),
53+
},
5254
nowFunc: timeAfter(time.Second),
5355
},
5456
expected: shortAndLong(
@@ -58,39 +60,39 @@ func TestProgressString(t *testing.T) {
5860
},
5961
{
6062
name: "some progress without a start time",
61-
progress: JSONProgress{Current: 50, Total: 100},
63+
progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 100}},
6264
expected: shortAndLong(
6365
" 50B/100B",
6466
"[=========================> ] 50B/100B",
6567
),
6668
},
6769
{
6870
name: "current more than total is not negative gh#7136",
69-
progress: JSONProgress{Current: 50, Total: 40},
71+
progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 40}},
7072
expected: shortAndLong(
7173
" 50B",
7274
"[==================================================>] 50B",
7375
),
7476
},
7577
{
7678
name: "with units",
77-
progress: JSONProgress{Current: 50, Total: 100, Units: "units"},
79+
progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 100, Units: "units"}},
7880
expected: shortAndLong(
7981
"50/100 units",
8082
"[=========================> ] 50/100 units",
8183
),
8284
},
8385
{
8486
name: "current more than total with units is not negative ",
85-
progress: JSONProgress{Current: 50, Total: 40, Units: "units"},
87+
progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 40, Units: "units"}},
8688
expected: shortAndLong(
8789
"50 units",
8890
"[==================================================>] 50 units",
8991
),
9092
},
9193
{
9294
name: "hide counts",
93-
progress: JSONProgress{Current: 50, Total: 100, HideCounts: true},
95+
progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 100, HideCounts: true}},
9496
expected: shortAndLong(
9597
"",
9698
"[=========================> ] ",
@@ -172,7 +174,7 @@ func TestJSONMessageDisplay(t *testing.T) {
172174
{
173175
Status: "status",
174176
Stream: "",
175-
Progress: &JSONProgress{Current: 1},
177+
Progress: &JSONProgress{Progress: jsonstream.Progress{Current: 1}},
176178
}: {
177179
"",
178180
fmt.Sprintf("%c[2K\rstatus 1B\r", 27),

pkg/streamformatter/streamformatter.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,14 @@ func (out *progressOutput) WriteProgress(prog progress.Progress) error {
120120
if prog.Message != "" {
121121
formatted = out.sf.formatStatus(prog.ID, prog.Message)
122122
} else {
123-
jsonProgress := jsonmessage.JSONProgress{Current: prog.Current, Total: prog.Total, HideCounts: prog.HideCounts, Units: prog.Units}
123+
jsonProgress := jsonmessage.JSONProgress{
124+
Progress: jsonstream.Progress{
125+
Current: prog.Current,
126+
Total: prog.Total,
127+
HideCounts: prog.HideCounts,
128+
Units: prog.Units,
129+
},
130+
}
124131
formatted = out.sf.formatProgress(prog.ID, prog.Action, &jsonProgress, prog.Aux)
125132
}
126133

pkg/streamformatter/streamformatter_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ func TestRawProgressFormatterFormatStatus(t *testing.T) {
2424
func TestRawProgressFormatterFormatProgress(t *testing.T) {
2525
sf := rawProgressFormatter{}
2626
jsonProgress := &jsonmessage.JSONProgress{
27-
Current: 15,
28-
Total: 30,
29-
Start: 1,
27+
Progress: jsonstream.Progress{
28+
Current: 15,
29+
Total: 30,
30+
Start: 1,
31+
},
3032
}
3133
res := sf.formatProgress("id", "action", jsonProgress, nil)
3234
out := string(res)
@@ -57,9 +59,11 @@ func TestFormatJSONError(t *testing.T) {
5759
func TestJsonProgressFormatterFormatProgress(t *testing.T) {
5860
sf := &jsonProgressFormatter{}
5961
jsonProgress := &jsonmessage.JSONProgress{
60-
Current: 15,
61-
Total: 30,
62-
Start: 1,
62+
Progress: jsonstream.Progress{
63+
Current: 15,
64+
Total: 30,
65+
Start: 1,
66+
},
6367
}
6468
aux := "aux message"
6569
res := sf.formatProgress("id", "action", jsonProgress, aux)

vendor/github.com/moby/moby/api/types/jsonstream/progress.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)