Skip to content

Commit

Permalink
Add piped output
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Kluth <[email protected]>
  • Loading branch information
Alexander Kluth committed Sep 2, 2019
1 parent 4f55fcb commit bc00510
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
. "github.com/logrusorgru/aurora"
"io/ioutil"
"log"
"bufio"
"os"
"os/exec"
"strings"
Expand All @@ -28,6 +29,7 @@ type task struct {
Commands []string
Tasks []string
Output bool
Piped bool
}

func remove(slice []string, s int) []string {
Expand Down Expand Up @@ -64,14 +66,42 @@ func executeTask(doFile Dofile, dirPrefix string, taskName string) {
//}

if doFile.Tasks[taskName].Output == true {
out, _ := cmd.CombinedOutput()

fmt.Println()
fmt.Println(Bold(Yellow("Output:")))
fmt.Println(Yellow("--------------------------------------------------------------------------"))
fmt.Printf(string(out))
fmt.Println(Yellow("--------------------------------------------------------------------------"))
fmt.Println()
if doFile.Tasks[taskName].Piped == true {
cmdReader, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
//TODO: Don't bail out, continue without piping/logging
os.Exit(1)
}

scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
fmt.Printf("\t%s\n", scanner.Text())
}
}()

err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
os.Exit(1)
}

err = cmd.Wait()
if err != nil {
fmt.Fprintln(os.Stderr, "Error waiting for Cmd", err)
os.Exit(1)
}
} else {
out, _ := cmd.CombinedOutput()

fmt.Println()
fmt.Println(Bold(Yellow("Output:")))
fmt.Println(Yellow("--------------------------------------------------------------------------"))
fmt.Printf(string(out))
fmt.Println(Yellow("--------------------------------------------------------------------------"))
fmt.Println()
}
} else {
if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down

0 comments on commit bc00510

Please sign in to comment.