forked from carvel-dev/kapp-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytt.go
93 lines (71 loc) · 1.98 KB
/
ytt.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
package template
import (
"bytes"
"io"
goexec "os/exec"
"github.com/k14s/kapp-controller/pkg/apis/kappctrl/v1alpha1"
"github.com/k14s/kapp-controller/pkg/exec"
"github.com/k14s/kapp-controller/pkg/fetch"
"github.com/k14s/kapp-controller/pkg/memdir"
)
type Ytt struct {
opts v1alpha1.AppTemplateYtt
genericOpts GenericOpts
fetchFactory fetch.Factory
}
var _ Template = &Ytt{}
func NewYtt(opts v1alpha1.AppTemplateYtt,
genericOpts GenericOpts, fetchFactory fetch.Factory) *Ytt {
return &Ytt{opts, genericOpts, fetchFactory}
}
func (t *Ytt) TemplateDir(dirPath string) exec.CmdRunResult {
return t.template(dirPath, nil)
}
func (t *Ytt) TemplateStream(input io.Reader) exec.CmdRunResult {
return t.template("-", input)
}
func (t *Ytt) template(dirPath string, input io.Reader) exec.CmdRunResult {
args := t.addArgs([]string{"-f", dirPath})
args, inlineDir, err := t.addInlinePaths(args)
if inlineDir != nil {
defer inlineDir.Remove()
}
if err != nil {
return exec.NewCmdRunResultWithErr(err)
}
var stdoutBs, stderrBs bytes.Buffer
cmd := goexec.Command("ytt", args...)
cmd.Stdin = input
cmd.Stdout = &stdoutBs
cmd.Stderr = &stderrBs
err = cmd.Run()
result := exec.CmdRunResult{
Stdout: stdoutBs.String(),
Stderr: stderrBs.String(),
}
result.AttachErrorf("Templating dir: %s", err)
return result
}
func (t *Ytt) addArgs(args []string) []string {
if t.opts.IgnoreUnknownComments {
args = append(args, "--ignore-unknown-comments")
}
return args
}
func (t *Ytt) addInlinePaths(args []string) ([]string, *memdir.TmpDir, error) {
if t.opts.Inline == nil {
return args, nil, nil
}
inlineDir := memdir.NewTmpDir("template-ytt-inline")
err := inlineDir.Create()
if err != nil {
return nil, nil, err
}
inline := t.fetchFactory.NewInline(*t.opts.Inline, t.genericOpts.Namespace)
err = inline.Retrieve(inlineDir.Path())
if err != nil {
return nil, inlineDir, err
}
args = append(args, []string{"-f", inlineDir.Path()}...)
return args, inlineDir, nil
}