瀏覽代碼

a few cleanups for client output

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
Qiang Huang 10 年之前
父節點
當前提交
5a6db4fd44

+ 1 - 2
api/client/cli.go

@@ -90,8 +90,7 @@ func (cli *DockerCli) Cmd(args ...string) error {
 	if len(args) > 0 {
 		method, exists := cli.getMethod(args[0])
 		if !exists {
-			fmt.Fprintf(cli.err, "docker: '%s' is not a docker command. See 'docker --help'.\n", args[0])
-			os.Exit(1)
+			return fmt.Errorf("docker: '%s' is not a docker command. See 'docker --help'.", args[0])
 		}
 		return method(args[1:]...)
 	}

+ 1 - 3
api/client/help.go

@@ -2,7 +2,6 @@ package client
 
 import (
 	"fmt"
-	"os"
 
 	flag "github.com/docker/docker/pkg/mflag"
 )
@@ -23,8 +22,7 @@ func (cli *DockerCli) CmdHelp(args ...string) error {
 	if len(args) > 0 {
 		method, exists := cli.getMethod(args[0])
 		if !exists {
-			fmt.Fprintf(cli.err, "docker: '%s' is not a docker command. See 'docker --help'.\n", args[0])
-			os.Exit(1)
+			return fmt.Errorf("docker: '%s' is not a docker command. See 'docker --help'.", args[0])
 		} else {
 			method("--help")
 			return nil

+ 0 - 1
api/client/inspect.go

@@ -26,7 +26,6 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
 	if *tmplStr != "" {
 		var err error
 		if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil {
-			fmt.Fprintf(cli.err, "Template parsing error: %v\n", err)
 			return StatusError{StatusCode: 64,
 				Status: "Template parsing error: " + err.Error()}
 		}

+ 6 - 3
api/client/kill.go

@@ -16,14 +16,17 @@ func (cli *DockerCli) CmdKill(args ...string) error {
 
 	cmd.ParseFlags(args, true)
 
-	var encounteredError error
+	var errNames []string
 	for _, name := range cmd.Args() {
 		if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/kill?signal=%s", name, *signal), nil, nil)); err != nil {
 			fmt.Fprintf(cli.err, "%s\n", err)
-			encounteredError = fmt.Errorf("Error: failed to kill one or more containers")
+			errNames = append(errNames, name)
 		} else {
 			fmt.Fprintf(cli.out, "%s\n", name)
 		}
 	}
-	return encounteredError
+	if len(errNames) > 0 {
+		return fmt.Errorf("Error: failed to kill containers: %v", errNames)
+	}
+	return nil
 }

+ 6 - 3
api/client/pause.go

@@ -14,14 +14,17 @@ func (cli *DockerCli) CmdPause(args ...string) error {
 	cmd.Require(flag.Min, 1)
 	cmd.ParseFlags(args, false)
 
-	var encounteredError error
+	var errNames []string
 	for _, name := range cmd.Args() {
 		if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/pause", name), nil, nil)); err != nil {
 			fmt.Fprintf(cli.err, "%s\n", err)
-			encounteredError = fmt.Errorf("Error: failed to pause container named %s", name)
+			errNames = append(errNames, name)
 		} else {
 			fmt.Fprintf(cli.out, "%s\n", name)
 		}
 	}
-	return encounteredError
+	if len(errNames) > 0 {
+		return fmt.Errorf("Error: failed to pause containers: %v", errNames)
+	}
+	return nil
 }

+ 6 - 3
api/client/restart.go

@@ -21,15 +21,18 @@ func (cli *DockerCli) CmdRestart(args ...string) error {
 	v := url.Values{}
 	v.Set("t", strconv.Itoa(*nSeconds))
 
-	var encounteredError error
+	var errNames []string
 	for _, name := range cmd.Args() {
 		_, _, err := readBody(cli.call("POST", "/containers/"+name+"/restart?"+v.Encode(), nil, nil))
 		if err != nil {
 			fmt.Fprintf(cli.err, "%s\n", err)
-			encounteredError = fmt.Errorf("Error: failed to restart one or more containers")
+			errNames = append(errNames, name)
 		} else {
 			fmt.Fprintf(cli.out, "%s\n", name)
 		}
 	}
-	return encounteredError
+	if len(errNames) > 0 {
+		return fmt.Errorf("Error: failed to restart containers: %v", errNames)
+	}
+	return nil
 }

+ 6 - 3
api/client/rm.go

@@ -32,7 +32,7 @@ func (cli *DockerCli) CmdRm(args ...string) error {
 		val.Set("force", "1")
 	}
 
-	var encounteredError error
+	var errNames []string
 	for _, name := range cmd.Args() {
 		if name == "" {
 			return fmt.Errorf("Container name cannot be empty")
@@ -42,10 +42,13 @@ func (cli *DockerCli) CmdRm(args ...string) error {
 		_, _, err := readBody(cli.call("DELETE", "/containers/"+name+"?"+val.Encode(), nil, nil))
 		if err != nil {
 			fmt.Fprintf(cli.err, "%s\n", err)
-			encounteredError = fmt.Errorf("Error: failed to remove one or more containers")
+			errNames = append(errNames, name)
 		} else {
 			fmt.Fprintf(cli.out, "%s\n", name)
 		}
 	}
-	return encounteredError
+	if len(errNames) > 0 {
+		return fmt.Errorf("Error: failed to remove containers: %v", errNames)
+	}
+	return nil
 }

+ 7 - 4
api/client/rmi.go

@@ -29,17 +29,17 @@ func (cli *DockerCli) CmdRmi(args ...string) error {
 		v.Set("noprune", "1")
 	}
 
-	var encounteredError error
+	var errNames []string
 	for _, name := range cmd.Args() {
 		rdr, _, err := cli.call("DELETE", "/images/"+name+"?"+v.Encode(), nil, nil)
 		if err != nil {
 			fmt.Fprintf(cli.err, "%s\n", err)
-			encounteredError = fmt.Errorf("Error: failed to remove one or more images")
+			errNames = append(errNames, name)
 		} else {
 			dels := []types.ImageDelete{}
 			if err := json.NewDecoder(rdr).Decode(&dels); err != nil {
 				fmt.Fprintf(cli.err, "%s\n", err)
-				encounteredError = fmt.Errorf("Error: failed to remove one or more images")
+				errNames = append(errNames, name)
 				continue
 			}
 
@@ -52,5 +52,8 @@ func (cli *DockerCli) CmdRmi(args ...string) error {
 			}
 		}
 	}
-	return encounteredError
+	if len(errNames) > 0 {
+		return fmt.Errorf("Error: failed to remove images: %v", errNames)
+	}
+	return nil
 }

+ 5 - 1
api/client/start.go

@@ -120,6 +120,7 @@ func (cli *DockerCli) CmdStart(args ...string) error {
 	}
 
 	var encounteredError error
+	var errNames []string
 	for _, name := range cmd.Args() {
 		_, _, err := readBody(cli.call("POST", "/containers/"+name+"/start", nil, nil))
 		if err != nil {
@@ -127,7 +128,7 @@ func (cli *DockerCli) CmdStart(args ...string) error {
 				// attach and openStdin is false means it could be starting multiple containers
 				// when a container start failed, show the error message and start next
 				fmt.Fprintf(cli.err, "%s\n", err)
-				encounteredError = fmt.Errorf("Error: failed to start one or more containers")
+				errNames = append(errNames, name)
 			} else {
 				encounteredError = err
 			}
@@ -138,6 +139,9 @@ func (cli *DockerCli) CmdStart(args ...string) error {
 		}
 	}
 
+	if len(errNames) > 0 {
+		encounteredError = fmt.Errorf("Error: failed to start containers: %v", errNames)
+	}
 	if encounteredError != nil {
 		return encounteredError
 	}

+ 6 - 3
api/client/stop.go

@@ -23,15 +23,18 @@ func (cli *DockerCli) CmdStop(args ...string) error {
 	v := url.Values{}
 	v.Set("t", strconv.Itoa(*nSeconds))
 
-	var encounteredError error
+	var errNames []string
 	for _, name := range cmd.Args() {
 		_, _, err := readBody(cli.call("POST", "/containers/"+name+"/stop?"+v.Encode(), nil, nil))
 		if err != nil {
 			fmt.Fprintf(cli.err, "%s\n", err)
-			encounteredError = fmt.Errorf("Error: failed to stop one or more containers")
+			errNames = append(errNames, name)
 		} else {
 			fmt.Fprintf(cli.out, "%s\n", name)
 		}
 	}
-	return encounteredError
+	if len(errNames) > 0 {
+		return fmt.Errorf("Error: failed to stop containers: %v", errNames)
+	}
+	return nil
 }

+ 6 - 3
api/client/unpause.go

@@ -14,14 +14,17 @@ func (cli *DockerCli) CmdUnpause(args ...string) error {
 	cmd.Require(flag.Min, 1)
 	cmd.ParseFlags(args, false)
 
-	var encounteredError error
+	var errNames []string
 	for _, name := range cmd.Args() {
 		if _, _, err := readBody(cli.call("POST", fmt.Sprintf("/containers/%s/unpause", name), nil, nil)); err != nil {
 			fmt.Fprintf(cli.err, "%s\n", err)
-			encounteredError = fmt.Errorf("Error: failed to unpause container named %s", name)
+			errNames = append(errNames, name)
 		} else {
 			fmt.Fprintf(cli.out, "%s\n", name)
 		}
 	}
-	return encounteredError
+	if len(errNames) > 0 {
+		return fmt.Errorf("Error: failed to unpause containers: %v", errNames)
+	}
+	return nil
 }

+ 6 - 3
api/client/wait.go

@@ -17,15 +17,18 @@ func (cli *DockerCli) CmdWait(args ...string) error {
 
 	cmd.ParseFlags(args, true)
 
-	var encounteredError error
+	var errNames []string
 	for _, name := range cmd.Args() {
 		status, err := waitForExit(cli, name)
 		if err != nil {
 			fmt.Fprintf(cli.err, "%s\n", err)
-			encounteredError = fmt.Errorf("Error: failed to wait one or more containers")
+			errNames = append(errNames, name)
 		} else {
 			fmt.Fprintf(cli.out, "%d\n", status)
 		}
 	}
-	return encounteredError
+	if len(errNames) > 0 {
+		return fmt.Errorf("Error: failed to wait containers: %v", errNames)
+	}
+	return nil
 }

+ 2 - 2
integration-cli/docker_cli_rm_test.go

@@ -105,8 +105,8 @@ func (s *DockerSuite) TestRmContainerOrphaning(c *check.C) {
 func (s *DockerSuite) TestRmInvalidContainer(c *check.C) {
 	if out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "rm", "unknown")); err == nil {
 		c.Fatal("Expected error on rm unknown container, got none")
-	} else if !strings.Contains(out, "failed to remove one or more containers") {
-		c.Fatalf("Expected output to contain 'failed to remove one or more containers', got %q", out)
+	} else if !strings.Contains(out, "failed to remove containers") {
+		c.Fatalf("Expected output to contain 'failed to remove containers', got %q", out)
 	}
 
 }