Skip to content

Commit c238428

Browse files
committed
image/push: respect NO_COLOR in aux notes
Signed-off-by: WilliamK112 <164879897+WilliamK112@users.noreply.github.com>
1 parent 0b459a4 commit c238428

2 files changed

Lines changed: 38 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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package image
22

33
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
47
"errors"
58
"io"
69
"net/http"
10+
"strings"
711
"testing"
812

913
"github.com/docker/cli/internal/test"
14+
"github.com/moby/moby/api/types/auxprogress"
1015
"github.com/moby/moby/client"
16+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
1117
"gotest.tools/v3/assert"
1218
)
1319

@@ -86,3 +92,30 @@ func TestNewPushCommandSuccess(t *testing.T) {
8692
})
8793
}
8894
}
95+
96+
func TestRunPushRespectsNoColorForAuxNotes(t *testing.T) {
97+
t.Setenv("NO_COLOR", "1")
98+
cli := test.NewFakeCli(&fakeClient{
99+
imagePushFunc: func(ref string, options client.ImagePushOptions) (client.ImagePushResponse, error) {
100+
aux, err := json.Marshal(auxprogress.ManifestPushedInsteadOfIndex{
101+
ManifestPushedInsteadOfIndex: true,
102+
OriginalIndex: ocispec.Descriptor{Digest: "sha256:1111111111111111111111111111111111111111111111111111111111111111"},
103+
SelectedManifest: ocispec.Descriptor{Digest: "sha256:2222222222222222222222222222222222222222222222222222222222222222"},
104+
})
105+
assert.NilError(t, err)
106+
line := append([]byte(`{"aux":`), aux...)
107+
line = append(line, '}', '\n')
108+
return fakeStreamResult{ReadCloser: io.NopCloser(bytes.NewReader(line))}, nil
109+
},
110+
})
111+
cli.Out().SetIsTerminal(true)
112+
notes = nil
113+
t.Cleanup(func() { notes = nil })
114+
115+
err := runPush(context.Background(), cli, pushOptions{remote: "image:tag"})
116+
assert.NilError(t, err)
117+
118+
out := cli.OutBuffer().String()
119+
assert.Assert(t, strings.Contains(out, "sha256:1111111111111111111111111111111111111111111111111111111111111111 -> sha256:2222222222222222222222222222222222222222222222222222222222222222"))
120+
assert.Assert(t, !strings.Contains(out, "\x1b["), "output should not contain ANSI escape codes, output: %s", out)
121+
}

0 commit comments

Comments
 (0)