From 124b2e04661dfca22e724cb587d5dd7d6921014c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Fernando=20Carri=C3=B3n?= Date: Mon, 30 Oct 2023 11:27:45 +0100 Subject: [PATCH] break up main file --- main.go | 107 ----------------------------------------------- templates.go | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 107 deletions(-) create mode 100644 templates.go diff --git a/main.go b/main.go index 8c91527..7fc9496 100644 --- a/main.go +++ b/main.go @@ -1,24 +1,18 @@ 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" htl "gitlab.com/CRThaze/helm-tmpl-lang" - s "gitlab.com/CRThaze/sugar" ) var Version string = "0.0.0" @@ -177,107 +171,6 @@ func checkErr(err error) { } } -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 diff --git a/templates.go b/templates.go new file mode 100644 index 0000000..2f73856 --- /dev/null +++ b/templates.go @@ -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) + 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 +}