Browse Source

redirect stdout

pull/78/head
Chris Mague 6 years ago
parent
commit
bb6f162247
  1. 29
      plugin.go

29
plugin.go

@ -1,7 +1,9 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
@ -162,6 +164,8 @@ func (p Plugin) Exec() error {
if p.Config.RootDir != "" { if p.Config.RootDir != "" {
c.Tfcmd.Dir = c.Tfcmd.Dir + "/" + p.Config.RootDir c.Tfcmd.Dir = c.Tfcmd.Dir + "/" + p.Config.RootDir
} }
if c.Ofile == "" {
c.Tfcmd.Stdout = os.Stdout c.Tfcmd.Stdout = os.Stdout
c.Tfcmd.Stderr = os.Stderr c.Tfcmd.Stderr = os.Stderr
if !p.Config.Sensitive { if !p.Config.Sensitive {
@ -174,7 +178,30 @@ func (p Plugin) Exec() error {
"error": err, "error": err,
}).Fatal("Failed to execute a command") }).Fatal("Failed to execute a command")
} }
logrus.Debug("Command completed successfully") } else {
// open the out file for writing
outfile, err := os.Create(c.Ofile)
if err != nil {
panic(err)
}
defer outfile.Close()
stdoutPipe, err := c.Tfcmd.StdoutPipe()
if err != nil {
panic(err)
}
writer := bufio.NewWriter(outfile)
err = c.Tfcmd.Start()
if err != nil {
panic(err)
}
go io.Copy(writer, stdoutPipe)
c.Tfcmd.Wait()
}
} }
return nil return nil

Loading…
Cancel
Save