Browse code

Migrate import command to cobra

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2016/06/06 20:58:23
Showing 5 changed files
... ...
@@ -11,7 +11,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
11 11
 		"exec":    cli.CmdExec,
12 12
 		"history": cli.CmdHistory,
13 13
 		"images":  cli.CmdImages,
14
-		"import":  cli.CmdImport,
15 14
 		"info":    cli.CmdInfo,
16 15
 		"inspect": cli.CmdInspect,
17 16
 		"kill":    cli.CmdKill,
18 17
new file mode 100644
... ...
@@ -0,0 +1,86 @@
0
+package image
1
+
2
+import (
3
+	"io"
4
+	"os"
5
+
6
+	"golang.org/x/net/context"
7
+
8
+	"github.com/docker/docker/api/client"
9
+	"github.com/docker/docker/cli"
10
+	"github.com/docker/docker/pkg/jsonmessage"
11
+	"github.com/docker/docker/pkg/urlutil"
12
+	"github.com/docker/engine-api/types"
13
+	"github.com/spf13/cobra"
14
+)
15
+
16
+type importOptions struct {
17
+	source    string
18
+	reference string
19
+	changes   []string
20
+	message   string
21
+}
22
+
23
+// NewImportCommand creates a new `docker import` command
24
+func NewImportCommand(dockerCli *client.DockerCli) *cobra.Command {
25
+	var opts importOptions
26
+
27
+	cmd := &cobra.Command{
28
+		Use:   "import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]",
29
+		Short: "Import the contents from a tarball to create a filesystem image",
30
+		Args:  cli.RequiresMinArgs(1),
31
+		RunE: func(cmd *cobra.Command, args []string) error {
32
+			opts.source = args[0]
33
+			if len(args) > 1 {
34
+				opts.reference = args[1]
35
+			}
36
+			return runImport(dockerCli, opts)
37
+		},
38
+	}
39
+
40
+	flags := cmd.Flags()
41
+
42
+	flags.StringSliceVarP(&opts.changes, "change", "c", []string{}, "Apply Dockerfile instruction to the created image")
43
+	flags.StringVarP(&opts.message, "message", "m", "", "Set commit message for imported image")
44
+
45
+	return cmd
46
+}
47
+
48
+func runImport(dockerCli *client.DockerCli, opts importOptions) error {
49
+	var (
50
+		in      io.Reader
51
+		srcName = opts.source
52
+	)
53
+
54
+	if opts.source == "-" {
55
+		in = dockerCli.In()
56
+	} else if !urlutil.IsURL(opts.source) {
57
+		srcName = "-"
58
+		file, err := os.Open(opts.source)
59
+		if err != nil {
60
+			return err
61
+		}
62
+		defer file.Close()
63
+		in = file
64
+	}
65
+
66
+	source := types.ImageImportSource{
67
+		Source:     in,
68
+		SourceName: srcName,
69
+	}
70
+
71
+	options := types.ImageImportOptions{
72
+		Message: opts.message,
73
+		Changes: opts.changes,
74
+	}
75
+
76
+	clnt := dockerCli.Client()
77
+
78
+	responseBody, err := clnt.ImageImport(context.Background(), source, opts.reference, options)
79
+	if err != nil {
80
+		return err
81
+	}
82
+	defer responseBody.Close()
83
+
84
+	return jsonmessage.DisplayJSONMessagesStream(responseBody, dockerCli.Out(), dockerCli.OutFd(), dockerCli.IsTerminalOut(), nil)
85
+}
0 86
deleted file mode 100644
... ...
@@ -1,68 +0,0 @@
1
-package client
2
-
3
-import (
4
-	"io"
5
-	"os"
6
-
7
-	"golang.org/x/net/context"
8
-
9
-	Cli "github.com/docker/docker/cli"
10
-	"github.com/docker/docker/opts"
11
-	"github.com/docker/docker/pkg/jsonmessage"
12
-	flag "github.com/docker/docker/pkg/mflag"
13
-	"github.com/docker/docker/pkg/urlutil"
14
-	"github.com/docker/engine-api/types"
15
-)
16
-
17
-// CmdImport creates an empty filesystem image, imports the contents of the tarball into the image, and optionally tags the image.
18
-//
19
-// The URL argument is the address of a tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) file or a path to local file relative to docker client. If the URL is '-', then the tar file is read from STDIN.
20
-//
21
-// Usage: docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
22
-func (cli *DockerCli) CmdImport(args ...string) error {
23
-	cmd := Cli.Subcmd("import", []string{"file|URL|- [REPOSITORY[:TAG]]"}, Cli.DockerCommands["import"].Description, true)
24
-	flChanges := opts.NewListOpts(nil)
25
-	cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image")
26
-	message := cmd.String([]string{"m", "-message"}, "", "Set commit message for imported image")
27
-	cmd.Require(flag.Min, 1)
28
-
29
-	cmd.ParseFlags(args, true)
30
-
31
-	var (
32
-		in      io.Reader
33
-		src     = cmd.Arg(0)
34
-		srcName = src
35
-		ref     = cmd.Arg(1)
36
-		changes = flChanges.GetAll()
37
-	)
38
-
39
-	if src == "-" {
40
-		in = cli.in
41
-	} else if !urlutil.IsURL(src) {
42
-		srcName = "-"
43
-		file, err := os.Open(src)
44
-		if err != nil {
45
-			return err
46
-		}
47
-		defer file.Close()
48
-		in = file
49
-	}
50
-
51
-	source := types.ImageImportSource{
52
-		Source:     in,
53
-		SourceName: srcName,
54
-	}
55
-
56
-	options := types.ImageImportOptions{
57
-		Message: *message,
58
-		Changes: changes,
59
-	}
60
-
61
-	responseBody, err := cli.client.ImageImport(context.Background(), source, ref, options)
62
-	if err != nil {
63
-		return err
64
-	}
65
-	defer responseBody.Close()
66
-
67
-	return jsonmessage.DisplayJSONMessagesStream(responseBody, cli.out, cli.outFd, cli.isTerminalOut, nil)
68
-}
... ...
@@ -44,6 +44,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
44 44
 		container.NewUnpauseCommand(dockerCli),
45 45
 		image.NewRemoveCommand(dockerCli),
46 46
 		image.NewSearchCommand(dockerCli),
47
+		image.NewImportCommand(dockerCli),
47 48
 		network.NewNetworkCommand(dockerCli),
48 49
 		volume.NewVolumeCommand(dockerCli),
49 50
 	)
... ...
@@ -16,7 +16,6 @@ var DockerCommandUsage = []Command{
16 16
 	{"exec", "Run a command in a running container"},
17 17
 	{"history", "Show the history of an image"},
18 18
 	{"images", "List images"},
19
-	{"import", "Import the contents from a tarball to create a filesystem image"},
20 19
 	{"info", "Display system-wide information"},
21 20
 	{"inspect", "Return low-level information on a container or image"},
22 21
 	{"kill", "Kill a running container"},