Browse code

a few cleanups for client output

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>

Qiang Huang authored on 2015/05/05 16:59:17
Showing 13 changed files
... ...
@@ -90,8 +90,7 @@ func (cli *DockerCli) Cmd(args ...string) error {
90 90
 	if len(args) > 0 {
91 91
 		method, exists := cli.getMethod(args[0])
92 92
 		if !exists {
93
-			fmt.Fprintf(cli.err, "docker: '%s' is not a docker command. See 'docker --help'.\n", args[0])
94
-			os.Exit(1)
93
+			return fmt.Errorf("docker: '%s' is not a docker command. See 'docker --help'.", args[0])
95 94
 		}
96 95
 		return method(args[1:]...)
97 96
 	}
... ...
@@ -2,7 +2,6 @@ package client
2 2
 
3 3
 import (
4 4
 	"fmt"
5
-	"os"
6 5
 
7 6
 	flag "github.com/docker/docker/pkg/mflag"
8 7
 )
... ...
@@ -23,8 +22,7 @@ func (cli *DockerCli) CmdHelp(args ...string) error {
23 23
 	if len(args) > 0 {
24 24
 		method, exists := cli.getMethod(args[0])
25 25
 		if !exists {
26
-			fmt.Fprintf(cli.err, "docker: '%s' is not a docker command. See 'docker --help'.\n", args[0])
27
-			os.Exit(1)
26
+			return fmt.Errorf("docker: '%s' is not a docker command. See 'docker --help'.", args[0])
28 27
 		} else {
29 28
 			method("--help")
30 29
 			return nil
... ...
@@ -26,7 +26,6 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
26 26
 	if *tmplStr != "" {
27 27
 		var err error
28 28
 		if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
29
-			fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
30 29
 			return StatusError{StatusCode: 64,
31 30
 				Status: "Template parsing error: " + err.Error()}
32 31
 		}
... ...
@@ -16,14 +16,17 @@ func (cli *DockerCli) CmdKill(args ...string) error {
16 16
 
17 17
 	cmd.ParseFlags(args, true)
18 18
 
19
-	var encounteredError error
19
+	var errNames []string
20 20
 	for _, name := range cmd.Args() {
21 21
 		if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/kill?signal=%s", name, *signal), nil, nil)); err != nil {
22 22
 			fmt.Fprintf(cli.err, "%s\n", err)
23
-			encounteredError = fmt.Errorf("Error: failed to kill one or more containers")
23
+			errNames = append(errNames, name)
24 24
 		} else {
25 25
 			fmt.Fprintf(cli.out, "%s\n", name)
26 26
 		}
27 27
 	}
28
-	return encounteredError
28
+	if len(errNames) > 0 {
29
+		return fmt.Errorf("Error: failed to kill containers: %v", errNames)
30
+	}
31
+	return nil
29 32
 }
... ...
@@ -14,14 +14,17 @@ func (cli *DockerCli) CmdPause(args ...string) error {
14 14
 	cmd.Require(flag.Min, 1)
15 15
 	cmd.ParseFlags(args, false)
16 16
 
17
-	var encounteredError error
17
+	var errNames []string
18 18
 	for _, name := range cmd.Args() {
19 19
 		if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/pause", name), nil, nil)); err != nil {
20 20
 			fmt.Fprintf(cli.err, "%s\n", err)
21
-			encounteredError = fmt.Errorf("Error: failed to pause container named %s", name)
21
+			errNames = append(errNames, name)
22 22
 		} else {
23 23
 			fmt.Fprintf(cli.out, "%s\n", name)
24 24
 		}
25 25
 	}
26
-	return encounteredError
26
+	if len(errNames) > 0 {
27
+		return fmt.Errorf("Error: failed to pause containers: %v", errNames)
28
+	}
29
+	return nil
27 30
 }
... ...
@@ -21,15 +21,18 @@ func (cli *DockerCli) CmdRestart(args ...string) error {
21 21
 	v := url.Values{}
22 22
 	v.Set("t", strconv.Itoa(*nSeconds))
23 23
 
24
-	var encounteredError error
24
+	var errNames []string
25 25
 	for _, name := range cmd.Args() {
26 26
 		_, _, err := readBody(cli.call("POST", "/containers/"+name+"/restart?"+v.Encode(), nil, nil))
27 27
 		if err != nil {
28 28
 			fmt.Fprintf(cli.err, "%s\n", err)
29
-			encounteredError = fmt.Errorf("Error: failed to restart one or more containers")
29
+			errNames = append(errNames, name)
30 30
 		} else {
31 31
 			fmt.Fprintf(cli.out, "%s\n", name)
32 32
 		}
33 33
 	}
34
-	return encounteredError
34
+	if len(errNames) > 0 {
35
+		return fmt.Errorf("Error: failed to restart containers: %v", errNames)
36
+	}
37
+	return nil
35 38
 }
... ...
@@ -32,7 +32,7 @@ func (cli *DockerCli) CmdRm(args ...string) error {
32 32
 		val.Set("force", "1")
33 33
 	}
34 34
 
35
-	var encounteredError error
35
+	var errNames []string
36 36
 	for _, name := range cmd.Args() {
37 37
 		if name == "" {
38 38
 			return fmt.Errorf("Container name cannot be empty")
... ...
@@ -42,10 +42,13 @@ func (cli *DockerCli) CmdRm(args ...string) error {
42 42
 		_, _, err := readBody(cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil, nil))
43 43
 		if err != nil {
44 44
 			fmt.Fprintf(cli.err, "%s\n", err)
45
-			encounteredError = fmt.Errorf("Error: failed to remove one or more containers")
45
+			errNames = append(errNames, name)
46 46
 		} else {
47 47
 			fmt.Fprintf(cli.out, "%s\n", name)
48 48
 		}
49 49
 	}
50
-	return encounteredError
50
+	if len(errNames) > 0 {
51
+		return fmt.Errorf("Error: failed to remove containers: %v", errNames)
52
+	}
53
+	return nil
51 54
 }
... ...
@@ -29,17 +29,17 @@ func (cli *DockerCli) CmdRmi(args ...string) error {
29 29
 		v.Set("noprune", "1")
30 30
 	}
31 31
 
32
-	var encounteredError error
32
+	var errNames []string
33 33
 	for _, name := range cmd.Args() {
34 34
 		rdr, _, err := cli.call("DELETE", "/images/"+name+"?"+v.Encode(), nil, nil)
35 35
 		if err != nil {
36 36
 			fmt.Fprintf(cli.err, "%s\n", err)
37
-			encounteredError = fmt.Errorf("Error: failed to remove one or more images")
37
+			errNames = append(errNames, name)
38 38
 		} else {
39 39
 			dels := []types.ImageDelete{}
40 40
 			if err := json.NewDecoder(rdr).Decode(&dels); err != nil {
41 41
 				fmt.Fprintf(cli.err, "%s\n", err)
42
-				encounteredError = fmt.Errorf("Error: failed to remove one or more images")
42
+				errNames = append(errNames, name)
43 43
 				continue
44 44
 			}
45 45
 
... ...
@@ -52,5 +52,8 @@ func (cli *DockerCli) CmdRmi(args ...string) error {
52 52
 			}
53 53
 		}
54 54
 	}
55
-	return encounteredError
55
+	if len(errNames) > 0 {
56
+		return fmt.Errorf("Error: failed to remove images: %v", errNames)
57
+	}
58
+	return nil
56 59
 }
... ...
@@ -120,6 +120,7 @@ func (cli *DockerCli) CmdStart(args ...string) error {
120 120
 	}
121 121
 
122 122
 	var encounteredError error
123
+	var errNames []string
123 124
 	for _, name := range cmd.Args() {
124 125
 		_, _, err := readBody(cli.call("POST", "/containers/"+name+"/start", nil, nil))
125 126
 		if err != nil {
... ...
@@ -127,7 +128,7 @@ func (cli *DockerCli) CmdStart(args ...string) error {
127 127
 				// attach and openStdin is false means it could be starting multiple containers
128 128
 				// when a container start failed, show the error message and start next
129 129
 				fmt.Fprintf(cli.err, "%s\n", err)
130
-				encounteredError = fmt.Errorf("Error: failed to start one or more containers")
130
+				errNames = append(errNames, name)
131 131
 			} else {
132 132
 				encounteredError = err
133 133
 			}
... ...
@@ -138,6 +139,9 @@ func (cli *DockerCli) CmdStart(args ...string) error {
138 138
 		}
139 139
 	}
140 140
 
141
+	if len(errNames) > 0 {
142
+		encounteredError = fmt.Errorf("Error: failed to start containers: %v", errNames)
143
+	}
141 144
 	if encounteredError != nil {
142 145
 		return encounteredError
143 146
 	}
... ...
@@ -23,15 +23,18 @@ func (cli *DockerCli) CmdStop(args ...string) error {
23 23
 	v := url.Values{}
24 24
 	v.Set("t", strconv.Itoa(*nSeconds))
25 25
 
26
-	var encounteredError error
26
+	var errNames []string
27 27
 	for _, name := range cmd.Args() {
28 28
 		_, _, err := readBody(cli.call("POST", "/containers/"+name+"/stop?"+v.Encode(), nil, nil))
29 29
 		if err != nil {
30 30
 			fmt.Fprintf(cli.err, "%s\n", err)
31
-			encounteredError = fmt.Errorf("Error: failed to stop one or more containers")
31
+			errNames = append(errNames, name)
32 32
 		} else {
33 33
 			fmt.Fprintf(cli.out, "%s\n", name)
34 34
 		}
35 35
 	}
36
-	return encounteredError
36
+	if len(errNames) > 0 {
37
+		return fmt.Errorf("Error: failed to stop containers: %v", errNames)
38
+	}
39
+	return nil
37 40
 }
... ...
@@ -14,14 +14,17 @@ func (cli *DockerCli) CmdUnpause(args ...string) error {
14 14
 	cmd.Require(flag.Min, 1)
15 15
 	cmd.ParseFlags(args, false)
16 16
 
17
-	var encounteredError error
17
+	var errNames []string
18 18
 	for _, name := range cmd.Args() {
19 19
 		if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/unpause", name), nil, nil)); err != nil {
20 20
 			fmt.Fprintf(cli.err, "%s\n", err)
21
-			encounteredError = fmt.Errorf("Error: failed to unpause container named %s", name)
21
+			errNames = append(errNames, name)
22 22
 		} else {
23 23
 			fmt.Fprintf(cli.out, "%s\n", name)
24 24
 		}
25 25
 	}
26
-	return encounteredError
26
+	if len(errNames) > 0 {
27
+		return fmt.Errorf("Error: failed to unpause containers: %v", errNames)
28
+	}
29
+	return nil
27 30
 }
... ...
@@ -17,15 +17,18 @@ func (cli *DockerCli) CmdWait(args ...string) error {
17 17
 
18 18
 	cmd.ParseFlags(args, true)
19 19
 
20
-	var encounteredError error
20
+	var errNames []string
21 21
 	for _, name := range cmd.Args() {
22 22
 		status, err := waitForExit(cli, name)
23 23
 		if err != nil {
24 24
 			fmt.Fprintf(cli.err, "%s\n", err)
25
-			encounteredError = fmt.Errorf("Error: failed to wait one or more containers")
25
+			errNames = append(errNames, name)
26 26
 		} else {
27 27
 			fmt.Fprintf(cli.out, "%d\n", status)
28 28
 		}
29 29
 	}
30
-	return encounteredError
30
+	if len(errNames) > 0 {
31
+		return fmt.Errorf("Error: failed to wait containers: %v", errNames)
32
+	}
33
+	return nil
31 34
 }
... ...
@@ -105,8 +105,8 @@ func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
105 105
 func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
106 106
 	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
107 107
 		c.Fatal("Expected error on rm unknown container, got none")
108
-	} else if !strings.Contains(out, "failed to remove one or more containers") {
109
-		c.Fatalf("Expected output to contain 'failed to remove one or more containers', got %q", out)
108
+	} else if !strings.Contains(out, "failed to remove containers") {
109
+		c.Fatalf("Expected output to contain 'failed to remove containers', got %q", out)
110 110
 	}
111 111
 
112 112
 }