improve helm output options

This commit is contained in:
Diego Fernando Carrión 2023-10-23 10:24:16 +02:00
parent 07d0cab3ee
commit 480b2c6e04
Signed by: CRThaze
GPG Key ID: 0F647F8A6A3AF588

70
main.go
View File

@ -76,13 +76,17 @@ func (v *valSlice) Set(value string) error {
}
var flags struct {
valuesFiles valFileSlice
values valSlice
outputDelimiter string
helm bool
helmStrict bool
helmDNSFunc bool
helmLint bool
valuesFiles valFileSlice
values valSlice
outputDelimiter string
helm bool
helmStrict bool
helmDNSFunc bool
helmLint bool
helmNestValues bool
outputFilenamePrefix string
outputFilenameSTDERR bool
outputDelimiterSTDERR bool
}
func init() {
@ -105,7 +109,7 @@ func init() {
)
flag.BoolVar(
&flags.helmDNSFunc,
"helm", true,
"helmDNS", true,
"When using Helm Tpl Lang, support DNS resolution.",
)
flag.BoolVar(
@ -113,6 +117,26 @@ func init() {
"helmLint", false,
"When using Helm Tpl Lang, enable the Linting Mode.",
)
flag.BoolVar(
&flags.helmNestValues,
"helmNestValues", true,
"When using Helm Tpl Lang, Nest provided values under '.Values'",
)
flag.StringVar(
&flags.outputFilenamePrefix,
"fp", "# ",
"Prefix for the filename in the output.",
)
flag.BoolVar(
&flags.outputDelimiterSTDERR,
"delimErr", false,
"Whether to print output delimiter to STDERR.",
)
flag.BoolVar(
&flags.outputFilenameSTDERR,
"filenameErr", true,
"Whether to print filename to STDERR.",
)
flag.Parse()
flag.Usage = func() {
@ -149,7 +173,7 @@ func init() {
func checkErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %+v", err)
fmt.Fprintf(os.Stderr, "ERROR: %+v\n", err)
os.Exit(1)
}
}
@ -224,7 +248,7 @@ func expandFiles(files []string) (expFiles []string) {
func mergeValues() (res map[string]interface{}) {
res = map[string]interface{}{}
result := res
if flags.helm {
if flags.helm && flags.helmNestValues {
res["Values"] = map[string]interface{}{}
result = res["Values"].(map[string]interface{})
}
@ -322,16 +346,36 @@ func main() {
if first {
first = false
} else {
fmt.Fprintf(os.Stdout, "%s: %s", flags.outputDelimiter, filename)
out := os.Stdout
if flags.outputDelimiterSTDERR {
out = os.Stderr
}
fmt.Fprintf(out, "%s\n", flags.outputDelimiter)
out = os.Stdout
if flags.outputFilenameSTDERR {
out = os.Stderr
}
fmt.Fprintf(out, "%s%s\n", flags.outputFilenamePrefix, filename)
}
fmt.Fprintln(os.Stdout, content)
fmt.Fprint(os.Stdout, content)
}
} else {
tpl, err = tpl.ParseFiles(tplFiles...)
checkErr(err)
for i, file := range tplFiles {
if i > 0 {
fmt.Fprintf(os.Stdout, "%s: %s", flags.outputDelimiter, file)
out := os.Stdout
if flags.outputDelimiterSTDERR {
out = os.Stderr
}
fmt.Fprintf(out, "%s\n", flags.outputDelimiter)
out = os.Stdout
if flags.outputFilenameSTDERR {
out = os.Stderr
}
fmt.Fprintf(out, "%s%s\n", flags.outputFilenamePrefix, file)
}
checkErr(tpl.ExecuteTemplate(os.Stdout, filepath.Base(file), values))
}