From 3bc73fa21e4428cd7040df6c5a384bd0e4772236 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 5 Sep 2013 23:54:03 +0000 Subject: [PATCH] Return the process exit code for run commands --- commands.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/commands.go b/commands.go index b2a4d3db13..7512577a2c 100644 --- a/commands.go +++ b/commands.go @@ -367,16 +367,11 @@ func (cli *DockerCli) CmdWait(args ...string) error { return nil } for _, name := range cmd.Args() { - body, _, err := cli.call("POST", "/containers/"+name+"/wait", nil) + status, err := waitForExit(cli, name) if err != nil { fmt.Fprintf(cli.err, "%s", err) } else { - var out APIWait - err = json.Unmarshal(body, &out) - if err != nil { - return err - } - fmt.Fprintf(cli.out, "%d\n", out.StatusCode) + fmt.Fprintf(cli.out, "%d\n", status) } } return nil @@ -1477,8 +1472,16 @@ func (cli *DockerCli) CmdRun(args ...string) error { } if !config.AttachStdout && !config.AttachStderr { + // Detached mode <-wait + } else { + status, err := waitForExit(cli, runResult.ID) + if err != nil { + return err + } + os.Exit(status) } + return nil } @@ -1759,6 +1762,19 @@ func (cli *DockerCli) LoadConfigFile() (err error) { return err } +func waitForExit(cli *DockerCli, containerId string) (int, error) { + body, _, err := cli.call("POST", "/containers/"+containerId+"/wait", nil) + if err != nil { + return -1, err + } + + var out APIWait + if err := json.Unmarshal(body, &out); err != nil { + return -1, err + } + return out.StatusCode, nil +} + func NewDockerCli(in io.ReadCloser, out, err io.Writer, proto, addr string) *DockerCli { var ( isTerminal = false