Browse code

Refactor CLI docker save

Signed-off-by: Hu Keping <hukeping@huawei.com>

Hu Keping authored on 2015/09/10 21:28:38
Showing 2 changed files
... ...
@@ -52,7 +52,7 @@ type DockerCli struct {
52 52
 	outFd uintptr
53 53
 	// isTerminalIn indicates whether the client's STDIN is a TTY
54 54
 	isTerminalIn bool
55
-	// isTerminalOut dindicates whether the client's STDOUT is a TTY
55
+	// isTerminalOut indicates whether the client's STDOUT is a TTY
56 56
 	isTerminalOut bool
57 57
 	// transport holds the client transport instance.
58 58
 	transport *http.Transport
... ...
@@ -25,13 +25,14 @@ func (cli *DockerCli) CmdSave(args ...string) error {
25 25
 		output = cli.out
26 26
 		err    error
27 27
 	)
28
+
29
+	if *outfile == "" && cli.isTerminalOut {
30
+		return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
31
+	}
28 32
 	if *outfile != "" {
29
-		output, err = os.Create(*outfile)
30
-		if err != nil {
33
+		if output, err = os.Create(*outfile); err != nil {
31 34
 			return err
32 35
 		}
33
-	} else if cli.isTerminalOut {
34
-		return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
35 36
 	}
36 37
 
37 38
 	sopts := &streamOpts{
... ...
@@ -39,19 +40,13 @@ func (cli *DockerCli) CmdSave(args ...string) error {
39 39
 		out:         output,
40 40
 	}
41 41
 
42
-	if len(cmd.Args()) == 1 {
43
-		image := cmd.Arg(0)
44
-		if _, err := cli.stream("GET", "/images/"+image+"/get", sopts); err != nil {
45
-			return err
46
-		}
47
-	} else {
48
-		v := url.Values{}
49
-		for _, arg := range cmd.Args() {
50
-			v.Add("names", arg)
51
-		}
52
-		if _, err := cli.stream("GET", "/images/get?"+v.Encode(), sopts); err != nil {
53
-			return err
54
-		}
42
+	v := url.Values{}
43
+	for _, arg := range cmd.Args() {
44
+		v.Add("names", arg)
55 45
 	}
46
+	if _, err := cli.stream("GET", "/images/get?"+v.Encode(), sopts); err != nil {
47
+		return err
48
+	}
49
+
56 50
 	return nil
57 51
 }