From c92abca6c7dd70742ba723bf332d5241c2b16989 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Mon, 31 Mar 2014 12:45:12 -0700 Subject: [PATCH] beam/examples/beamsh: 'prompt' asks the user for a value and sends it. 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 b7eccc3..e02424b 100644 --- a/beam/examples/beamsh/beamsh.go +++ b/beam/examples/beamsh/beamsh.go @@ -361,6 +361,39 @@ func GetHandler(name string) Handler { } } } + } else if name == "prompt" { + return func(args []string, in beam.Receiver, out beam.Sender) { + stdout, err := beam.SendPipe(out, data.Empty().Set("cmd", "log", "stdout").Set("fromcmd", args...).Bytes()) + if err != nil { + return + } + defer stdout.Close() + stderr, err := beam.SendPipe(out, data.Empty().Set("cmd", "log", "stderr").Set("fromcmd", args...).Bytes()) + if err != nil { + return + } + defer stderr.Close() + if len(args) < 2 { + fmt.Fprintf(stderr, "usage: %s PROMPT...\n", args[0]) + return + } + if !term.IsTerminal(0) { + fmt.Fprintf(stderr, "can't prompt: no tty available...\n") + return + } + fmt.Printf("%s: ", strings.Join(args[1:], " ")) + oldState, _ := term.SaveState(0) + term.DisableEcho(0, oldState) + line, _, err := bufio.NewReader(os.Stdin).ReadLine() + if err != nil { + fmt.Fprintln(stderr, err.Error()) + return + } + val := string(line) + fmt.Printf("\n") + term.RestoreTerminal(0, oldState) + out.Send(data.Empty().Set("fromcmd", args...).Set("value", val).Bytes(), nil) + } } else if name == "stdio" { return func(args []string, in beam.Receiver, out beam.Sender) { stdout, err := beam.SendPipe(out, data.Empty().Set("cmd", "log", "stdout").Set("fromcmd", args...).Bytes())