Skip to content

Commit

Permalink
use Shell from the environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
brentp committed Jul 18, 2016
1 parent 1de5298 commit 2c1a07c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```

Expand Down
27 changes: 16 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."`
Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
Expand All @@ -137,7 +139,7 @@ func runUnOrdered(args Args) {
if !ok {
return
}
process(c, cmd, args, x)
process(c, cmd, args, x, shell)
}
}()

Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions tests/functional-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
}
Expand Down Expand Up @@ -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)

0 comments on commit 2c1a07c

Please sign in to comment.