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
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
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/docker/docker/pkg/urlutil"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type importOptions struct {
|
|
|
|
source string
|
|
|
|
reference string
|
|
|
|
changes dockeropts.ListOpts
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
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 {
|
2016-09-08 13:11:39 -04:00
|
|
|
var (
|
|
|
|
in io.Reader
|
2017-05-15 08:45:19 -04:00
|
|
|
srcName = options.source
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
if options.source == "-" {
|
2016-09-08 13:11:39 -04:00
|
|
|
in = dockerCli.In()
|
2017-05-15 08:45:19 -04:00
|
|
|
} else if !urlutil.IsURL(options.source) {
|
2016-09-08 13:11:39 -04:00
|
|
|
srcName = "-"
|
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()
|
|
|
|
in = file
|
|
|
|
}
|
|
|
|
|
|
|
|
source := types.ImageImportSource{
|
|
|
|
Source: in,
|
|
|
|
SourceName: srcName,
|
|
|
|
}
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
importOptions := types.ImageImportOptions{
|
|
|
|
Message: options.message,
|
|
|
|
Changes: options.changes.GetAll(),
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
clnt := dockerCli.Client()
|
|
|
|
|
2017-05-15 08:45:19 -04:00
|
|
|
responseBody, err := clnt.ImageImport(context.Background(), source, options.reference, importOptions)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
|
|
|
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
|
|
|
|
}
|