Compare commits

...

3 Commits

4 changed files with 130 additions and 127 deletions

2
go.mod
View File

@ -6,7 +6,7 @@ require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/peterbourgon/mergemap v0.0.1
gitlab.com/CRThaze/helm-tmpl-lang v0.0.0-20231023121015-db8999e64395
gitlab.com/CRThaze/sugar v0.0.2
gitlab.com/CRThaze/sugar v0.0.3
gopkg.in/yaml.v3 v3.0.1
)

4
go.sum
View File

@ -126,8 +126,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
gitlab.com/CRThaze/helm-tmpl-lang v0.0.0-20231023121015-db8999e64395 h1:gRGvQGJhtG5dHV7F2C+amtdhyT5eE195lsdpz9ew37o=
gitlab.com/CRThaze/helm-tmpl-lang v0.0.0-20231023121015-db8999e64395/go.mod h1:AJdDOAsr2yJZ5j4k5KGeBb/gcXbwpRzGmELX4fZWFQY=
gitlab.com/CRThaze/sugar v0.0.2 h1:kJvrruckSF15kj9v8HtHFusCmFmOCdHbcHZGgwyw6G8=
gitlab.com/CRThaze/sugar v0.0.2/go.mod h1:iEbJlix4YEoPONzHzzXMptItMeLeItVMvp0TdEmAzBE=
gitlab.com/CRThaze/sugar v0.0.3 h1:dNfRk2ft45uzOQr/fWbTONZoUodoJ0ldIQoVER3hPoQ=
gitlab.com/CRThaze/sugar v0.0.3/go.mod h1:iEbJlix4YEoPONzHzzXMptItMeLeItVMvp0TdEmAzBE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

135
main.go
View File

@ -1,24 +1,19 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"text/template"
"time"
"github.com/peterbourgon/mergemap"
sprig "github.com/Masterminds/sprig/v3"
yaml "gopkg.in/yaml.v3"
s "gitlab.com/CRThaze/sugar"
htl "gitlab.com/CRThaze/helm-tmpl-lang"
s "gitlab.com/CRThaze/sugar"
)
var Version string = "0.0.0"
@ -170,114 +165,6 @@ func init() {
}
}
func checkErr(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %+v\n", err)
os.Exit(1)
}
}
func expandFiles(files []string) (expFiles []string) {
expFiles = []string{}
fileSet := s.StrSet{}
directories := []string{}
// Ensure we don't process any provided values files as templates.
for _, path := range flags.valuesFiles {
abs, err := filepath.Abs(path)
if err != nil {
fileSet.Add(path)
break
}
fileSet.Add(abs)
}
for _, path := range files {
fileInfo, err := os.Stat(path)
checkErr(err)
if fileInfo.IsDir() {
// When provided path is a directory, defer loading files from it.
directories = append(directories, path)
} else {
abs, err := filepath.Abs(path)
if err != nil {
// On failure to find the absolute path, just use the provided path.
abs = path
}
// Deduplicate files.
if ok := fileSet.AddHas(abs); !ok {
expFiles = append(expFiles, path)
}
}
// Walk each provided directory to find templates.
for _, path := range directories {
filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: %+v", err)
return nil
}
if d.IsDir() {
// Don't recurse hidden directories.
if d.Name() != "." && strings.HasPrefix(d.Name(), ".") {
return filepath.SkipDir
}
return nil
}
// Skip hidden files.
if strings.HasPrefix(d.Name(), ".") {
return nil
}
abs, err := filepath.Abs(path)
if err != nil {
// On failure to find the absolute path, just use the provided path.
abs = path
}
// Deduplicate files.
if ok := fileSet.AddHas(abs); !ok {
expFiles = append(expFiles, path)
}
return nil
})
}
}
return
}
func mergeValues() (res map[string]interface{}) {
res = map[string]interface{}{}
result := res
if flags.helm && flags.helmNestValues {
res["Values"] = map[string]interface{}{}
result = res["Values"].(map[string]interface{})
}
for _, vf := range flags.valuesFiles {
vfContent, err := os.ReadFile(vf)
checkErr(err)
if strings.HasSuffix(vf, ".json") {
err = json.Unmarshal(vfContent, &result)
checkErr(err)
} else {
err = yaml.Unmarshal(vfContent, result)
checkErr(err)
}
}
for _, v := range flags.values {
valueMap := map[string]interface{}{}
subMap := valueMap
for i, node := range v.address {
if i == len(v.address)-1 {
subMap[node] = v.v
break
}
subMap[node] = map[string]interface{}{}
subMap = subMap[node].(map[string]interface{})
}
mergemap.Merge(result, valueMap)
}
return
}
func main() {
var err error
var ht *htl.Templater
@ -307,7 +194,7 @@ func main() {
data, err = io.ReadAll(os.Stdin)
// Tell the waiting routine that we got input on STDIN.
ch <- struct{}{}
checkErr(err)
s.CheckExit(err)
if flags.helm {
ht = htl.NewTemplaterWithBytes(data, nil)
@ -316,27 +203,27 @@ func main() {
} else {
tpl, err = tpl.Parse(string(data))
}
checkErr(err)
s.CheckExit(err)
} else if len(flag.Args()) == 1 && flag.Args()[0] == "-" {
// If only a single non-flag argument is provided and it's a -, wait for STDIN indefinitely.
var data []byte
data, err = io.ReadAll(os.Stdin)
checkErr(err)
s.CheckExit(err)
if flags.helm {
ht = htl.NewTemplaterWithBytes(data, lo)
} else {
tpl, err = tpl.Parse(string(data))
}
checkErr(err)
s.CheckExit(err)
} else {
// Otherwise use the provided tplFiles/directories for templates to render.
tplFiles := expandFiles(flag.Args())
if flags.helm {
ht, err = htl.NewTemplaterWithFiles(tplFiles, lo)
checkErr(err)
s.CheckExit(err)
rendered, err := ht.Render(values)
checkErr(err)
s.CheckExit(err)
first := true
for filename, content := range rendered {
if first {
@ -358,7 +245,7 @@ func main() {
}
} else {
tpl, err = tpl.ParseFiles(tplFiles...)
checkErr(err)
s.CheckExit(err)
for i, file := range tplFiles {
if i > 0 {
out := os.Stdout
@ -373,16 +260,16 @@ func main() {
}
fmt.Fprintf(out, "%s%s\n", flags.outputFilenamePrefix, file)
}
checkErr(tpl.ExecuteTemplate(os.Stdout, filepath.Base(file), values))
s.CheckExit(tpl.ExecuteTemplate(os.Stdout, filepath.Base(file), values))
}
}
return
}
if ht != nil {
rendered, err := ht.Render(values)
checkErr(err)
s.CheckExit(err)
fmt.Fprintln(os.Stdout, rendered)
} else {
checkErr(tpl.Execute(os.Stdout, values))
s.CheckExit(tpl.Execute(os.Stdout, values))
}
}

116
prep.go Normal file
View File

@ -0,0 +1,116 @@
package main
import (
"encoding/json"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/peterbourgon/mergemap"
s "gitlab.com/CRThaze/sugar"
yaml "gopkg.in/yaml.v3"
)
func expandFiles(files []string) (expFiles []string) {
expFiles = []string{}
fileSet := s.StrSet{}
directories := []string{}
// Ensure we don't process any provided values files as templates.
for _, path := range flags.valuesFiles {
abs, err := filepath.Abs(path)
if err != nil {
fileSet.Add(path)
break
}
fileSet.Add(abs)
}
for _, path := range files {
fileInfo, err := os.Stat(path)
s.CheckExit(err)
if fileInfo.IsDir() {
// When provided path is a directory, defer loading files from it.
directories = append(directories, path)
} else {
abs, err := filepath.Abs(path)
if err != nil {
// On failure to find the absolute path, just use the provided path.
abs = path
}
// Deduplicate files.
if ok := fileSet.AddHas(abs); !ok {
expFiles = append(expFiles, path)
}
}
// Walk each provided directory to find templates.
for _, path := range directories {
filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
if err != nil {
fmt.Fprintf(os.Stderr, "WARN: %+v", err)
return nil
}
if d.IsDir() {
// Don't recurse hidden directories.
if d.Name() != "." && strings.HasPrefix(d.Name(), ".") {
return filepath.SkipDir
}
return nil
}
// Skip hidden files.
if strings.HasPrefix(d.Name(), ".") {
return nil
}
abs, err := filepath.Abs(path)
if err != nil {
// On failure to find the absolute path, just use the provided path.
abs = path
}
// Deduplicate files.
if ok := fileSet.AddHas(abs); !ok {
expFiles = append(expFiles, path)
}
return nil
})
}
}
return
}
func mergeValues() (res map[string]interface{}) {
res = map[string]interface{}{}
result := res
if flags.helm && flags.helmNestValues {
res["Values"] = map[string]interface{}{}
result = res["Values"].(map[string]interface{})
}
for _, vf := range flags.valuesFiles {
vfContent, err := os.ReadFile(vf)
s.CheckExit(err)
if strings.HasSuffix(vf, ".json") {
err = json.Unmarshal(vfContent, &result)
s.CheckExit(err)
} else {
err = yaml.Unmarshal(vfContent, result)
s.CheckExit(err)
}
}
for _, v := range flags.values {
valueMap := map[string]interface{}{}
subMap := valueMap
for i, node := range v.address {
if i == len(v.address)-1 {
subMap[node] = v.v
break
}
subMap[node] = map[string]interface{}{}
subMap = subMap[node].(map[string]interface{})
}
mergemap.Merge(result, valueMap)
}
return
}