@@ -2,14 +2,16 @@ package kubernetes
22
33import (
44 "bytes"
5- "github.com/commitdev/commit0/internal/config"
6- "github.com/commitdev/commit0/internal/templator"
5+ "fmt"
76 "io"
87 "log"
98 "os"
109 "os/exec"
1110 "path/filepath"
1211 "sync"
12+
13+ "github.com/commitdev/commit0/internal/config"
14+ "github.com/commitdev/commit0/internal/templator"
1315)
1416
1517func Generate (templator * templator.Templator , config * config.Commit0Config , wg * sync.WaitGroup ) {
@@ -36,7 +38,6 @@ func tf_plan() *exec.Cmd {
3638 return exec .Command ("terraform" , "plan" )
3739}
3840
39- // Executes cmd passed in
4041func execute (cmd * exec.Cmd ) {
4142 dir , err1 := filepath .Abs (filepath .Dir (os .Args [0 ]))
4243 if err1 != nil {
@@ -45,11 +46,13 @@ func execute(cmd *exec.Cmd) {
4546
4647 cmd .Dir = dir + "/kubernetes/terraform/environments/staging"
4748
48- var errStdout , errStderr error
49+ var stdoutBuf , stderrBuf bytes. Buffer
4950 stdoutIn , _ := cmd .StdoutPipe ()
5051 stderrIn , _ := cmd .StderrPipe ()
51- stdout := NewCapturingPassThroughWriter (os .Stdout )
52- stderr := NewCapturingPassThroughWriter (os .Stderr )
52+
53+ var errStdout , errStderr error
54+ stdout := io .MultiWriter (os .Stdout , & stdoutBuf )
55+ stderr := io .MultiWriter (os .Stderr , & stderrBuf )
5356 err := cmd .Start ()
5457 if err != nil {
5558 log .Fatalf ("cmd.Start() failed with '%s'\n " , err )
@@ -73,29 +76,6 @@ func execute(cmd *exec.Cmd) {
7376 if errStdout != nil || errStderr != nil {
7477 log .Fatal ("failed to capture stdout or stderr\n " )
7578 }
76- }
77-
78- // CapturingPassThroughWriter is a writer that remembers
79- // data written to it and passes it to w
80- type CapturingPassThroughWriter struct {
81- buf bytes.Buffer
82- w io.Writer
83- }
84-
85- // NewCapturingPassThroughWriter creates new CapturingPassThroughWriter
86- func NewCapturingPassThroughWriter (w io.Writer ) * CapturingPassThroughWriter {
87- return & CapturingPassThroughWriter {
88- w : w ,
89- }
90- }
91-
92- // Write writes data to the writer, returns number of bytes written and an error
93- func (w * CapturingPassThroughWriter ) Write (d []byte ) (int , error ) {
94- w .buf .Write (d )
95- return w .w .Write (d )
96- }
97-
98- // Bytes returns bytes written to the writer
99- func (w * CapturingPassThroughWriter ) Bytes () []byte {
100- return w .buf .Bytes ()
79+ outStr , errStr := string (stdoutBuf .Bytes ()), string (stderrBuf .Bytes ())
80+ fmt .Printf ("\n out:\n %s\n err:\n %s\n " , outStr , errStr )
10181}
0 commit comments