Skip to content

Commit

Permalink
fix pull image process output from stderr to stdout
Browse files Browse the repository at this point in the history
Signed-off-by: Kay Yan <[email protected]>
  • Loading branch information
yankay committed Dec 19, 2024
1 parent c41b394 commit 27ffb55
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/nerdctl/image/image_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ func processPullCommandFlags(cmd *cobra.Command) (types.ImagePullOptions, error)
RFlags: types.RemoteSnapshotterFlags{
SociIndexDigest: sociIndexDigest,
},
Stdout: cmd.OutOrStdout(),
Stderr: cmd.OutOrStderr(),
Stdout: cmd.OutOrStdout(),
Stderr: cmd.OutOrStderr(),
ProgressOutputToStdout: true,
}, nil
}

Expand Down
41 changes: 41 additions & 0 deletions cmd/nerdctl/image/image_pull_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,44 @@ func TestImagePullSoci(t *testing.T) {

testCase.Run(t)
}

func TestImagePullProcessOutput(t *testing.T) {
nerdtest.Setup()

testCase := &test.Case{
Require: test.Require(
test.Linux,
),
SubTests: []*test.Case{
{
Description: "Pull Image - output should be in stdout",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rmi", "-f", testutil.AlpineImage)
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("pull", testutil.AlpineImage)
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: test.Contains(testutil.AlpineImage)}
},
},
{
Description: "Run Container with image pull - output should be in stderr",
Setup: func(data test.Data, helpers test.Helpers) {
helpers.Anyhow("rmi", "-f", testutil.AlpineImage)
},
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
return helpers.Command("run", "--rm", testutil.AlpineImage)
},
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
return &test.Expected{
Output: test.DoesNotContain(testutil.AlpineImage),
}
},
},
},
}

testCase.Run(t)
}
7 changes: 5 additions & 2 deletions pkg/api/types/image_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,11 @@ type RemoteSnapshotterFlags struct {

// ImagePullOptions specifies options for `nerdctl (image) pull`.
type ImagePullOptions struct {
Stdout io.Writer
Stderr io.Writer
Stdout io.Writer
Stderr io.Writer
// ProgressOutputToStdout directs progress output to stdout instead of stderr
ProgressOutputToStdout bool

GOptions GlobalCommandOptions
VerifyOptions ImageVerifyOptions
// Unpack the image for the current single platform.
Expand Down
3 changes: 3 additions & 0 deletions pkg/imgutil/imgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ func PullImage(ctx context.Context, client *containerd.Client, resolver remotes.
}
if !options.Quiet {
config.ProgressOutput = options.Stderr
if options.ProgressOutputToStdout {
config.ProgressOutput = options.Stdout
}
}

// unpack(B) if given 1 platform unless specified by `unpack`
Expand Down

0 comments on commit 27ffb55

Please sign in to comment.