beam/examples/beamsh: simplify code by using sendWPipe utility

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
Solomon Hykes 2014-03-28 15:50:14 -07:00
parent f7971cfc1e
commit 7958f295e8

View file

@ -228,16 +228,10 @@ func GetHandler(name string) Handler {
if attachment == nil { if attachment == nil {
continue continue
} }
r, w, err := os.Pipe() w, err := sendWPipe(out, payload)
if err != nil { if err != nil {
attachment.Close()
return
}
if err := beam.Send(out, payload, r); err != nil {
attachment.Close()
r.Close()
w.Close()
fmt.Fprintf(stderr, "%v\n", err) fmt.Fprintf(stderr, "%v\n", err)
attachment.Close()
return return
} }
tasks.Add(1) tasks.Add(1)
@ -390,19 +384,19 @@ func GetHandler(name string) Handler {
} else if name == "exec" { } else if name == "exec" {
return func(args []string, in *net.UnixConn, out *net.UnixConn) { return func(args []string, in *net.UnixConn, out *net.UnixConn) {
cmd := exec.Command(args[1], args[2:]...) cmd := exec.Command(args[1], args[2:]...)
outR, outW, err := os.Pipe() stdout, err := sendWPipe(out, data.Empty().Set("cmd", "log", "stdout").Set("fromcmd", args...).Bytes())
if err != nil { if err != nil {
return return
} }
cmd.Stdout = outW defer stdout.Close()
errR, errW, err := os.Pipe() cmd.Stdout = stdout
stderr, err := sendWPipe(out, data.Empty().Set("cmd", "log", "stderr").Set("fromcmd", args...).Bytes())
if err != nil { if err != nil {
return return
} }
cmd.Stderr = errW defer stderr.Close()
cmd.Stderr = stderr
cmd.Stdin = os.Stdin cmd.Stdin = os.Stdin
beam.Send(out, data.Empty().Set("cmd", "log", "stdout").Set("fromcmd", args...).Bytes(), outR)
beam.Send(out, data.Empty().Set("cmd", "log", "stderr").Set("fromcmd", args...).Bytes(), errR)
execErr := cmd.Run() execErr := cmd.Run()
var status string var status string
if execErr != nil { if execErr != nil {
@ -411,8 +405,6 @@ func GetHandler(name string) Handler {
status = "ok" status = "ok"
} }
beam.Send(out, data.Empty().Set("status", status).Set("cmd", args...).Bytes(), nil) beam.Send(out, data.Empty().Set("status", status).Set("cmd", args...).Bytes(), nil)
outW.Close()
errW.Close()
} }
} else if name == "trace" { } else if name == "trace" {
return func(args []string, in *net.UnixConn, out *net.UnixConn) { return func(args []string, in *net.UnixConn, out *net.UnixConn) {