Skip to content

Commit

Permalink
Merge pull request #3773 from yankay/fix-pull-image-process-to-stderr2
Browse files Browse the repository at this point in the history
Fix pull image process output from stderr to stdout
  • Loading branch information
AkihiroSuda authored Dec 20, 2024
2 parents c41b394 + 3dbd24c commit c5b57fa
Show file tree
Hide file tree
Showing 4 changed files with 44 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
33 changes: 33 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,36 @@ func TestImagePullSoci(t *testing.T) {

testCase.Run(t)
}

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

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

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 c5b57fa

Please sign in to comment.