-
Notifications
You must be signed in to change notification settings - Fork 1
/
output.go
147 lines (134 loc) · 3.17 KB
/
output.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"text/template"
"time"
)
// SendOutput starts 'kubectl' and pipes the templated result into an 'apply'
func SendOutput(cfgs []*SK8config) bool {
div := ""
var cmd *exec.Cmd
stopIn := make(chan bool)
stopErr := make(chan bool)
var output io.WriteCloser
output = os.Stdout
if args.Apply {
cmd = exec.Command("kubectl", "apply", "-f", "-")
overrideIn, err := cmd.StdinPipe()
if err != nil {
panic(err)
}
overrideErr, err := cmd.StderrPipe()
if err != nil {
panic(err)
}
overrideOut, err := cmd.StdoutPipe()
if err != nil {
panic(err)
}
scannerIn := bufio.NewScanner(overrideOut)
go readStuff(scannerIn, stopIn, false)
scannerErr := bufio.NewScanner(overrideErr)
go readStuff(scannerErr, stopErr, true)
output = overrideIn
}
fmt.Fprintf(output, "# This file was auto-created by 'sk8' %s\n#\n", time.Now().String())
for _, o := range cfgs {
if o.cfgType == typeKubectl {
log.Infof("Send raw k8-configs: %s", o.Kind)
_, _ = output.Write([]byte("---\n"))
_, _ = output.Write(o.RawYAML)
} else {
if !args.Apply {
pretty, err := json_Marshal(o) //, "## ", " ")
if err == nil {
lines := strings.Split(string(pretty), "\n")
for _, line := range lines {
fmt.Fprintln(output, "## ", line)
}
fmt.Fprintln(output, "#")
//fmt.Fprintf(output, "## %s\n#\n", string(pretty))
}
}
if args.AllTemplates {
for _, path := range o.Templates {
o.makeYaml(path, &div, &output)
}
} else {
for _, tmplArg := range args.Templates {
for tmplID, path := range o.Templates {
if strings.HasPrefix(tmplID, tmplArg) {
o.makeYaml(path, &div, &output)
}
}
// path := o.Templates[tmplkey]
}
}
}
}
if args.Apply {
output.Close()
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
<-stopIn
<-stopErr
err = cmd.Wait()
if err != nil {
log.Errorf("kubectl failed: %s", err.Error())
return false
}
}
return true
}
func (cfg *SK8config) makeYaml(pathname string, div *string, output *io.WriteCloser) {
fi, err := os.Stat(pathname)
if err != nil {
cwd, _ := os.Getwd()
log.Debugf("cwd = %s", cwd)
log.Warningf("Warning: template %s error: %v", pathname, err)
return
}
if fi.IsDir() {
log.Debugf("Ignore template %s -> directory", pathname)
return
}
if pathname != "" {
w := *output
log.Infof("Create YAML from the %q template for %s/%s", pathname, cfg.Namespace, cfg.Name)
tmpl := template.New("tmpl").Funcs(FuncMap())
buf, err := ioutil.ReadFile(pathname)
if err != nil {
panic(err)
}
tmpl, err = tmpl.Parse(string(buf))
//tmpl, err := tmpl.ParseFiles(os.Args[2])
if err != nil {
panic(err)
}
fmt.Fprint(w, *div)
fmt.Fprintf(w, "# This is autogenerated from the %s-template\n#\n", path.Base(pathname))
*div = "---\n"
//err = tmpl.Execute(os.Stdout, cfg)
err = tmpl.Execute(w, cfg)
if err != nil {
panic(err)
}
}
}
func dump(o interface{}) {
out, err := json_Marshal(&o) //, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(out))
fmt.Println("##############################################")
}