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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-07-29 14:42:42 +02:00
parent 0515e1c991
commit f4127d76c5
6 changed files with 51 additions and 27 deletions

View File

@@ -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.

View File

@@ -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),

View File

@@ -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)
}

View File

@@ -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)