Merge pull request #12961 from hqhq/hq_remove_err_out

a few cleanups for client output
This commit is contained in:
Jessie Frazelle 2015-05-07 16:51:56 -07:00
commit 372977148d
13 changed files with 58 additions and 34 deletions

View File

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

View File

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

View File

@ -26,7 +26,6 @@ func (cli *DockerCli) CmdInspect(args ...string) error {
if *tmplStr != "" { if *tmplStr != "" {
var err error var err error
if tmpl, err = template.New("").Funcs(funcMap).Parse(*tmplStr); err != nil { 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, return StatusError{StatusCode: 64,
Status: "Template parsing error: " + err.Error()} Status: "Template parsing error: " + err.Error()}
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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