From 2c1a07c764a4150c24209270ae5d487bb224fe9c Mon Sep 17 00:00:00 2001 From: Brent Pedersen Date: Mon, 18 Jul 2016 16:48:44 -0600 Subject: [PATCH] use Shell from the environment. --- README.md | 5 ++--- main.go | 27 ++++++++++++++++----------- tests/functional-test.sh | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index ec7358a..ff53f1e 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Usage ===== ``` -usage: gargs [--procs PROCS] [--nlines NLINES] [--sep SEP] [--shell SHELL] [--verbose] [--continue-on-error] [--ordered] [--dry-run] COMMAND +usage: gargs_race [--procs PROCS] [--nlines NLINES] [--sep SEP] [--verbose] [--continue-on-error] [--ordered] [--dry-run] COMMAND positional arguments: command command to execute @@ -75,12 +75,11 @@ options: --nlines NLINES, -n NLINES number of lines to consume for each command. -s and -n are mutually exclusive. [default: 1] --sep SEP, -s SEP regular expression split line with to fill multiple template spots default is not to split. -s and -n are mutually exclusive. - --shell SHELL shell to use [default: bash] --verbose, -v print commands to stderr before they are executed. --continue-on-error, -c report errors but don't stop the entire execution (which is the default). --ordered, -o keep output in order of input; default is to output in order of return which greatly improves parallelization. - --dry-run, -d print (but do not run) the commands (for debugging) + --dry-run, -d print (but do not run) the commands --help, -h display this help and exit ``` diff --git a/main.go b/main.go index f61b34f..bafcd9a 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,7 @@ import ( ) // VERSION is the current version -const VERSION = "0.3.1-dev" +const VERSION = "0.3.1" // EXIT_CODE is the highest exit code seen in any command var EXIT_CODE = 0 @@ -29,7 +29,6 @@ type Args struct { Nlines int `arg:"-n,help:number of lines to consume for each command. -s and -n are mutually exclusive."` Command string `arg:"positional,required,help:command to execute"` Sep string `arg:"-s,help:regular expression split line with to fill multiple template spots default is not to split. -s and -n are mutually exclusive."` - Shell string `arg:"help:shell to use"` Verbose bool `arg:"-v,help:print commands to stderr before they are executed."` ContinueOnError bool `arg:"-c,--continue-on-error,help:report errors but don't stop the entire execution (which is the default)."` Ordered bool `arg:"-o,help:keep output in order of input; default is to output in order of return which greatly improves parallelization."` @@ -48,7 +47,10 @@ func main() { args.Procs = 1 args.Nlines = 1 args.Sep = "" - args.Shell = "bash" + shell := os.Getenv("SHELL") + if shell == "" { + shell = "bash" + } args.Verbose = false args.ContinueOnError = false args.Ordered = false @@ -63,9 +65,9 @@ func main() { } runtime.GOMAXPROCS(args.Procs) if args.Ordered { - runOrdered(args) + runOrdered(args, shell) } else { - runUnOrdered(args) + runUnOrdered(args, shell) } os.Exit(EXIT_CODE) } @@ -121,7 +123,7 @@ func genXargs(n int, sep string) chan *xargs { return ch } -func runUnOrdered(args Args) { +func runUnOrdered(args Args, shell string) { c := make(chan []byte) chXargs := genXargs(args.Nlines, args.Sep) cmd := makeCommand(args.Command) @@ -137,7 +139,7 @@ func runUnOrdered(args Args) { if !ok { return } - process(c, cmd, args, x) + process(c, cmd, args, x, shell) } }() @@ -151,7 +153,7 @@ func runUnOrdered(args Args) { } } -func runOrdered(args Args) { +func runOrdered(args Args, shell string) { ch := make(chan chan []byte, args.Procs) chXargs := genXargs(args.Nlines, args.Sep) @@ -162,7 +164,7 @@ func runOrdered(args Args) { ich := make(chan []byte, 1) ch <- ich go func(ich chan []byte, x *xargs) { - process(ich, cmd, args, x) + process(ich, cmd, args, x, shell) close(ich) }(ich, xa) } @@ -184,7 +186,7 @@ func makeCommand(cmd string) string { return v } -func process(ch chan []byte, cmdStr string, args Args, xarg *xargs) { +func process(ch chan []byte, cmdStr string, args Args, xarg *xargs, shell string) { tmpl, err := template.New(cmdStr).Parse(cmdStr) check(err) @@ -202,7 +204,10 @@ func process(ch chan []byte, cmdStr string, args Args, xarg *xargs) { return } - cmd := exec.Command(args.Shell, "-c", cmdStr) + cmd := exec.Command(shell, "-c", cmdStr) + if strings.HasSuffix(shell, "perl") { + cmd = exec.Command(shell, "-e", cmdStr) + } cmd.Stderr = os.Stderr out, err := cmd.Output() if err != nil { diff --git a/tests/functional-test.sh b/tests/functional-test.sh index 7abdc94..6938e91 100755 --- a/tests/functional-test.sh +++ b/tests/functional-test.sh @@ -3,9 +3,12 @@ test -e ssshtest || wget -q https://raw.githubusercontent.com/ryanlayer/ssshtest/master/ssshtest . ssshtest +set -e go build -o gargs_race -race -a +set +e + fn_check_basic() { seq 12 -1 1 | ./gargs_race -p 5 -n 3 -d -v 'sleep {0}; echo {1} {2}' } @@ -33,3 +36,21 @@ run check_exit_err fn_check_exit_err assert_exit_code 1 assert_in_stdout "0.2" assert_in_stderr "ZeroDivisionError" + + +fn_custom_shell(){ + seq 0 5 | SHELL=python ./gargs_race -c "print '%.2f' % {}" +} +run check_custom_shell fn_custom_shell +assert_exit_code 0 +assert_in_stdout "1.00" +assert_equal "6" $(wc -l $STDOUT_FILE) + +fn_perl() { + seq 1 5 | SHELL=perl ./gargs_race 'print {} . "\n"' +} +run check_perl fn_perl +assert_exit_code 0 +assert_in_stdout "1" +assert_equal "5" $(wc -l $STDOUT_FILE) +