Skip to content

Commit 0d50b35

Browse files
committed
image: honor NO_COLOR in push notes
Signed-off-by: popsiclelmlm <no_7seven@163.com>
1 parent 4e815a7 commit 0d50b35

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

cli/command/image/push.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,27 @@ To push the complete multi-platform image, remove the --platform flag.
131131
}()
132132

133133
if opts.quiet {
134-
err = jsonstream.Display(ctx, responseBody, streams.NewOut(io.Discard), jsonstream.WithAuxCallback(handleAux()))
134+
err = jsonstream.Display(ctx, responseBody, streams.NewOut(io.Discard), jsonstream.WithAuxCallback(handleAux(out)))
135135
if err == nil {
136136
_, _ = fmt.Fprintln(dockerCli.Out(), ref.String())
137137
}
138138
return err
139139
}
140-
return jsonstream.Display(ctx, responseBody, dockerCli.Out(), jsonstream.WithAuxCallback(handleAux()))
140+
return jsonstream.Display(ctx, responseBody, dockerCli.Out(), jsonstream.WithAuxCallback(handleAux(out)))
141141
}
142142

143143
var notes []string
144144

145-
func handleAux() func(jm jsonstream.JSONMessage) {
145+
func handleAux(out tui.Output) func(jm jsonstream.JSONMessage) {
146146
return func(jm jsonstream.JSONMessage) {
147147
b := []byte(*jm.Aux)
148148

149149
var stripped auxprogress.ManifestPushedInsteadOfIndex
150150
err := json.Unmarshal(b, &stripped)
151151
if err == nil && stripped.ManifestPushedInsteadOfIndex {
152152
note := fmt.Sprintf("Not all multiplatform-content is present and only the available single-platform image was pushed\n%s -> %s",
153-
aec.RedF.Apply(stripped.OriginalIndex.Digest.String()),
154-
aec.GreenF.Apply(stripped.SelectedManifest.Digest.String()),
153+
out.Color(aec.RedF).Apply(stripped.OriginalIndex.Digest.String()),
154+
out.Color(aec.GreenF).Apply(stripped.SelectedManifest.Digest.String()),
155155
)
156156
notes = append(notes, note)
157157
}

cli/command/image/push_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package image
22

33
import (
4+
"context"
45
"errors"
56
"io"
67
"net/http"
8+
"strings"
79
"testing"
810

911
"github.com/docker/cli/internal/test"
@@ -86,3 +88,58 @@ func TestNewPushCommandSuccess(t *testing.T) {
8688
})
8789
}
8890
}
91+
92+
func TestRunPushManifestNoteHonorsNoColor(t *testing.T) {
93+
oldNotes := notes
94+
t.Cleanup(func() {
95+
notes = oldNotes
96+
})
97+
98+
const (
99+
originalDigest = "sha256:1111111111111111111111111111111111111111111111111111111111111111"
100+
selectedDigest = "sha256:2222222222222222222222222222222222222222222222222222222222222222"
101+
)
102+
response := `{"aux":{"manifestPushedInsteadOfIndex":true,"originalIndex":{"digest":"` + originalDigest + `"},"selectedManifest":{"digest":"` + selectedDigest + `"}}}` + "\n"
103+
104+
testCases := []struct {
105+
name string
106+
noColor bool
107+
hasANSI bool
108+
}{
109+
{
110+
name: "terminal",
111+
hasANSI: true,
112+
},
113+
{
114+
name: "no-color",
115+
noColor: true,
116+
hasANSI: false,
117+
},
118+
}
119+
120+
for _, tc := range testCases {
121+
t.Run(tc.name, func(t *testing.T) {
122+
notes = nil
123+
if tc.noColor {
124+
t.Setenv("NO_COLOR", "1")
125+
} else {
126+
t.Setenv("NO_COLOR", "")
127+
}
128+
129+
cli := test.NewFakeCli(&fakeClient{
130+
imagePushFunc: func(ref string, options client.ImagePushOptions) (client.ImagePushResponse, error) {
131+
return fakeStreamResult{ReadCloser: io.NopCloser(strings.NewReader(response))}, nil
132+
},
133+
})
134+
cli.Out().SetIsTerminal(true)
135+
136+
assert.NilError(t, runPush(context.Background(), cli, pushOptions{remote: "example.com/image:tag"}))
137+
138+
out := cli.OutBuffer().String()
139+
assert.Check(t, strings.Contains(out, "Not all multiplatform-content is present"))
140+
assert.Check(t, strings.Contains(out, originalDigest))
141+
assert.Check(t, strings.Contains(out, selectedDigest))
142+
assert.Equal(t, tc.hasANSI, strings.Contains(out, "\x1b["))
143+
})
144+
}
145+
}

0 commit comments

Comments
 (0)