-
Notifications
You must be signed in to change notification settings - Fork 52
/
generate.go
107 lines (85 loc) · 2.25 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package kubernetes
import (
"bytes"
"fmt"
"io"
"sync"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/commitdev/commit0/config"
"github.com/commitdev/commit0/templator"
)
func Generate(templator *templator.Templator, config *config.Commit0Config) {
templator.Kubernetes.TemplateFiles(config, false)
if config.Kubernetes.Deploy {
_tf_init := tf_init()
_tf_plan := tf_plan()
execute(_tf_init)
execute(_tf_plan)
}
}
// Terraform init cmd
func tf_init() *exec.Cmd {
return exec.Command("terraform","init")
}
// Terraform plan cmd
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 {
log.Fatal(err1)
}
cmd.Dir = dir + "/kubernetes/terraform"
var errStdout, errStderr error
stdoutIn, _ := cmd.StdoutPipe()
stderrIn, _ := cmd.StderrPipe()
stdout := NewCapturingPassThroughWriter(os.Stdout)
stderr := NewCapturingPassThroughWriter(os.Stderr)
err := cmd.Start()
if err != nil {
log.Fatalf("cmd.Start() failed with '%s'\n", err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
wg.Done()
}()
_, errStderr = io.Copy(stderr, stderrIn)
wg.Wait()
err = cmd.Wait()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
if errStdout != nil || errStderr != nil {
log.Fatal("failed to capture stdout or stderr\n")
}
outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
fmt.Printf("\nout:\n%s\nerr:\n%s\n", outStr, errStr)
}
// 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()
}