Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/input/formats/yaml/ytt.go
2848 views
1
package yaml
2
3
import (
4
"fmt"
5
"strings"
6
7
yttcmd "carvel.dev/ytt/pkg/cmd/template"
8
yttui "carvel.dev/ytt/pkg/cmd/ui"
9
yttfiles "carvel.dev/ytt/pkg/files"
10
"gopkg.in/yaml.v2"
11
)
12
13
func ytt(tpl, dvs []string, varFiles []string) ([]byte, error) {
14
// create and invoke ytt "template" command
15
templatingOptions := yttcmd.NewOptions()
16
17
input, err := templatesAsInput(tpl...)
18
if err != nil {
19
return nil, err
20
}
21
22
if len(varFiles) > 0 {
23
// Load vaarFiles into the templating options.
24
templatingOptions.DataValuesFlags.FromFiles = varFiles
25
}
26
27
// equivalent to `--data-value-yaml`
28
templatingOptions.DataValuesFlags.KVsFromYAML = dvs
29
30
// for in-memory use, pipe output to "/dev/null"
31
noopUI := yttui.NewCustomWriterTTY(false, noopWriter{}, noopWriter{})
32
33
// Evaluate the template given the configured data values...
34
output := templatingOptions.RunWithFiles(input, noopUI)
35
if output.Err != nil {
36
return nil, output.Err
37
}
38
39
return output.DocSet.AsBytes()
40
}
41
42
// templatesAsInput conveniently wraps one or more strings, each in a files.File, into a template.Input.
43
func templatesAsInput(tpl ...string) (yttcmd.Input, error) {
44
var files []*yttfiles.File
45
for i, t := range tpl {
46
// to make this less brittle, you'll probably want to use well-defined names for `path`, here, for each input.
47
// this matters when you're processing errors which report based on these paths.
48
file, err := yttfiles.NewFileFromSource(yttfiles.NewBytesSource(fmt.Sprintf("tpl%d.yml", i), []byte(t)))
49
if err != nil {
50
return yttcmd.Input{}, err
51
}
52
53
files = append(files, file)
54
}
55
56
return yttcmd.Input{Files: files}, nil
57
}
58
59
func mapToKeyValueSlice(m map[string]interface{}) []string {
60
var result []string
61
for k, v := range m {
62
y, _ := yaml.Marshal(v)
63
result = append(result, fmt.Sprintf("%s=%s", k, strings.TrimSpace(string(y))))
64
}
65
return result
66
}
67
68
type noopWriter struct{}
69
70
func (w noopWriter) Write(data []byte) (int, error) { return len(data), nil }
71
72