From 1a3c8051c5eeffae90f4ba72af062500d9f83912 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Thu, 27 Mar 2014 15:54:18 -0700 Subject: [PATCH] beam/examples/beamsh: scripts can be passed as filenames Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- beam/examples/beamsh/beamsh.go | 62 ++++++++++++++++++++---------- beam/examples/beamsh/helloworld.ds | 8 ++++ 2 files changed, 49 insertions(+), 21 deletions(-) create mode 100755 beam/examples/beamsh/helloworld.ds diff --git a/beam/examples/beamsh/beamsh.go b/beam/examples/beamsh/beamsh.go index 05cec7c..fac986e 100644 --- a/beam/examples/beamsh/beamsh.go +++ b/beam/examples/beamsh/beamsh.go @@ -25,34 +25,54 @@ func main() { Fatal(err) } defer devnull.Close() - if term.IsTerminal(0) { - input := bufio.NewScanner(os.Stdin) - for { - os.Stdout.Write([]byte("beamsh> ")) - if !input.Scan() { - break - } - line := input.Text() - if len(line) != 0 { - cmd, err := dockerscript.Parse(strings.NewReader(line)) - if err != nil { - fmt.Fprintf(os.Stderr, "error: %v\n", err) - continue + if len(os.Args) == 1 { + if term.IsTerminal(0) { + input := bufio.NewScanner(os.Stdin) + for { + os.Stdout.Write([]byte("beamsh> ")) + if !input.Scan() { + break + } + line := input.Text() + if len(line) != 0 { + cmd, err := dockerscript.Parse(strings.NewReader(line)) + if err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + continue + } + if err := executeScript(devnull, cmd); err != nil { + Fatal(err) + } + } + if err := input.Err(); err == io.EOF { + break + } else if err != nil { + Fatal(err) } - executeScript(devnull, cmd) } - if err := input.Err(); err == io.EOF { - break - } else if err != nil { + } else { + script, err := dockerscript.Parse(os.Stdin) + if err != nil { + Fatal("parse error: %v\n", err) + } + if err := executeScript(devnull, script); err != nil { Fatal(err) } } } else { - script, err := dockerscript.Parse(os.Stdin) - if err != nil { - Fatal("parse error: %v\n", err) + for _, scriptpath := range os.Args[1:] { + f, err := os.Open(scriptpath) + if err != nil { + Fatal(err) + } + script, err := dockerscript.Parse(f) + if err != nil { + Fatal("parse error: %v\n", err) + } + if err := executeScript(devnull, script); err != nil { + Fatal(err) + } } - executeScript(devnull, script) } } diff --git a/beam/examples/beamsh/helloworld.ds b/beam/examples/beamsh/helloworld.ds new file mode 100755 index 0000000..d3c6c6f --- /dev/null +++ b/beam/examples/beamsh/helloworld.ds @@ -0,0 +1,8 @@ +#!/usr/bin/env beamsh + +print { + trace { + emit hello + emit world + } +}