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

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

View File

@@ -18,16 +18,7 @@ const RFC3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
// JSONProgress describes a progress message in a JSON stream. // JSONProgress describes a progress message in a JSON stream.
type JSONProgress struct { type JSONProgress struct {
// Current is the current status and value of the progress made towards Total. jsonstream.Progress
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"`
// terminalFd is the fd of the current terminal, if any. It is used // terminalFd is the fd of the current terminal, if any. It is used
// to get the terminal width. // to get the terminal width.

View File

@@ -40,15 +40,17 @@ func TestProgressString(t *testing.T) {
}, },
{ {
name: "progress 1", name: "progress 1",
progress: JSONProgress{Current: 1}, progress: JSONProgress{Progress: jsonstream.Progress{Current: 1}},
expected: shortAndLong(" 1B", " 1B"), expected: shortAndLong(" 1B", " 1B"),
}, },
{ {
name: "some progress with a start time", name: "some progress with a start time",
progress: JSONProgress{ progress: JSONProgress{
Current: 20, Progress: jsonstream.Progress{
Total: 100, Current: 20,
Start: start.Unix(), Total: 100,
Start: start.Unix(),
},
nowFunc: timeAfter(time.Second), nowFunc: timeAfter(time.Second),
}, },
expected: shortAndLong( expected: shortAndLong(
@@ -58,7 +60,7 @@ func TestProgressString(t *testing.T) {
}, },
{ {
name: "some progress without a start time", name: "some progress without a start time",
progress: JSONProgress{Current: 50, Total: 100}, progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 100}},
expected: shortAndLong( expected: shortAndLong(
" 50B/100B", " 50B/100B",
"[=========================> ] 50B/100B", "[=========================> ] 50B/100B",
@@ -66,7 +68,7 @@ func TestProgressString(t *testing.T) {
}, },
{ {
name: "current more than total is not negative gh#7136", 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( expected: shortAndLong(
" 50B", " 50B",
"[==================================================>] 50B", "[==================================================>] 50B",
@@ -74,7 +76,7 @@ func TestProgressString(t *testing.T) {
}, },
{ {
name: "with units", name: "with units",
progress: JSONProgress{Current: 50, Total: 100, Units: "units"}, progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 100, Units: "units"}},
expected: shortAndLong( expected: shortAndLong(
"50/100 units", "50/100 units",
"[=========================> ] 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 ", 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( expected: shortAndLong(
"50 units", "50 units",
"[==================================================>] 50 units", "[==================================================>] 50 units",
@@ -90,7 +92,7 @@ func TestProgressString(t *testing.T) {
}, },
{ {
name: "hide counts", name: "hide counts",
progress: JSONProgress{Current: 50, Total: 100, HideCounts: true}, progress: JSONProgress{Progress: jsonstream.Progress{Current: 50, Total: 100, HideCounts: true}},
expected: shortAndLong( expected: shortAndLong(
"", "",
"[=========================> ] ", "[=========================> ] ",
@@ -172,7 +174,7 @@ func TestJSONMessageDisplay(t *testing.T) {
{ {
Status: "status", Status: "status",
Stream: "", Stream: "",
Progress: &JSONProgress{Current: 1}, Progress: &JSONProgress{Progress: jsonstream.Progress{Current: 1}},
}: { }: {
"", "",
fmt.Sprintf("%c[2K\rstatus 1B\r", 27), 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 != "" { if prog.Message != "" {
formatted = out.sf.formatStatus(prog.ID, prog.Message) formatted = out.sf.formatStatus(prog.ID, prog.Message)
} else { } 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) 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) { func TestRawProgressFormatterFormatProgress(t *testing.T) {
sf := rawProgressFormatter{} sf := rawProgressFormatter{}
jsonProgress := &jsonmessage.JSONProgress{ jsonProgress := &jsonmessage.JSONProgress{
Current: 15, Progress: jsonstream.Progress{
Total: 30, Current: 15,
Start: 1, Total: 30,
Start: 1,
},
} }
res := sf.formatProgress("id", "action", jsonProgress, nil) res := sf.formatProgress("id", "action", jsonProgress, nil)
out := string(res) out := string(res)
@@ -57,9 +59,11 @@ func TestFormatJSONError(t *testing.T) {
func TestJsonProgressFormatterFormatProgress(t *testing.T) { func TestJsonProgressFormatterFormatProgress(t *testing.T) {
sf := &jsonProgressFormatter{} sf := &jsonProgressFormatter{}
jsonProgress := &jsonmessage.JSONProgress{ jsonProgress := &jsonmessage.JSONProgress{
Current: 15, Progress: jsonstream.Progress{
Total: 30, Current: 15,
Start: 1, Total: 30,
Start: 1,
},
} }
aux := "aux message" aux := "aux message"
res := sf.formatProgress("id", "action", jsonProgress, aux) res := sf.formatProgress("id", "action", jsonProgress, aux)

View File

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