Skip to content

Commit 90f2f30

Browse files
authored
Merge pull request #7030 from thaJeztah/refactor_handleAux
cli/command/image: handleAux: avoid using global var
2 parents b50c369 + a11beec commit 90f2f30

2 files changed

Lines changed: 7 additions & 11 deletions

File tree

cli/command/image/push.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ To push the complete multi-platform image, remove the --platform flag.
123123
return err
124124
}
125125

126+
var notes []string
126127
defer func() {
127128
_ = responseBody.Close()
128129
for _, note := range notes {
@@ -131,18 +132,16 @@ To push the complete multi-platform image, remove the --platform flag.
131132
}()
132133

133134
if opts.quiet {
134-
err = jsonstream.Display(ctx, responseBody, streams.NewOut(io.Discard), jsonstream.WithAuxCallback(handleAux(out)))
135+
err = jsonstream.Display(ctx, responseBody, streams.NewOut(io.Discard), jsonstream.WithAuxCallback(handleAux(&notes, out)))
135136
if err == nil {
136137
_, _ = fmt.Fprintln(dockerCli.Out(), ref.String())
137138
}
138139
return err
139140
}
140-
return jsonstream.Display(ctx, responseBody, dockerCli.Out(), jsonstream.WithAuxCallback(handleAux(out)))
141+
return jsonstream.Display(ctx, responseBody, dockerCli.Out(), jsonstream.WithAuxCallback(handleAux(&notes, out)))
141142
}
142143

143-
var notes []string
144-
145-
func handleAux(out tui.Output) func(jm jsonstream.JSONMessage) {
144+
func handleAux(notes *[]string, out tui.Output) func(jm jsonstream.JSONMessage) {
146145
return func(jm jsonstream.JSONMessage) {
147146
b := []byte(*jm.Aux)
148147

@@ -153,7 +152,7 @@ func handleAux(out tui.Output) func(jm jsonstream.JSONMessage) {
153152
out.Color(aec.RedF).Apply(stripped.OriginalIndex.Digest.String()),
154153
out.Color(aec.GreenF).Apply(stripped.SelectedManifest.Digest.String()),
155154
)
156-
notes = append(notes, note)
155+
*notes = append(*notes, note)
157156
}
158157

159158
var missing auxprogress.ContentMissing
@@ -166,7 +165,7 @@ func handleAux(out tui.Output) func(jm jsonstream.JSONMessage) {
166165
Make sure you have all the referenced content and try again.
167166
168167
You can also push only a single platform specific manifest directly by specifying the platform you want to push with the --platform flag.`
169-
notes = append(notes, note)
168+
*notes = append(*notes, note)
170169
}
171170
}
172171
}

cli/command/image/push_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package image
22

33
import (
44
"bytes"
5-
"context"
65
"encoding/json"
76
"errors"
87
"io"
@@ -109,10 +108,8 @@ func TestRunPushRespectsNoColorForAuxNotes(t *testing.T) {
109108
},
110109
})
111110
cli.Out().SetIsTerminal(true)
112-
notes = nil
113-
t.Cleanup(func() { notes = nil })
114111

115-
err := runPush(context.Background(), cli, pushOptions{remote: "image:tag"})
112+
err := runPush(t.Context(), cli, pushOptions{remote: "image:tag"})
116113
assert.NilError(t, err)
117114

118115
out := cli.OutBuffer().String()

0 commit comments

Comments
 (0)