From 052b4086b9b2d1994961f3f089a12be61c89448e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 12 Apr 2022 17:16:02 +0200 Subject: [PATCH] cli/command/image: remove use of docker/docker/pkg/urlutil pkg/urlutil (despite its poorly chosen name) is not really intended as a generic utility to handle URLs, and should only be used by the builder to handle (remote) build contexts. The `IsURL()` function only does a very rudimentary check for `http(s)://` prefixes, without any other validation, but due to its name may give incorrect expectations. As we're deprecating this package for uses other than for build-contexts, this patch replaces this instance of the utility for a local function. While changing, also cleaned up some intermediate variables, and made the logic slightly more descriptive. Signed-off-by: Sebastiaan van Stijn --- cli/command/image/import.go | 45 ++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/cli/command/image/import.go b/cli/command/image/import.go index 2d2b7efc61..b966a4b497 100644 --- a/cli/command/image/import.go +++ b/cli/command/image/import.go @@ -2,15 +2,14 @@ package image import ( "context" - "io" "os" + "strings" "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" dockeropts "github.com/docker/cli/opts" "github.com/docker/docker/api/types" "github.com/docker/docker/pkg/jsonmessage" - "github.com/docker/docker/pkg/urlutil" "github.com/spf13/cobra" ) @@ -50,37 +49,37 @@ func NewImportCommand(dockerCli command.Cli) *cobra.Command { } func runImport(dockerCli command.Cli, options importOptions) error { - var ( - in io.Reader - srcName = options.source - ) - - if options.source == "-" { - in = dockerCli.In() - } else if !urlutil.IsURL(options.source) { - srcName = "-" + var source types.ImageImportSource + switch { + case options.source == "-": + // import from STDIN + source = types.ImageImportSource{ + Source: dockerCli.In(), + SourceName: options.source, + } + case strings.HasPrefix(options.source, "https://"), strings.HasPrefix(options.source, "http://"): + // import from a remote source (handled by the daemon) + source = types.ImageImportSource{ + SourceName: options.source, + } + default: + // import from a local file file, err := os.Open(options.source) if err != nil { return err } defer file.Close() - in = file + source = types.ImageImportSource{ + Source: file, + SourceName: "-", + } } - source := types.ImageImportSource{ - Source: in, - SourceName: srcName, - } - - importOptions := types.ImageImportOptions{ + responseBody, err := dockerCli.Client().ImageImport(context.Background(), source, options.reference, types.ImageImportOptions{ Message: options.message, Changes: options.changes.GetAll(), Platform: options.platform, - } - - clnt := dockerCli.Client() - - responseBody, err := clnt.ImageImport(context.Background(), source, options.reference, importOptions) + }) if err != nil { return err }