Skip to content

Commit 4b19eed

Browse files
austinvazquezclaude
andcommitted
Refactor to remove dependency on github.com/moby/moby/pkg/streamformatter
Co-authored-by: Claude <[email protected]> Signed-off-by: Austin Vazquez <[email protected]>
1 parent 7b4cde6 commit 4b19eed

File tree

9 files changed

+126
-62
lines changed

9 files changed

+126
-62
lines changed

cli/command/container/run_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212
"github.com/creack/pty"
1313
"github.com/docker/cli/cli"
1414
"github.com/docker/cli/cli/streams"
15+
"github.com/docker/cli/internal/streamformatter"
1516
"github.com/docker/cli/internal/test"
1617
"github.com/docker/cli/internal/test/notary"
1718
"github.com/moby/moby/api/pkg/progress"
18-
"github.com/moby/moby/api/pkg/streamformatter"
1919
"github.com/moby/moby/api/types"
2020
"github.com/moby/moby/api/types/container"
2121
"github.com/moby/moby/api/types/network"

cli/command/image/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
"github.com/docker/cli/cli/command/image/build"
2020
"github.com/docker/cli/cli/streams"
2121
"github.com/docker/cli/internal/jsonstream"
22+
"github.com/docker/cli/internal/streamformatter"
2223
"github.com/docker/cli/opts"
2324
"github.com/moby/go-archive"
2425
"github.com/moby/moby/api/pkg/progress"
25-
"github.com/moby/moby/api/pkg/streamformatter"
2626
buildtypes "github.com/moby/moby/api/types/build"
2727
"github.com/moby/moby/api/types/container"
2828
registrytypes "github.com/moby/moby/api/types/registry"

cli/command/image/build/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import (
1818
"time"
1919

2020
"github.com/docker/cli/cli/command/image/build/internal/git"
21+
"github.com/docker/cli/internal/streamformatter"
2122
"github.com/moby/go-archive"
2223
"github.com/moby/go-archive/compression"
2324
"github.com/moby/moby/api/pkg/progress"
24-
"github.com/moby/moby/api/pkg/streamformatter"
2525
"github.com/moby/patternmatcher"
2626
)
2727

cli/command/service/progress/progress.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"time"
1313

1414
"github.com/docker/cli/cli/command/formatter"
15+
"github.com/docker/cli/internal/streamformatter"
1516
"github.com/moby/moby/api/pkg/progress"
16-
"github.com/moby/moby/api/pkg/streamformatter"
1717
"github.com/moby/moby/api/types/swarm"
1818
"github.com/moby/moby/client"
1919
)

cli/command/swarm/progress/root_rotation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"os/signal"
99
"time"
1010

11+
"github.com/docker/cli/internal/streamformatter"
1112
"github.com/moby/moby/api/pkg/progress"
12-
"github.com/moby/moby/api/pkg/streamformatter"
1313
"github.com/moby/moby/api/types/swarm"
1414
"github.com/moby/moby/client"
1515
"github.com/opencontainers/go-digest"

vendor/github.com/moby/moby/api/pkg/streamformatter/streamformatter.go renamed to internal/streamformatter/streamformatter.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ func FormatError(err error) []byte {
6363
return []byte(`{"error":"format error"}` + streamNewline)
6464
}
6565

66-
func (sf *jsonProgressFormatter) formatStatus(id, format string, a ...any) []byte {
66+
func (*jsonProgressFormatter) formatStatus(id, format string, a ...any) []byte {
6767
return FormatStatus(id, format, a...)
6868
}
6969

7070
// formatProgress formats the progress information for a specified action.
71-
func (sf *jsonProgressFormatter) formatProgress(id, action string, progress *jsonstream.Progress, aux any) []byte {
72-
if progress == nil {
73-
progress = &jsonstream.Progress{}
71+
func (*jsonProgressFormatter) formatProgress(id, action string, p *jsonstream.Progress, aux any) []byte {
72+
if p == nil {
73+
p = &jsonstream.Progress{}
7474
}
7575
var auxJSON *json.RawMessage
7676
if aux != nil {
@@ -83,7 +83,7 @@ func (sf *jsonProgressFormatter) formatProgress(id, action string, progress *jso
8383
}
8484
b, err := json.Marshal(&jsonMessage{
8585
Status: action,
86-
Progress: progress,
86+
Progress: p,
8787
ID: id,
8888
Aux: auxJSON,
8989
})
@@ -95,7 +95,7 @@ func (sf *jsonProgressFormatter) formatProgress(id, action string, progress *jso
9595

9696
type rawProgressFormatter struct{}
9797

98-
func (sf *rawProgressFormatter) formatStatus(id, format string, a ...any) []byte {
98+
func (*rawProgressFormatter) formatStatus(id, format string, a ...any) []byte {
9999
return []byte(fmt.Sprintf(format, a...) + streamNewline)
100100
}
101101

@@ -155,12 +155,12 @@ func rawProgressString(p *jsonstream.Progress) string {
155155
return pbBox + numbersBox + timeLeftBox
156156
}
157157

158-
func (sf *rawProgressFormatter) formatProgress(id, action string, progress *jsonstream.Progress, aux any) []byte {
159-
if progress == nil {
160-
progress = &jsonstream.Progress{}
158+
func (*rawProgressFormatter) formatProgress(id, action string, p *jsonstream.Progress, aux any) []byte {
159+
if p == nil {
160+
p = &jsonstream.Progress{}
161161
}
162162
endl := "\r"
163-
out := rawProgressString(progress)
163+
out := rawProgressString(p)
164164
if out == "" {
165165
endl += "\n"
166166
}
@@ -181,7 +181,7 @@ func NewJSONProgressOutput(out io.Writer, newLines bool) progress.Output {
181181

182182
type formatProgress interface {
183183
formatStatus(id, format string, a ...any) []byte
184-
formatProgress(id, action string, progress *jsonstream.Progress, aux any) []byte
184+
formatProgress(id, action string, p *jsonstream.Progress, aux any) []byte
185185
}
186186

187187
type progressOutput struct {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package streamformatter
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"strings"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/moby/moby/api/types/jsonstream"
12+
"gotest.tools/v3/assert"
13+
is "gotest.tools/v3/assert/cmp"
14+
)
15+
16+
func TestRawProgressFormatterFormatStatus(t *testing.T) {
17+
sf := rawProgressFormatter{}
18+
res := sf.formatStatus("ID", "%s%d", "a", 1)
19+
assert.Check(t, is.Equal("a1\r\n", string(res)))
20+
}
21+
22+
func TestRawProgressFormatterFormatProgress(t *testing.T) {
23+
sf := rawProgressFormatter{}
24+
jsonProgress := &jsonstream.Progress{
25+
Current: 15,
26+
Total: 30,
27+
Start: 1,
28+
}
29+
res := sf.formatProgress("id", "action", jsonProgress, nil)
30+
out := string(res)
31+
assert.Check(t, strings.HasPrefix(out, "action [===="))
32+
assert.Check(t, is.Contains(out, "15B/30B"))
33+
assert.Check(t, strings.HasSuffix(out, "\r"))
34+
}
35+
36+
func TestFormatStatus(t *testing.T) {
37+
res := FormatStatus("ID", "%s%d", "a", 1)
38+
expected := `{"status":"a1","id":"ID"}` + streamNewline
39+
assert.Check(t, is.Equal(expected, string(res)))
40+
}
41+
42+
func TestFormatError(t *testing.T) {
43+
res := FormatError(errors.New("Error for formatter"))
44+
expected := `{"errorDetail":{"message":"Error for formatter"},"error":"Error for formatter"}` + "\r\n"
45+
assert.Check(t, is.Equal(expected, string(res)))
46+
}
47+
48+
func TestFormatJSONError(t *testing.T) {
49+
err := &jsonstream.Error{Code: 50, Message: "Json error"}
50+
res := FormatError(err)
51+
expected := `{"errorDetail":{"code":50,"message":"Json error"},"error":"Json error"}` + streamNewline
52+
assert.Check(t, is.Equal(expected, string(res)))
53+
}
54+
55+
func TestJsonProgressFormatterFormatProgress(t *testing.T) {
56+
sf := &jsonProgressFormatter{}
57+
jsonProgress := &jsonstream.Progress{
58+
Current: 15,
59+
Total: 30,
60+
Start: 1,
61+
}
62+
aux := "aux message"
63+
res := sf.formatProgress("id", "action", jsonProgress, aux)
64+
msg := &jsonMessage{}
65+
66+
assert.NilError(t, json.Unmarshal(res, msg))
67+
68+
rawAux := json.RawMessage(`"` + aux + `"`)
69+
expected := &jsonMessage{
70+
ID: "id",
71+
Status: "action",
72+
Aux: &rawAux,
73+
Progress: jsonProgress,
74+
}
75+
assert.DeepEqual(t, msg, expected, cmpJSONMessageOpt())
76+
}
77+
78+
func cmpJSONMessageOpt() cmp.Option {
79+
progressMessagePath := func(path cmp.Path) bool {
80+
return path.String() == "ProgressMessage"
81+
}
82+
return cmp.Options{
83+
// Ignore deprecated property that is a derivative of Progress
84+
cmp.FilterPath(progressMessagePath, cmp.Ignore()),
85+
}
86+
}
87+
88+
func TestJsonProgressFormatterFormatStatus(t *testing.T) {
89+
sf := jsonProgressFormatter{}
90+
res := sf.formatStatus("ID", "%s%d", "a", 1)
91+
assert.Check(t, is.Equal(`{"status":"a1","id":"ID"}`+streamNewline, string(res)))
92+
}
93+
94+
func TestNewJSONProgressOutput(t *testing.T) {
95+
b := bytes.Buffer{}
96+
b.Write(FormatStatus("id", "Downloading"))
97+
_ = NewJSONProgressOutput(&b, false)
98+
assert.Check(t, is.Equal(`{"status":"Downloading","id":"id"}`+streamNewline, b.String()))
99+
}
100+
101+
func TestAuxFormatterEmit(t *testing.T) {
102+
b := bytes.Buffer{}
103+
aux := &AuxFormatter{Writer: &b}
104+
sampleAux := &struct {
105+
Data string
106+
}{"Additional data"}
107+
err := aux.Emit("", sampleAux)
108+
assert.NilError(t, err)
109+
assert.Check(t, is.Equal(`{"aux":{"Data":"Additional data"}}`+streamNewline, b.String()))
110+
}

vendor/github.com/moby/moby/api/pkg/streamformatter/streamwriter.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

vendor/modules.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ github.com/moby/go-archive/tarheader
173173
github.com/moby/moby/api/pkg/authconfig
174174
github.com/moby/moby/api/pkg/progress
175175
github.com/moby/moby/api/pkg/stdcopy
176-
github.com/moby/moby/api/pkg/streamformatter
177176
github.com/moby/moby/api/types
178177
github.com/moby/moby/api/types/auxprogress
179178
github.com/moby/moby/api/types/blkiodev

0 commit comments

Comments
 (0)