From 77e312e408053e483c4b17e40dd12c6cf1115f8f Mon Sep 17 00:00:00 2001 From: fengxsong Date: Mon, 19 Jun 2023 14:07:20 +0800 Subject: [PATCH] feat: support override platform in diff command (#3376) Signed-off-by: fengxsong --- pkg/buildah/diff.go | 4 +++- pkg/buildah/interface.go | 3 ++- pkg/buildah/runtime.go | 14 +++++++------- pkg/buildah/util.go | 4 +++- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/buildah/diff.go b/pkg/buildah/diff.go index 26bd627e2f0..a85cde3496e 100644 --- a/pkg/buildah/diff.go +++ b/pkg/buildah/diff.go @@ -20,6 +20,7 @@ import ( "io" "os" + "github.com/containers/common/libimage" "github.com/containers/storage/pkg/archive" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -117,6 +118,7 @@ func newDiffCommand() *cobra.Command { rootCmd.CommandPath()), } opts.RegisterFlags(cmd.Flags()) + cmd.Flags().AddFlagSet(getPlatformFlags()) cmd.SetUsageTemplate(UsageTemplate()) return cmd } @@ -159,7 +161,7 @@ func runDiff(c *cobra.Command, args []string, opts *diffOption) error { ctx := getContext() if diffType == DiffImage { - if args, err = r.pullOrLoadImages(ctx, args...); err != nil { + if args, err = r.PullOrLoadImages(ctx, args, libimage.CopyOptions{}); err != nil { return err } } diff --git a/pkg/buildah/interface.go b/pkg/buildah/interface.go index c28c687fddf..fb502d50125 100644 --- a/pkg/buildah/interface.go +++ b/pkg/buildah/interface.go @@ -20,6 +20,7 @@ import ( "github.com/containers/buildah" "github.com/containers/buildah/pkg/parse" + "github.com/containers/common/libimage" "github.com/containers/image/v5/transports" "github.com/containers/image/v5/types" "github.com/containers/storage" @@ -235,7 +236,7 @@ func (impl *realImpl) ListContainers() ([]JSONContainer, error) { func (impl *realImpl) Load(input string, transport string) (string, error) { ref := FormatReferenceWithTransportName(transport, input) - names, err := impl.runtime.pullOrLoadImages(getContext(), ref) + names, err := impl.runtime.PullOrLoadImages(getContext(), []string{ref}, libimage.CopyOptions{}) if err != nil { return "", err } diff --git a/pkg/buildah/runtime.go b/pkg/buildah/runtime.go index a71107b2e12..2cbf5338c65 100644 --- a/pkg/buildah/runtime.go +++ b/pkg/buildah/runtime.go @@ -89,7 +89,11 @@ func (r *Runtime) getLayerID(id string, diffType DiffType) (string, error) { return "", fmt.Errorf("%s not found: %w", id, lastErr) } -func (r *Runtime) pullOrLoadImages(ctx context.Context, args ...string) ([]string, error) { +func (r *Runtime) PullOrLoadImages(ctx context.Context, args []string, options libimage.CopyOptions) ([]string, error) { + copyOpts := options + if copyOpts.Writer == nil { + copyOpts.Writer = os.Stderr + } var result []string for i := range args { name := args[i] @@ -103,9 +107,7 @@ func (r *Runtime) pullOrLoadImages(ctx context.Context, args ...string) ([]strin ref = strings.TrimPrefix(ref, "//") } pullImages, err := r.Runtime.Pull(ctx, ref, config.PullPolicyMissing, &libimage.PullOptions{ - CopyOptions: libimage.CopyOptions{ - Writer: os.Stderr, - }, + CopyOptions: copyOpts, }) if err != nil { return nil, err @@ -123,9 +125,7 @@ func (r *Runtime) pullOrLoadImages(ctx context.Context, args ...string) ([]strin ref = filepath.Join(cwd, ref) } images, err := r.Runtime.Load(ctx, ref, &libimage.LoadOptions{ - CopyOptions: libimage.CopyOptions{ - Writer: os.Stderr, - }, + CopyOptions: copyOpts, }) if err != nil { return nil, err diff --git a/pkg/buildah/util.go b/pkg/buildah/util.go index bd5df6673d0..2832e74376f 100644 --- a/pkg/buildah/util.go +++ b/pkg/buildah/util.go @@ -15,6 +15,8 @@ package buildah import ( + "github.com/containers/common/libimage" + "github.com/labring/sealos/pkg/utils/file" ) @@ -27,7 +29,7 @@ func PreloadIfTarFile(images []string, transport string) ([]string, error) { for i := range images { if file.IsTarFile(images[i]) { ref := FormatReferenceWithTransportName(transport, images[i]) - names, err := r.pullOrLoadImages(getContext(), ref) + names, err := r.PullOrLoadImages(getContext(), []string{ref}, libimage.CopyOptions{}) if err != nil { return nil, err }