Skip to content

Commit

Permalink
Reworking implementation to be more generalizable.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: David Grisham <[email protected]>
  • Loading branch information
dgrisham committed Sep 6, 2018
1 parent acda684 commit 84a1be4
Showing 1 changed file with 36 additions and 57 deletions.
93 changes: 36 additions & 57 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"path"
"regexp"
"strings"

cli "github.com/urfave/cli"
Expand All @@ -31,8 +30,10 @@ var RunCmd = cli.Command{
},
},
Before: func(c *cli.Context) error {
if present := isTerminatorPresent(c); present {
return c.Set("terminator", "true")
if !c.IsSet("cmdFile") {
if present := isTerminatorPresent(c); present {
return c.Set("terminator", "true")
}
}
return nil
},
Expand All @@ -46,77 +47,55 @@ var RunCmd = cli.Command{
return err
}

nodeRange, args := parseCommand(c.Args(), c.IsSet("terminator"))

if nodeRange == "" {
nodeRange = fmt.Sprintf("[0-%d]", len(nodes)-1)
}

list, err := parseRange(nodeRange)
if err != nil {
return fmt.Errorf("could not parse node range %s", nodeRange)
}

var args [][]string
var terminatorPresent []bool
if c.IsSet("cmdFile") {
cmdFile, err := os.Open(c.String("cmdFile"))
if err != nil {
return err
}
defer cmdFile.Close()

reg := regexp.MustCompile(`(?:(?P<nodeRange>.*?): *)(?P<cmd>[^\\z]+)`)
scanner := bufio.NewScanner(cmdFile)
line := 0
var ranges [][]int
var runCmds []outputFunc
for scanner.Scan() {
submatch := reg.FindStringSubmatch(scanner.Text())
if len(submatch) != 3 {
return fmt.Errorf("could not parse line %d of input file", line)
tokens := strings.Fields(scanner.Text())
term := false
if tokens[0] == "--" {
tokens = tokens[1:]
term = true
}
matches := make(map[string]string)
for i, name := range reg.SubexpNames() {
if i != 0 && name != "" {
matches[name] = submatch[i]
}
}

nodeRange := matches["nodeRange"]
if nodeRange == "" {
nodeRange = fmt.Sprintf("[0-%d]", len(nodes)-1)
}

list, err := parseRange(nodeRange)
if err != nil {
return fmt.Errorf("parse error on line %d: %s", line, err)
}
ranges = append(ranges, list)

tokens := strings.Fields(matches["cmd"])
runCmd := func(node testbedi.Core) (testbedi.Output, error) {
return node.RunCmd(context.Background(), nil, tokens...)
}
runCmds = append(runCmds, runCmd)

line++
terminatorPresent = append(terminatorPresent, term)
args = append(args, tokens)
}

results, err := mapListWithOutput(ranges, nodes, runCmds)
if err != nil {
return err
} else {
cArgsStr := make([]string, c.NArg())
for i, arg := range c.Args() {
cArgsStr[i] = arg
}
return buildReport(results)
args = append(args, cArgsStr)
terminatorPresent = append(terminatorPresent, c.IsSet("terminator"))
}

runCmd := func(node testbedi.Core) (testbedi.Output, error) {
return node.RunCmd(context.Background(), nil, args...)
}
ranges := make([][]int, len(args))
runCmds := make([]outputFunc, len(args))
for i, cmd := range args {
nodeRange, tokens := parseCommand(cmd, terminatorPresent[i])
if nodeRange == "" {
nodeRange = fmt.Sprintf("[0-%d]", len(nodes)-1)
}
list, err := parseRange(nodeRange)
if err != nil {
return fmt.Errorf("could not parse node range %s", nodeRange)
}
ranges[i] = list

results, err := mapWithOutput(list, nodes, runCmd)
if err != nil {
return err
runCmd := func(node testbedi.Core) (testbedi.Output, error) {
return node.RunCmd(context.Background(), nil, tokens...)
}
runCmds[i] = runCmd
}

results, err := mapListWithOutput(ranges, nodes, runCmds)
return buildReport(results)
},
}

0 comments on commit 84a1be4

Please sign in to comment.