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") return fmt.Errorf("empty command")
} }
Debugf("[executeCommand] sending job '%s'\n", strings.Join(cmd.Args, " ")) 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 { if err != nil {
return fmt.Errorf("%v\n", err) 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) { func CmdMultiprint(args []string, stdout, stderr io.Writer, in beam.Receiver, out beam.Sender) {
var tasks sync.WaitGroup var tasks sync.WaitGroup
for { defer tasks.Wait()
payload, a, err := in.Receive() r := beam.NewRouter(out)
if err != nil { multiprint := func(p []byte, a *os.File) error {
return
}
if a != nil {
tasks.Add(1) tasks.Add(1)
go func(payload []byte, attachment *os.File) { go func() {
defer tasks.Done() defer tasks.Done()
msg := data.Message(string(payload)) defer a.Close()
input := bufio.NewScanner(attachment) msg := data.Message(string(p))
input := bufio.NewScanner(a)
for input.Scan() { for input.Scan() {
fmt.Printf("[%s] %s\n", msg.Pretty(), input.Text()) fmt.Printf("[%s] %s\n", msg.Pretty(), input.Text())
} }
}(payload, a) }()
return nil
} }
} r.NewRoute().KeyIncludes("type", "job").Passthrough(out)
tasks.Wait() 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) { func CmdListen(args []string, stdout, stderr io.Writer, in beam.Receiver, out beam.Sender) {