Skip to content

Commit

Permalink
Merge pull request #3 from MasteryConnect/print-stderr
Browse files Browse the repository at this point in the history
Capture stderr and print out with a prefix of ERR:
  • Loading branch information
danielcbaldwin committed Nov 19, 2015
2 parents 4da87ce + 21e2a2e commit 139458c
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,10 @@ SOFTWARE.

cmd := exec.Command("sh", "-c", command)

cmdReader, err := cmd.StdoutPipe()
if err != nil {
log.Fatal("Error creating stdoutpipe for command", err)
}
setupStdout(cmd)
setupStderr(cmd)

scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
log.Println(scanner.Text())
}
}()

err = cmd.Start()
err := cmd.Start()
if err != nil {
log.Fatal("Error running command", err)
}
Expand All @@ -154,3 +145,32 @@ SOFTWARE.

app.Run(os.Args)
}

func setupStdout(cmd *exec.Cmd) {
cmdReader, err := cmd.StdoutPipe()
if err != nil {
log.Fatal("Error creating stdoutpipe for command", err)
}

scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
log.Println(scanner.Text())
}
}()
}

func setupStderr(cmd *exec.Cmd) {
cmdReader, err := cmd.StderrPipe()
if err != nil {
log.Fatal("Error creating stderrpipe for command", err)
}

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

}

0 comments on commit 139458c

Please sign in to comment.