use helm engine in cli

This commit is contained in:
Diego Fernando Carrión 2023-10-21 15:29:26 +02:00
parent 4dac2ea565
commit 07d0cab3ee
Signed by: CRThaze
GPG Key ID: 0F647F8A6A3AF588

84
main.go
View File

@ -79,8 +79,10 @@ var flags struct {
valuesFiles valFileSlice
values valSlice
outputDelimiter string
// helmCompatFuncs bool
helmCompat bool
helm bool
helmStrict bool
helmDNSFunc bool
helmLint bool
}
func init() {
@ -91,15 +93,25 @@ func init() {
"d", "---",
"Output delimiter between template files rendered",
)
// flag.BoolVar(
// &flags.helmCompatFuncs,
// "hf", false,
// "Enable Helm compatibility functions. See below for more info.",
// )
flag.BoolVar(
&flags.helmCompat,
&flags.helm,
"helm", false,
"Helm compatibility: behave more like Helm templating. See below for more info.",
"Use Helm templating language (superset of Sprig).",
)
flag.BoolVar(
&flags.helmStrict,
"helmStrict", true,
"When using Helm Tpl Lang, use strict rendering.",
)
flag.BoolVar(
&flags.helmDNSFunc,
"helm", true,
"When using Helm Tpl Lang, support DNS resolution.",
)
flag.BoolVar(
&flags.helmLint,
"helmLint", false,
"When using Helm Tpl Lang, enable the Linting Mode.",
)
flag.Parse()
@ -127,20 +139,9 @@ func init() {
fmt.Fprintf(flag.CommandLine.Output(), "\n")
fmt.Fprintf(
flag.CommandLine.Output(),
"Helm Compatibility Mode: this mode attempts to mimic the behavior\n"+
"of the Helm Template command. It does the following:\n"+
"\t- Provided values are automatically nested under the .Values key.\n"+
"\t- Helm compatibility functions are enabled (see below for more info).\n",
)
fmt.Fprintf(flag.CommandLine.Output(), "\n")
fmt.Fprintf(
flag.CommandLine.Output(),
"Helm Compatibility Funcs: these are approximate implementations of the\n"+
"additional functions included in the Helm Templating language."+
"The following are implemented:\n"+
"\t- include\n"+
"\t- required\n"+
"\t- tpl\n",
"Helm Mode: use the Helm Templating Language/Engine\n"+
"with the following caveats:\n"+
"\t- 'lookup' function unavailable.\n",
)
fmt.Fprintf(flag.CommandLine.Output(), "\n")
}
@ -223,7 +224,7 @@ func expandFiles(files []string) (expFiles []string) {
func mergeValues() (res map[string]interface{}) {
res = map[string]interface{}{}
result := res
if flags.helmCompat {
if flags.helm {
res["Values"] = map[string]interface{}{}
result = res["Values"].(map[string]interface{})
}
@ -257,6 +258,11 @@ func mergeValues() (res map[string]interface{}) {
func main() {
var err error
var ht *helm.Templater
lo := &helm.LangOpts{
Strict: flags.helmStrict,
LintMode: flags.helmLint,
EnableDNS: flags.helmDNSFunc,
}
tpl := template.New("base").Funcs(sprig.FuncMap())
values := mergeValues()
@ -281,7 +287,7 @@ func main() {
ch <- struct{}{}
checkErr(err)
if flags.helmCompat {
if flags.helm {
ht = helm.NewTemplaterWithBytes(data, nil)
fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n")
tpl, err = tpl.Parse(string(data))
@ -295,10 +301,9 @@ func main() {
data, err = io.ReadAll(os.Stdin)
checkErr(err)
if flags.helmCompat {
ht = helm.NewTemplaterWithBytes(data, nil)
if flags.helm {
fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n")
tpl, err = tpl.Parse(string(data))
ht = helm.NewTemplaterWithBytes(data, lo)
} else {
tpl, err = tpl.Parse(string(data))
}
@ -306,24 +311,27 @@ func main() {
} else {
// Otherwise use the provided tplFiles/directories for templates to render.
tplFiles := expandFiles(flag.Args())
if flags.helmCompat {
if flags.helm {
fmt.Fprintf(os.Stderr, "WARN: Helm Compatibility only partially implemented.\n")
ht, err = helm.NewTemplaterWithFiles(tplFiles, nil)
ht, err = helm.NewTemplaterWithFiles(tplFiles, lo)
checkErr(err)
tpl, err = tpl.ParseFiles(tplFiles...)
rendered, err := ht.Render(values)
checkErr(err)
for i, file := range tplFiles {
if i > 0 {
fmt.Println(flags.outputDelimiter)
first := true
for filename, content := range rendered {
if first {
first = false
} else {
fmt.Fprintf(os.Stdout, "%s: %s", flags.outputDelimiter, filename)
}
checkErr(tpl.ExecuteTemplate(os.Stdout, filepath.Base(file), values))
fmt.Fprintln(os.Stdout, content)
}
} else {
tpl, err = tpl.ParseFiles(tplFiles...)
checkErr(err)
for i, file := range tplFiles {
if i > 0 {
fmt.Println(flags.outputDelimiter)
fmt.Fprintf(os.Stdout, "%s: %s", flags.outputDelimiter, file)
}
checkErr(tpl.ExecuteTemplate(os.Stdout, filepath.Base(file), values))
}
@ -331,7 +339,9 @@ func main() {
return
}
if ht != nil {
checkErr(tpl.Execute(os.Stdout, values))
rendered, err := ht.Render(values)
checkErr(err)
fmt.Fprintln(os.Stdout, rendered)
} else {
checkErr(tpl.Execute(os.Stdout, values))
}