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

Commit a6b2cbb

Browse files
Merge pull request #3 from segmentio/add-xtrace
Run shell commands with -x when -x arg
2 parents c89b5c8 + 127a0fb commit a6b2cbb

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

cli/cli.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ func Run(c *config.Config, name string, args []string) {
105105
}
106106
}
107107

108+
// Run the task with xtrace
109+
func RunTrace(c *config.Config, name string, args []string) {
110+
task, ok := c.Tasks[name]
111+
if !ok {
112+
Fatalf("undefined task %q", name)
113+
}
114+
115+
task.LookupPath = filepath.Dir(c.File)
116+
117+
err := task.RunTrace(args)
118+
if err != nil {
119+
Fatalf("error: %s", err)
120+
}
121+
}
122+
108123
// Fatalf writes to stderr and exits.
109124
func Fatalf(msg string, args ...interface{}) {
110125
fmt.Fprintf(os.Stderr, "\n %s\n\n", fmt.Sprintf(msg, args...))

main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ var (
2222
const usage = `
2323
Usage:
2424
robo [--config file]
25-
robo <task> [<arg>...] [--config file]
25+
robo [--xtrace] <task> [<arg>...] [--config file]
2626
robo help [<task>] [--config file]
2727
robo variables [--config file]
2828
robo -h | --help
2929
robo --version
3030
3131
Options:
3232
-c, --config file config file to load
33+
-x, --xtrace set xtrace when running scripts or commands
3334
-h, --help output help information
3435
-v, --version output version
3536
@@ -96,7 +97,12 @@ func main() {
9697
Set("args", arguments),
9798
})
9899

99-
cli.Run(conf, name, arguments)
100+
xtrace, ok := args["--xtrace"].(bool)
101+
if xtrace && ok {
102+
cli.RunTrace(conf, name, arguments)
103+
} else {
104+
cli.Run(conf, name, arguments)
105+
}
100106
} else {
101107
cli.List(conf)
102108
}

task/task.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ type Task struct {
2525
Exec string
2626
Usage string
2727
Examples []*Example
28+
Trace bool
29+
}
30+
31+
// Run the task with xtrace shell option
32+
func (t *Task) RunTrace(args []string) error {
33+
t.Trace = true
34+
return t.Run(args)
2835
}
2936

3037
// Run the task with `args`.
@@ -48,6 +55,9 @@ func (t *Task) Run(args []string) error {
4855
func (t *Task) RunScript(args []string) error {
4956
path := filepath.Join(t.LookupPath, t.Script)
5057
args = append([]string{path}, args...)
58+
if t.Trace {
59+
args = append([]string{"-x"}, args...)
60+
}
5161
cmd := exec.Command("sh", args...)
5262
cmd.Stdin = os.Stdin
5363
cmd.Stdout = os.Stdout
@@ -58,6 +68,9 @@ func (t *Task) RunScript(args []string) error {
5868
// RunCommand runs the `command` via the shell.
5969
func (t *Task) RunCommand(args []string) error {
6070
args = append([]string{"-c", t.Command, "sh"}, args...)
71+
if t.Trace {
72+
args = append([]string{"-x"}, args...)
73+
}
6174
cmd := exec.Command("sh", args...)
6275
cmd.Stdin = os.Stdin
6376
cmd.Stdout = os.Stdout

0 commit comments

Comments
 (0)