beam/examples/beamsh: use beam.Router to simplify 'multiprint' and fix job passthrough

Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
Solomon Hykes 2014-04-02 20:05:00 -07:00
parent d1528caaac
commit 9598cba7c0
2 changed files with 18 additions and 18 deletions

View file

@ -153,7 +153,7 @@ func executeCommand(out beam.Sender, cmd *dockerscript.Command) error {
return fmt.Errorf("empty command")
}
Debugf("[executeCommand] sending job '%s'\n", strings.Join(cmd.Args, " "))
job, err := beam.SendConn(out, data.Empty().Set("cmd", cmd.Args...).Bytes())
job, err := beam.SendConn(out, data.Empty().Set("cmd", cmd.Args...).Set("type", "job").Bytes())
if err != nil {
return fmt.Errorf("%v\n", err)
}

View file

@ -226,24 +226,24 @@ func CmdPrint(args []string, stdout, stderr io.Writer, in beam.Receiver, out bea
func CmdMultiprint(args []string, stdout, stderr io.Writer, in beam.Receiver, out beam.Sender) {
var tasks sync.WaitGroup
for {
payload, a, err := in.Receive()
if err != nil {
return
}
if a != nil {
defer tasks.Wait()
r := beam.NewRouter(out)
multiprint := func(p []byte, a *os.File) error {
tasks.Add(1)
go func(payload []byte, attachment *os.File) {
go func() {
defer tasks.Done()
msg := data.Message(string(payload))
input := bufio.NewScanner(attachment)
defer a.Close()
msg := data.Message(string(p))
input := bufio.NewScanner(a)
for input.Scan() {
fmt.Printf("[%s] %s\n", msg.Pretty(), input.Text())
}
}(payload, a)
}()
return nil
}
}
tasks.Wait()
r.NewRoute().KeyIncludes("type", "job").Passthrough(out)
r.NewRoute().HasAttachment().Handler(multiprint).Tee(out)
beam.Copy(r, in)
}
func CmdListen(args []string, stdout, stderr io.Writer, in beam.Receiver, out beam.Sender) {