Skip to content

Commit

Permalink
Replaced execute stdout & stderr with multiwriter
Browse files Browse the repository at this point in the history
  • Loading branch information
Shah Newaz Khan committed Oct 29, 2019
1 parent c6aa884 commit c721d7c
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions internal/generate/kubernetes/generate.go
Expand Up @@ -2,14 +2,16 @@ package kubernetes

import (
"bytes"
"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"sync"

"github.com/commitdev/commit0/internal/config"
"github.com/commitdev/commit0/internal/templator"
)

func Generate(templator *templator.Templator, config *config.Commit0Config, wg *sync.WaitGroup) {
Expand All @@ -36,7 +38,6 @@ func tf_plan() *exec.Cmd {
return exec.Command("terraform", "plan")
}

// Executes cmd passed in
func execute(cmd *exec.Cmd) {
dir, err1 := filepath.Abs(filepath.Dir(os.Args[0]))
if err1 != nil {
Expand All @@ -45,11 +46,13 @@ func execute(cmd *exec.Cmd) {

cmd.Dir = dir + "/kubernetes/terraform/environments/staging"

var errStdout, errStderr error
var stdoutBuf, stderrBuf bytes.Buffer
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
stdout := NewCapturingPassThroughWriter(os.Stdout)
stderr := NewCapturingPassThroughWriter(os.Stderr)

var errStdout, errStderr error
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
err := cmd.Start()
if err != nil {
log.Fatalf("cmd.Start() failed with '%s'\n", err)
Expand All @@ -73,29 +76,6 @@ func execute(cmd *exec.Cmd) {
if errStdout != nil || errStderr != nil {
log.Fatal("failed to capture stdout or stderr\n")
}
}

// CapturingPassThroughWriter is a writer that remembers
// data written to it and passes it to w
type CapturingPassThroughWriter struct {
buf bytes.Buffer
w io.Writer
}

// NewCapturingPassThroughWriter creates new CapturingPassThroughWriter
func NewCapturingPassThroughWriter(w io.Writer) *CapturingPassThroughWriter {
return &CapturingPassThroughWriter{
w: w,
}
}

// Write writes data to the writer, returns number of bytes written and an error
func (w *CapturingPassThroughWriter) Write(d []byte) (int, error) {
w.buf.Write(d)
return w.w.Write(d)
}

// Bytes returns bytes written to the writer
func (w *CapturingPassThroughWriter) Bytes() []byte {
return w.buf.Bytes()
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr)
}

1 comment on commit c721d7c

@bmonkman
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why capture the output instead of streaming it?

Please sign in to comment.