Files
docker-cli/cli/command/image/import.go
Sebastiaan van Stijn 052b4086b9 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 <github@gone.nl>
2022-04-29 12:21:53 +02:00

90 lines
2.3 KiB
Go

package image
import (
"context"
"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/spf13/cobra"
)
type importOptions struct {
source string
reference string
changes dockeropts.ListOpts
message string
platform string
}
// NewImportCommand creates a new `docker import` command
func NewImportCommand(dockerCli command.Cli) *cobra.Command {
var options importOptions
cmd := &cobra.Command{
Use: "import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]",
Short: "Import the contents from a tarball to create a filesystem image",
Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
options.source = args[0]
if len(args) > 1 {
options.reference = args[1]
}
return runImport(dockerCli, options)
},
}
flags := cmd.Flags()
options.changes = dockeropts.NewListOpts(nil)
flags.VarP(&options.changes, "change", "c", "Apply Dockerfile instruction to the created image")
flags.StringVarP(&options.message, "message", "m", "", "Set commit message for imported image")
command.AddPlatformFlag(flags, &options.platform)
return cmd
}
func runImport(dockerCli command.Cli, options importOptions) error {
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()
source = types.ImageImportSource{
Source: file,
SourceName: "-",
}
}
responseBody, err := dockerCli.Client().ImageImport(context.Background(), source, options.reference, types.ImageImportOptions{
Message: options.message,
Changes: options.changes.GetAll(),
Platform: options.platform,
})
if err != nil {
return err
}
defer responseBody.Close()
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
}