Skip to content

Commit

Permalink
Refactor to use error returns
Browse files Browse the repository at this point in the history
  • Loading branch information
pwr22 committed Mar 4, 2019
1 parent 1541313 commit 1f2cc34
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 40 deletions.
84 changes: 52 additions & 32 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,33 @@ var keepOrder = flag.BoolP("keep-order", "k", false, "print output in the order
var dryRun = flag.Bool("dry-run", false, "print the commands that would be run instead of running them") // no shorthand to match GNU Parallel

// parse flags and commandline args
func parseArgs() {
func parseArgs() (exitEarly bool, err error) {
flag.SetInterspersed(false) // don't confuse flags to the command with our own
flag.Parse()

if *printVersion {
fmt.Println(version)
os.Exit(0)
return true, nil
}

if *dryRun {
for _, c := range getCmdStrings() {
cmds, err := getCmdStrings()
if err != nil {
return false, err
}

for _, c := range cmds {
fmt.Println(c)
}
os.Exit(0)
return true, nil
}

return false, nil
}

// return any command arguments given on the command line, if any
// TODO pull out this parsing into a separate file and simplify it
func getArgSets() [][]string {
func getArgSets() ([][]string, error) {
var cmdSets [][]string

inArgList, inFileList, setStartIdx := false, false, 0
Expand All @@ -53,16 +60,19 @@ func getArgSets() [][]string {
arg, symType = "::::", "files"
}

fmt.Fprintln(os.Stderr, arg+" must be followed by "+symType)
os.Exit(2)
return nil, fmt.Errorf("%v must be followed by %v", arg, symType)
} else if inFileList { // store the file set we were building
for _, file := range flag.Args()[setStartIdx:i] {
if file == "-" && stdinRead {
fmt.Fprintln(os.Stderr, "standard input cannot be used as an argument source multiple times")
os.Exit(2)
return nil, fmt.Errorf("standard input cannot be used as an argument source multiple times")
}

cmdSets = append(cmdSets, readCmdsFromFile(file))
cmds, err := readCmdsFromFile(file)
if err != nil {
return nil, err
}

cmdSets = append(cmdSets, cmds)
stdinRead = true
}
inFileList = false
Expand All @@ -83,19 +93,22 @@ func getArgSets() [][]string {
arg, symType = "::::", "files"
}

fmt.Fprintln(os.Stderr, arg+" must be followed by "+symType)
os.Exit(2)
return nil, fmt.Errorf("%v must be followed by %v", arg, symType)
} else if inArgList { // store the arg set we were building
cmdSets = append(cmdSets, flag.Args()[setStartIdx:i])
inArgList = false
} else if inFileList { // store the previous file set we were building
for _, file := range flag.Args()[setStartIdx:i] {
if file == "-" && stdinRead {
fmt.Fprintln(os.Stderr, "standard input cannot be used as an argument source multiple times")
os.Exit(2)
return nil, fmt.Errorf("standard input cannot be used as an argument source multiple times")
}

cmds, err := readCmdsFromFile(file)
if err != nil {
return nil, err
}

cmdSets = append(cmdSets, readCmdsFromFile(file))
cmdSets = append(cmdSets, cmds)
stdinRead = true
}
}
Expand All @@ -115,23 +128,26 @@ func getArgSets() [][]string {
arg, symType = "::::", "files"
}

fmt.Fprintln(os.Stderr, arg+" must be followed by "+symType)
os.Exit(2)
return nil, fmt.Errorf("%v must be followed by %v", arg, symType)
} else if inArgList { // otherwise we just need to store the final set
cmdSets = append(cmdSets, flag.Args()[setStartIdx:len(flag.Args())])
} else if inFileList {
for _, file := range flag.Args()[setStartIdx:len(flag.Args())] {
if file == "-" && stdinRead {
fmt.Fprintln(os.Stderr, "standard input cannot be used as an argument source multiple times")
os.Exit(2)
return nil, fmt.Errorf("standard input cannot be used as an argument source multiple times")
}

cmdSets = append(cmdSets, readCmdsFromFile(file))
cmds, err := readCmdsFromFile(file)
if err != nil {
return nil, err
}

cmdSets = append(cmdSets, cmds)
stdinRead = true
}
}

return cmdSets
return cmdSets, nil
}

// return the command prefix given on the commandline, if any
Expand Down Expand Up @@ -186,14 +202,14 @@ func permuteArgSets(sets [][]string) []string {
}

// reads in commands from a file with "-" meaning std in
func readCmdsFromFile(name string) []string {
func readCmdsFromFile(name string) ([]string, error) {
var file io.Reader
if name == "-" {
file = os.Stdin
} else {
f, err := os.Open(name)
if err != nil {
panic(err)
return nil, err
}
file = f
}
Expand All @@ -208,22 +224,26 @@ func readCmdsFromFile(name string) []string {
cmds = append(cmds, scanner.Text())
}

return cmds
return cmds, nil
}

const placeHolder string = "{}"

// read in commands to run
func getCmdStrings() []string {
prefix := getCmdPrefix() // any command given as arguments on the command line
cmdLineArgSets := getArgSets() // looks for ::: arguments
var cmds []string
func getCmdStrings() ([]string, error) {
prefix := getCmdPrefix() // any command given as arguments on the command line
cmdLineArgSets, err := getArgSets() // looks for ::: arguments
if err != nil {
return nil, err
}

// get commands
var cmds []string
if len(cmdLineArgSets) == 0 { // get args from stdin
cmds = readCmdsFromFile("-")
if cmds, err = readCmdsFromFile("-"); err != nil {
return nil, err
}

} else {
} else { // get them from command line and / or files
cmds = permuteArgSets(cmdLineArgSets)
}

Expand All @@ -237,5 +257,5 @@ func getCmdStrings() []string {
}
}

return cmds
return cmds, nil
}
23 changes: 20 additions & 3 deletions args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,13 @@ func TestGetArgSets(t *testing.T) {
// TODO refactor so this isn't called directly and we pass arguments instead
os.Args = tC.args
parseArgs()
if as := getArgSets(); !reflect.DeepEqual(as, tC.exp) {

as, err := getArgSets()
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(as, tC.exp) {
t.Fatalf("expected %v but got %v", tC.exp, as)
}
})
Expand Down Expand Up @@ -220,7 +226,12 @@ func TestReadCmdsFromFile(t *testing.T) {
t.Fatal(err)
}

if cmds := readCmdsFromFile(tmpfile.Name()); !reflect.DeepEqual(cmds, exp) {
cmds, err := readCmdsFromFile(tmpfile.Name())
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(cmds, exp) {
t.Fatalf("expected %v but got %v", exp, cmds)
}

Expand Down Expand Up @@ -251,7 +262,13 @@ func TestGetCmdStrings(t *testing.T) {
// TODO refactor so this isn't called directly and we pass arguments instead
os.Args = tC.args
parseArgs()
if c := getCmdStrings(); !reflect.DeepEqual(c, tC.exp) {

c, err := getCmdStrings()
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(c, tC.exp) {
t.Fatalf("expected %v but got %v", tC.exp, c)
}
})
Expand Down
4 changes: 2 additions & 2 deletions job/job_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ func (job *Job) Stop() {
func sendCtrlBreak(pid int) {
dll, err := syscall.LoadDLL("kernel32.dll")
if err != nil {
panic(err)
panic(err) // should never happen
}

proc, err := dll.FindProc("GenerateConsoleCtrlEvent")
if err != nil {
panic(err)
panic(err) // should never happen
}

res, _, err := proc.Call(syscall.CTRL_BREAK_EVENT, uintptr(pid))
Expand Down
16 changes: 14 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"os"

"github.com/pwr22/zoom/run"
Expand All @@ -9,6 +10,17 @@ import (
const version = "v0.1.2"

func main() {
parseArgs()
os.Exit(run.Cmds(getCmdStrings(), *parallelism, *keepOrder))
if exitEarly, err := parseArgs(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
} else if exitEarly {
os.Exit(0)
}

cmds, err := getCmdStrings()
if err != nil {
os.Exit(2)
}

run.Cmds(cmds, *parallelism, *keepOrder)
}
2 changes: 1 addition & 1 deletion run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (state *runState) handleStopEarlySignal() {
}

// Cmds executes the commands its given in parallel
func Cmds(cmdStrs []string, numOfRunners int, keepOrder bool) int {
func Cmds(cmdStrs []string, numOfRunners int, keepOrder bool) (exitStatus int) {
if numOfRunners == 0 { // means run everything at once
numOfRunners = len(cmdStrs)
} else if numOfRunners > len(cmdStrs) { // or if there are more runners than commands then drop the excess
Expand Down

0 comments on commit 1f2cc34

Please sign in to comment.