From 89cc13b7329b38587175b6d63d5433fc73f46d5c Mon Sep 17 00:00:00 2001 From: Alexander Kluth Date: Wed, 31 Jul 2019 14:51:40 +0200 Subject: [PATCH] New output option to print stdout and stderr Signed-off-by: Alexander Kluth --- Dofile.example | 14 ++++++++++---- main.go | 19 ++++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Dofile.example b/Dofile.example index 288361f..5803d95 100644 --- a/Dofile.example +++ b/Dofile.example @@ -2,13 +2,19 @@ description = "Dofile example" [tasks] - [tasks.build] + [tasks.ls] commands = [ - 'echo "Ja hallo!', + 'ls /tmp', 'ls -l' ] - [tasks.clean] + [tasks.date] commands = [ - 'echo "Ui ui ui ui ui!"', + 'date', + ] + output = true + + [tasks.fail] + commands = [ + 'i-do-not-exist' ] \ No newline at end of file diff --git a/main.go b/main.go index f313978..c31e2b5 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ type Dofile struct { type task struct { Commands []string + Output bool } func remove(slice []string, s int) []string { @@ -50,9 +51,21 @@ func executeTask(doFile Dofile, taskName string) { cmdName := tokens[0] tokens = remove(tokens, 0) - if err := exec.Command(cmdName, tokens...).Run(); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) + cmd := exec.Command(cmdName, tokens...) + + if doFile.Tasks[taskName].Output == true { + out, _ := cmd.CombinedOutput() + + //TODO: Identify if the executable does not exist + //if err != nil { + // log.Fatalf("cmd.Run() failed with %s\n", err) + //} + fmt.Printf(string(out)) + } else { + if err := cmd.Run(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } } } } else {