-
Notifications
You must be signed in to change notification settings - Fork 136
/
main.go
85 lines (68 loc) · 2.1 KB
/
main.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
// Copyright 2024 The Carvel Authors.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"fmt"
"io"
"os"
yttcmd "carvel.dev/ytt/pkg/cmd/template"
yttui "carvel.dev/ytt/pkg/cmd/ui"
yttfiles "carvel.dev/ytt/pkg/files"
)
func main() {
run(os.Stdout)
}
func run(out io.Writer) {
// inputs are YAML-formatted strings
tpl := []string{`
#@ load("@ytt:data", "data")
---
greeting: #@ "Hello, " + data.values.name
`}
// in the format "(data-value-name)=value"
dvs := []string{"name=world"}
results, err := ytt(tpl, dvs)
if err != nil {
panic(err)
}
_, _ = fmt.Fprintf(out, "%s", results)
}
func ytt(tpl, dvs []string) (string, error) {
// create and invoke ytt "template" command
templatingOptions := yttcmd.NewOptions()
input, err := templatesAsInput(tpl...)
if err != nil {
return "", err
}
// equivalent to `--data-value-yaml`
templatingOptions.DataValuesFlags.KVsFromYAML = dvs
// for in-memory use, pipe output to "/dev/null"
noopUI := yttui.NewCustomWriterTTY(false, noopWriter{}, noopWriter{})
// Evaluate the template given the configured data values...
output := templatingOptions.RunWithFiles(input, noopUI)
if output.Err != nil {
return "", output.Err
}
// output.DocSet contains the full set of resulting YAML documents, in order.
bs, err := output.DocSet.AsBytes()
if err != nil {
return "", err
}
return string(bs), nil
}
// templatesAsInput conveniently wraps one or more strings, each in a files.File, into a template.Input.
func templatesAsInput(tpl ...string) (yttcmd.Input, error) {
var files []*yttfiles.File
for i, t := range tpl {
// to make this less brittle, you'll probably want to use well-defined names for `path`, here, for each input.
// this matters when you're processing errors which report based on these paths.
file, err := yttfiles.NewFileFromSource(yttfiles.NewBytesSource(fmt.Sprintf("tpl%d.yml", i), []byte(t)))
if err != nil {
return yttcmd.Input{}, err
}
files = append(files, file)
}
return yttcmd.Input{Files: files}, nil
}
type noopWriter struct{}
func (w noopWriter) Write(data []byte) (int, error) { return len(data), nil }