2016-09-08 13:11:39 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
"os"
|
2022-04-12 11:16:02 -04:00
|
|
|
"strings"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2017-05-15 08:45:19 -04:00
|
|
|
dockeropts "github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"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
|
2018-09-13 13:56:01 -04:00
|
|
|
platform string
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewImportCommand creates a new `docker import` command
|
2017-03-30 20:21:14 -04:00
|
|
|
func NewImportCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
var options importOptions
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
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 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.source = args[0]
|
2016-09-08 13:11:39 -04:00
|
|
|
if len(args) > 1 {
|
2017-05-15 08:45:19 -04:00
|
|
|
options.reference = args[1]
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-15 08:45:19 -04:00
|
|
|
return runImport(dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
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")
|
2018-09-13 13:56:01 -04:00
|
|
|
command.AddPlatformFlag(flags, &options.platform)
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
func runImport(dockerCli command.Cli, options importOptions) error {
|
2022-04-12 11:16:02 -04:00
|
|
|
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
|
2017-05-15 08:45:19 -04:00
|
|
|
file, err := os.Open(options.source)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2022-04-12 11:16:02 -04:00
|
|
|
source = types.ImageImportSource{
|
|
|
|
Source: file,
|
|
|
|
SourceName: "-",
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2022-04-12 11:16:02 -04:00
|
|
|
responseBody, err := dockerCli.Client().ImageImport(context.Background(), source, options.reference, types.ImageImportOptions{
|
2018-09-13 13:56:01 -04:00
|
|
|
Message: options.message,
|
|
|
|
Changes: options.changes.GetAll(),
|
|
|
|
Platform: options.platform,
|
2022-04-12 11:16:02 -04:00
|
|
|
})
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
|
|
|
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
|
|
|
|
}
|