2016-06-06 07:58:23 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-06-06 07:58:23 -04:00
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-08-31 18:48:53 -04:00
|
|
|
dockeropts "github.com/docker/docker/opts"
|
2016-06-06 07:58:23 -04:00
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
"github.com/docker/docker/pkg/urlutil"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type importOptions struct {
|
|
|
|
source string
|
|
|
|
reference string
|
2016-08-31 18:48:53 -04:00
|
|
|
changes dockeropts.ListOpts
|
2016-06-06 07:58:23 -04:00
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewImportCommand creates a new `docker import` command
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewImportCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-06 07:58:23 -04:00
|
|
|
var opts 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 {
|
|
|
|
opts.source = args[0]
|
|
|
|
if len(args) > 1 {
|
|
|
|
opts.reference = args[1]
|
|
|
|
}
|
|
|
|
return runImport(dockerCli, opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
|
|
|
|
2016-08-31 18:48:53 -04:00
|
|
|
opts.changes = dockeropts.NewListOpts(nil)
|
|
|
|
flags.VarP(&opts.changes, "change", "c", "Apply Dockerfile instruction to the created image")
|
2016-06-06 07:58:23 -04:00
|
|
|
flags.StringVarP(&opts.message, "message", "m", "", "Set commit message for imported image")
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runImport(dockerCli *command.DockerCli, opts importOptions) error {
|
2016-06-06 07:58:23 -04:00
|
|
|
var (
|
|
|
|
in io.Reader
|
|
|
|
srcName = opts.source
|
|
|
|
)
|
|
|
|
|
|
|
|
if opts.source == "-" {
|
|
|
|
in = dockerCli.In()
|
|
|
|
} else if !urlutil.IsURL(opts.source) {
|
|
|
|
srcName = "-"
|
|
|
|
file, err := os.Open(opts.source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
in = file
|
|
|
|
}
|
|
|
|
|
|
|
|
source := types.ImageImportSource{
|
|
|
|
Source: in,
|
|
|
|
SourceName: srcName,
|
|
|
|
}
|
|
|
|
|
|
|
|
options := types.ImageImportOptions{
|
|
|
|
Message: opts.message,
|
2016-08-31 18:48:53 -04:00
|
|
|
Changes: opts.changes.GetAll(),
|
2016-06-06 07:58:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
clnt := dockerCli.Client()
|
|
|
|
|
|
|
|
responseBody, err := clnt.ImageImport(context.Background(), source, opts.reference, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer responseBody.Close()
|
|
|
|
|
2016-08-29 11:11:29 -04:00
|
|
|
return jsonmessage.DisplayJSONMessagesToStream(responseBody, dockerCli.Out(), nil)
|
2016-06-06 07:58:23 -04:00
|
|
|
}
|