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 tasks.Add(1)
} go func() {
if a != nil { defer tasks.Done()
tasks.Add(1) defer a.Close()
go func(payload []byte, attachment *os.File) { msg := data.Message(string(p))
defer tasks.Done() input := bufio.NewScanner(a)
msg := data.Message(string(payload)) for input.Scan() {
input := bufio.NewScanner(attachment) fmt.Printf("[%s] %s\n", msg.Pretty(), input.Text())
for input.Scan() { }
fmt.Printf("[%s] %s\n", msg.Pretty(), input.Text()) }()
} return nil
}(payload, a)
}
} }
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) { func CmdListen(args []string, stdout, stderr io.Writer, in beam.Receiver, out beam.Sender) {