Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #3 from segmentio/add-xtrace
Browse files Browse the repository at this point in the history
Run shell commands with -x when -x arg
  • Loading branch information
nickatsegment authored Jan 28, 2019
2 parents c89b5c8 + 127a0fb commit a6b2cbb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
15 changes: 15 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,21 @@ func Run(c *config.Config, name string, args []string) {
}
}

// Run the task with xtrace
func RunTrace(c *config.Config, name string, args []string) {
task, ok := c.Tasks[name]
if !ok {
Fatalf("undefined task %q", name)
}

task.LookupPath = filepath.Dir(c.File)

err := task.RunTrace(args)
if err != nil {
Fatalf("error: %s", err)
}
}

// Fatalf writes to stderr and exits.
func Fatalf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "\n %s\n\n", fmt.Sprintf(msg, args...))
Expand Down
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ var (
const usage = `
Usage:
robo [--config file]
robo <task> [<arg>...] [--config file]
robo [--xtrace] <task> [<arg>...] [--config file]
robo help [<task>] [--config file]
robo variables [--config file]
robo -h | --help
robo --version
Options:
-c, --config file config file to load
-x, --xtrace set xtrace when running scripts or commands
-h, --help output help information
-v, --version output version
Expand Down Expand Up @@ -96,7 +97,12 @@ func main() {
Set("args", arguments),
})

cli.Run(conf, name, arguments)
xtrace, ok := args["--xtrace"].(bool)
if xtrace && ok {
cli.RunTrace(conf, name, arguments)
} else {
cli.Run(conf, name, arguments)
}
} else {
cli.List(conf)
}
Expand Down
13 changes: 13 additions & 0 deletions task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ type Task struct {
Exec string
Usage string
Examples []*Example
Trace bool
}

// Run the task with xtrace shell option
func (t *Task) RunTrace(args []string) error {
t.Trace = true
return t.Run(args)
}

// Run the task with `args`.
Expand All @@ -48,6 +55,9 @@ func (t *Task) Run(args []string) error {
func (t *Task) RunScript(args []string) error {
path := filepath.Join(t.LookupPath, t.Script)
args = append([]string{path}, args...)
if t.Trace {
args = append([]string{"-x"}, args...)
}
cmd := exec.Command("sh", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
Expand All @@ -58,6 +68,9 @@ func (t *Task) RunScript(args []string) error {
// RunCommand runs the `command` via the shell.
func (t *Task) RunCommand(args []string) error {
args = append([]string{"-c", t.Command, "sh"}, args...)
if t.Trace {
args = append([]string{"-x"}, args...)
}
cmd := exec.Command("sh", args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
Expand Down

0 comments on commit a6b2cbb

Please sign in to comment.