From 9470efa4387e8d3a15f2d5c1a00a7fb1ccd93462 Mon Sep 17 00:00:00 2001 From: "Hsing-Yu (David) Chen" Date: Thu, 19 Jan 2023 11:24:26 -0800 Subject: [PATCH] refactor: pass an io.Writer to image.List Signed-off-by: Hsing-Yu (David) Chen --- cmd/nerdctl/images.go | 5 +++-- pkg/api/types/image_types.go | 3 ++- pkg/cmd/image/list.go | 8 +++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/nerdctl/images.go b/cmd/nerdctl/images.go index 649b28f9b0b..21d7120c6be 100644 --- a/cmd/nerdctl/images.go +++ b/cmd/nerdctl/images.go @@ -66,7 +66,7 @@ Properties: return imagesCommand } -func processImagesFlag(cmd *cobra.Command, args []string) (types.ImageListOptions, error) { +func processImageListOptions(cmd *cobra.Command, args []string) (types.ImageListOptions, error) { globalOptions, err := processRootCmdFlags(cmd) if err != nil { return types.ImageListOptions{}, err @@ -118,12 +118,13 @@ func processImagesFlag(cmd *cobra.Command, args []string) (types.ImageListOption Digests: digests, Names: names, All: true, + Stdout: cmd.OutOrStdout(), }, nil } func imagesAction(cmd *cobra.Command, args []string) error { - options, err := processImagesFlag(cmd, args) + options, err := processImageListOptions(cmd, args) if err != nil { return err } diff --git a/pkg/api/types/image_types.go b/pkg/api/types/image_types.go index 00ea418ab1e..c76fd7aba05 100644 --- a/pkg/api/types/image_types.go +++ b/pkg/api/types/image_types.go @@ -18,8 +18,9 @@ package types import "io" -// ImageListOptions specifies options for `nerdctl image ls`. +// ImageListOptions specifies options for `nerdctl image list`. type ImageListOptions struct { + Stdout io.Writer // GOptions is the global options GOptions GlobalCommandOptions // Quiet only show numeric IDs diff --git a/pkg/cmd/image/list.go b/pkg/cmd/image/list.go index 1ab37d90f2c..3a721060ffd 100644 --- a/pkg/cmd/image/list.go +++ b/pkg/cmd/image/list.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "io" - "os" "regexp" "strings" "text/tabwriter" @@ -95,7 +94,7 @@ func List(ctx context.Context, options types.ImageListOptions) error { imageList = imgutil.FilterImages(imageList, beforeImages, sinceImages) } - return printImages(ctx, options, client, imageList) + return printImages(ctx, client, imageList, options) } func filterByReference(imageList []images.Image, filters []string) ([]images.Image, error) { @@ -181,10 +180,9 @@ type imagePrintable struct { Platform string // nerdctl extension } -func printImages(ctx context.Context, options types.ImageListOptions, client *containerd.Client, imageList []images.Image) error { +func printImages(ctx context.Context, client *containerd.Client, imageList []images.Image, options types.ImageListOptions) error { + w := options.Stdout digestsFlag := options.Digests - var w io.Writer - w = os.Stdout if options.Format == "wide" { digestsFlag = true }