From 5efb90b0cd6b1873ea43b25519e9d5832374eba4 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 25 Mar 2014 10:14:52 -0700 Subject: [PATCH] beam/examples/beamsh: simple 'exec' command Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- beam/examples/beamsh/beamsh.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/beam/examples/beamsh/beamsh.go b/beam/examples/beamsh/beamsh.go index 9b7fa93..dc3608f 100644 --- a/beam/examples/beamsh/beamsh.go +++ b/beam/examples/beamsh/beamsh.go @@ -4,6 +4,7 @@ import ( "io" "fmt" "os" + "os/exec" "github.com/dotcloud/docker/pkg/dockerscript" "github.com/dotcloud/docker/pkg/beam" "github.com/dotcloud/docker/pkg/beam/data" @@ -182,6 +183,36 @@ func CmdTrace(args []string, f *os.File) { } +func CmdExec(args []string, f *os.File) { + resp, err := beam.FdConn(int(f.Fd())) + if err != nil { + Fatal(err) + return + } + defer resp.Close() + cmd := exec.Command(args[1], args[2:]...) + Logf("EXEC %s %s\n", cmd.Path, cmd.Args) + stdoutR, stdoutW, err := os.Pipe() + if err != nil { + Fatal(err) + return + } + cmd.Stdout = stdoutW + stderrR, stderrW, err := os.Pipe() + if err != nil { + Fatal(err) + return + } + cmd.Stderr = stderrW + if err := beam.Send(resp, data.Empty().Set("cmd", "log", "stdout").Bytes(), stdoutR); err != nil { + Fatal(err) + } + if err := beam.Send(resp, data.Empty().Set("cmd", "log", "stderr").Bytes(), stderrR); err != nil { + Fatal(err) + } + cmd.Run() +} + func CmdLog(args []string, f *os.File) { defer Debugf("CmdLog done\n") var name string @@ -319,6 +350,8 @@ func builtinsHandler(args []string, attachment *os.File) { CmdLog(args, attachment) } else if args[0] == "trace" { CmdTrace(args, attachment) + } else if args[0] == "exec" { + CmdExec(args, attachment) } }