Skip to content

Commit 79e4e24

Browse files
committed
Add image load quiet mode
Signed-off-by: Austin Vazquez <[email protected]>
1 parent 62a8286 commit 79e4e24

File tree

7 files changed

+53
-11
lines changed

7 files changed

+53
-11
lines changed

cmd/nerdctl/container/container_create.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,17 @@ func processContainerCreateOptions(cmd *cobra.Command) (types.ContainerCreateOpt
409409
if err != nil {
410410
return opt, err
411411
}
412+
quiet, err := cmd.Flags().GetBool("quiet")
413+
if err != nil {
414+
return opt, err
415+
}
412416
opt.ImagePullOpt = types.ImagePullOptions{
413417
GOptions: opt.GOptions,
414418
VerifyOptions: imageVerifyOpt,
415419
IPFSAddress: opt.IPFSAddress,
416420
Stdout: opt.Stdout,
417421
Stderr: opt.Stderr,
422+
Quiet: quiet,
418423
}
419424
// #endregion
420425

cmd/nerdctl/container/container_run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func setCreateFlags(cmd *cobra.Command) {
9292
})
9393
cmd.Flags().Bool("rm", false, "Automatically remove the container when it exits")
9494
cmd.Flags().String("pull", "missing", `Pull image before running ("always"|"missing"|"never")`)
95+
cmd.Flags().BoolP("quiet", "q", false, "Suppress the pull output")
9596
cmd.RegisterFlagCompletionFunc("pull", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
9697
return []string{"always", "missing", "never"}, cobra.ShellCompDirectiveNoFileComp
9798
})

cmd/nerdctl/image/image_load.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func NewLoadCommand() *cobra.Command {
3838
}
3939

4040
loadCommand.Flags().StringP("input", "i", "", "Read from tar archive file, instead of STDIN")
41+
loadCommand.Flags().BoolP("quiet", "q", false, "Suppress the load output")
4142

4243
// #region platform flags
4344
// platform is defined as StringSlice, not StringArray, to allow specifying "--platform=amd64,arm64"
@@ -66,13 +67,18 @@ func processLoadCommandFlags(cmd *cobra.Command) (types.ImageLoadOptions, error)
6667
if err != nil {
6768
return types.ImageLoadOptions{}, err
6869
}
70+
quiet, err := cmd.Flags().GetBool("quiet")
71+
if err != nil {
72+
return types.ImageLoadOptions{}, err
73+
}
6974
return types.ImageLoadOptions{
7075
GOptions: globalOptions,
7176
Input: input,
7277
Platform: platform,
7378
AllPlatforms: allPlatforms,
7479
Stdout: cmd.OutOrStdout(),
7580
Stdin: cmd.InOrStdin(),
81+
Quiet: quiet,
7682
}, nil
7783
}
7884

cmd/nerdctl/image/image_load_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,33 @@ func TestLoadStdinEmpty(t *testing.T) {
7979

8080
testCase.Run(t)
8181
}
82+
83+
func TestLoadQuiet(t *testing.T) {
84+
nerdtest.Setup()
85+
86+
testCase := &test.Case{
87+
Description: "TestLoadQuiet",
88+
Setup: func(data test.Data, helpers test.Helpers) {
89+
helpers.Ensure("pull", testutil.CommonImage)
90+
helpers.Ensure("tag", testutil.CommonImage, data.Identifier())
91+
helpers.Ensure("save", data.Identifier(), "-o", filepath.Join(data.TempDir(), "common.tar"))
92+
helpers.Ensure("rmi", "-f", data.Identifier())
93+
},
94+
Cleanup: func(data test.Data, helpers test.Helpers) {
95+
helpers.Anyhow("rmi", data.Identifier())
96+
},
97+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
98+
return helpers.Command("load", "--quiet", "--input", filepath.Join(data.TempDir(), "common.tar"))
99+
},
100+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
101+
return &test.Expected{
102+
Output: test.All(
103+
test.Contains(fmt.Sprintf("Loaded image: %s:latest", data.Identifier())),
104+
test.DoesNotContain("Loading layer"),
105+
),
106+
}
107+
},
108+
}
109+
110+
testCase.Run(t)
111+
}

docs/command-reference.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ Basic flags:
151151
- :whale: `--rm`: Automatically remove the container when it exits
152152
- :whale: `--pull=(always|missing|never)`: Pull image before running
153153
- Default: "missing"
154+
- :whale: `-q, --quiet`: Suppress the pull output
154155
- :whale: `--pid=(host|container:<container>)`: PID namespace to use
155156
- :whale: `--uts=(host)` : UTS namespace to use
156157
- :whale: `--stop-signal`: Signal to stop a container (default "SIGTERM")
@@ -814,11 +815,10 @@ Usage: `nerdctl load [OPTIONS]`
814815
Flags:
815816

816817
- :whale: `-i, --input`: Read from tar archive file, instead of STDIN
818+
- :whale: `-q, --quiet`: Suppress the load output
817819
- :nerd_face: `--platform=(amd64|arm64|...)`: Import content for a specific platform
818820
- :nerd_face: `--all-platforms`: Import content for all platforms
819821

820-
Unimplemented `docker load` flags: `--quiet`
821-
822822
### :whale: nerdctl save
823823

824824
Save one or more images to a tar archive (streamed to STDOUT by default)

pkg/api/types/load_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ type ImageLoadOptions struct {
2929
Platform []string
3030
// AllPlatforms import content for all platforms
3131
AllPlatforms bool
32+
// Quiet suppresses the load output.
33+
Quiet bool
3234
}

pkg/cmd/image/load.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ func Load(ctx context.Context, client *containerd.Client, options types.ImageLoa
7373
if err != nil {
7474
return err
7575
}
76-
return loadImage(ctx, client, decompressor, platMC, false, options)
76+
return loadImage(ctx, client, decompressor, platMC, options)
7777
}
7878

79-
func loadImage(ctx context.Context, client *containerd.Client, in io.Reader, platMC platforms.MatchComparer, quiet bool, options types.ImageLoadOptions) error {
79+
func loadImage(ctx context.Context, client *containerd.Client, in io.Reader, platMC platforms.MatchComparer, options types.ImageLoadOptions) error {
8080
// In addition to passing WithImagePlatform() to client.Import(), we also need to pass WithDefaultPlatform() to NewClient().
8181
// Otherwise unpacking may fail.
8282
r := &readCounter{Reader: in}
@@ -95,19 +95,17 @@ func loadImage(ctx context.Context, client *containerd.Client, in io.Reader, pla
9595
image := containerd.NewImageWithPlatform(client, img, platMC)
9696

9797
// TODO: Show unpack status
98-
if !quiet {
98+
if !options.Quiet {
9999
fmt.Fprintf(options.Stdout, "unpacking %s (%s)...\n", img.Name, img.Target.Digest)
100100
}
101101
err = image.Unpack(ctx, options.GOptions.Snapshotter)
102102
if err != nil {
103103
return err
104104
}
105-
if quiet {
106-
fmt.Fprintln(options.Stdout, img.Target.Digest)
107-
} else {
108-
repo, tag := imgutil.ParseRepoTag(img.Name)
109-
fmt.Fprintf(options.Stdout, "Loaded image: %s:%s\n", repo, tag)
110-
}
105+
106+
// Loaded message is shown even when quiet.
107+
repo, tag := imgutil.ParseRepoTag(img.Name)
108+
fmt.Fprintf(options.Stdout, "Loaded image: %s:%s\n", repo, tag)
111109
}
112110

113111
return nil

0 commit comments

Comments
 (0)