From 90c50338865930a1086ef0a46d7f67384a67a775 Mon Sep 17 00:00:00 2001 From: Erol Keskin Date: Thu, 8 Sep 2022 02:11:08 +0300 Subject: [PATCH] warn users about --dest-compress and --dest-decompress misuse Signed-off-by: Erol Keskin --- cmd/skopeo/copy.go | 10 +--------- cmd/skopeo/utils.go | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/skopeo/copy.go b/cmd/skopeo/copy.go index e6cafaef..5a1c3565 100644 --- a/cmd/skopeo/copy.go +++ b/cmd/skopeo/copy.go @@ -17,7 +17,6 @@ import ( "github.com/containers/image/v5/transports/alltransports" encconfig "github.com/containers/ocicrypt/config" enchelpers "github.com/containers/ocicrypt/helpers" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -261,14 +260,7 @@ func (opts *copyOptions) run(args []string, stdout io.Writer) (retErr error) { } } - if destRef.Transport().Name() != "dir" { - if opts.destImage.dirForceCompression { - logrus.Warn("--dest-compress can only be used if the destination transport is 'dir'") - } - if opts.destImage.dirForceDecompression { - logrus.Warn("--dest-decompress can only be used if the destination transport is 'dir'") - } - } + opts.destImage.warnAboutIneffectiveOptions(destRef.Transport()) return retry.IfNecessary(ctx, func() error { manifestBytes, err := copy.Image(ctx, policyContext, destRef, srcRef, ©.Options{ diff --git a/cmd/skopeo/utils.go b/cmd/skopeo/utils.go index 6073bdc6..8d0fe8ae 100644 --- a/cmd/skopeo/utils.go +++ b/cmd/skopeo/utils.go @@ -10,6 +10,7 @@ import ( commonFlag "github.com/containers/common/pkg/flag" "github.com/containers/common/pkg/retry" + "github.com/containers/image/v5/directory" "github.com/containers/image/v5/manifest" "github.com/containers/image/v5/pkg/compression" "github.com/containers/image/v5/transports/alltransports" @@ -252,12 +253,13 @@ type imageDestOptions struct { compressionFormat string // Format to use for the compression compressionLevel commonFlag.OptionalInt // Level to use for the compression precomputeDigests bool // Precompute digests to dedup layers when saving to the docker: transport + imageDestFlagPrefix string } // imageDestFlags prepares a collection of CLI flags writing into imageDestOptions, and the managed imageDestOptions structure. func imageDestFlags(global *globalOptions, shared *sharedImageOptions, deprecatedTLSVerify *deprecatedTLSVerifyOption, flagPrefix, credsOptionAlias string) (pflag.FlagSet, *imageDestOptions) { genericFlags, genericOptions := imageFlags(global, shared, deprecatedTLSVerify, flagPrefix, credsOptionAlias) - opts := imageDestOptions{imageOptions: genericOptions} + opts := imageDestOptions{imageOptions: genericOptions, imageDestFlagPrefix: flagPrefix} fs := pflag.FlagSet{} fs.AddFlagSet(&genericFlags) fs.BoolVar(&opts.dirForceCompression, flagPrefix+"compress", false, "Compress tarball image layers when saving to directory using the 'dir' transport. (default is same compression type as source)") @@ -295,6 +297,17 @@ func (opts *imageDestOptions) newSystemContext() (*types.SystemContext, error) { return ctx, err } +func (opts *imageDestOptions) warnAboutIneffectiveOptions(destTransport types.ImageTransport) { + if destTransport.Name() != directory.Transport.Name() { + if opts.dirForceCompression { + logrus.Warnf("--%s can only be used if the destination transport is 'dir'", opts.imageDestFlagPrefix+"compress") + } + if opts.dirForceDecompression { + logrus.Warnf("--%s can only be used if the destination transport is 'dir'", opts.imageDestFlagPrefix+"decompress") + } + } +} + func parseCreds(creds string) (string, string, error) { if creds == "" { return "", "", errors.New("credentials can't be empty")