break up main file

This commit is contained in:
Diego Fernando Carrión 2023-10-30 11:27:45 +01:00
parent 390af14f45
commit 124b2e0466
No known key found for this signature in database
GPG Key ID: 811EF2E03998BFC4
2 changed files with 116 additions and 107 deletions

107
main.go
View File

@ -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

116
templates.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)
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
}