Browse code

Migrate load command to cobra

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

Vincent Demeester authored on 2016/06/10 00:28:33
Showing 5 changed files
... ...
@@ -8,7 +8,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
8 8
 		"exec":    cli.CmdExec,
9 9
 		"info":    cli.CmdInfo,
10 10
 		"inspect": cli.CmdInspect,
11
-		"load":    cli.CmdLoad,
12 11
 		"login":   cli.CmdLogin,
13 12
 		"logout":  cli.CmdLogout,
14 13
 		"ps":      cli.CmdPs,
15 14
new file mode 100644
... ...
@@ -0,0 +1,67 @@
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/spf13/cobra"
12
+)
13
+
14
+type loadOptions struct {
15
+	input string
16
+	quiet bool
17
+}
18
+
19
+// NewLoadCommand creates a new `docker load` command
20
+func NewLoadCommand(dockerCli *client.DockerCli) *cobra.Command {
21
+	var opts loadOptions
22
+
23
+	cmd := &cobra.Command{
24
+		Use:   "load [OPTIONS]",
25
+		Short: "Load an image from a tar archive or STDIN",
26
+		Args:  cli.NoArgs,
27
+		RunE: func(cmd *cobra.Command, args []string) error {
28
+			return runLoad(dockerCli, opts)
29
+		},
30
+	}
31
+
32
+	flags := cmd.Flags()
33
+
34
+	flags.StringVarP(&opts.input, "input", "i", "", "Read from tar archive file, instead of STDIN")
35
+	flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress the load output")
36
+
37
+	return cmd
38
+}
39
+
40
+func runLoad(dockerCli *client.DockerCli, opts loadOptions) error {
41
+
42
+	var input io.Reader = dockerCli.In()
43
+	if opts.input != "" {
44
+		file, err := os.Open(opts.input)
45
+		if err != nil {
46
+			return err
47
+		}
48
+		defer file.Close()
49
+		input = file
50
+	}
51
+	if !dockerCli.IsTerminalOut() {
52
+		opts.quiet = true
53
+	}
54
+	response, err := dockerCli.Client().ImageLoad(context.Background(), input, opts.quiet)
55
+	if err != nil {
56
+		return err
57
+	}
58
+	defer response.Body.Close()
59
+
60
+	if response.Body != nil && response.JSON {
61
+		return jsonmessage.DisplayJSONMessagesStream(response.Body, dockerCli.Out(), dockerCli.OutFd(), dockerCli.IsTerminalOut(), nil)
62
+	}
63
+
64
+	_, err = io.Copy(dockerCli.Out(), response.Body)
65
+	return err
66
+}
0 67
deleted file mode 100644
... ...
@@ -1,50 +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/pkg/jsonmessage"
11
-	flag "github.com/docker/docker/pkg/mflag"
12
-)
13
-
14
-// CmdLoad loads an image from a tar archive.
15
-//
16
-// The tar archive is read from STDIN by default, or from a tar archive file.
17
-//
18
-// Usage: docker load [OPTIONS]
19
-func (cli *DockerCli) CmdLoad(args ...string) error {
20
-	cmd := Cli.Subcmd("load", nil, Cli.DockerCommands["load"].Description, true)
21
-	infile := cmd.String([]string{"i", "-input"}, "", "Read from a tar archive file, instead of STDIN")
22
-	quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Suppress the load output")
23
-	cmd.Require(flag.Exact, 0)
24
-	cmd.ParseFlags(args, true)
25
-
26
-	var input io.Reader = cli.in
27
-	if *infile != "" {
28
-		file, err := os.Open(*infile)
29
-		if err != nil {
30
-			return err
31
-		}
32
-		defer file.Close()
33
-		input = file
34
-	}
35
-	if !cli.isTerminalOut {
36
-		*quiet = true
37
-	}
38
-	response, err := cli.client.ImageLoad(context.Background(), input, *quiet)
39
-	if err != nil {
40
-		return err
41
-	}
42
-	defer response.Body.Close()
43
-
44
-	if response.Body != nil && response.JSON {
45
-		return jsonmessage.DisplayJSONMessagesStream(response.Body, cli.out, cli.outFd, cli.isTerminalOut, nil)
46
-	}
47
-
48
-	_, err = io.Copy(cli.out, response.Body)
49
-	return err
50
-}
... ...
@@ -56,6 +56,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
56 56
 		image.NewBuildCommand(dockerCli),
57 57
 		image.NewHistoryCommand(dockerCli),
58 58
 		image.NewImagesCommand(dockerCli),
59
+		image.NewLoadCommand(dockerCli),
59 60
 		image.NewRemoveCommand(dockerCli),
60 61
 		image.NewSearchCommand(dockerCli),
61 62
 		image.NewImportCommand(dockerCli),
... ...
@@ -13,7 +13,6 @@ var DockerCommandUsage = []Command{
13 13
 	{"exec", "Run a command in a running container"},
14 14
 	{"info", "Display system-wide information"},
15 15
 	{"inspect", "Return low-level information on a container or image"},
16
-	{"load", "Load an image from a tar archive or STDIN"},
17 16
 	{"login", "Log in to a Docker registry"},
18 17
 	{"logout", "Log out from a Docker registry"},
19 18
 	{"ps", "List containers"},