break up main file
This commit is contained in:
parent
390af14f45
commit
124b2e0466
107
main.go
107
main.go
@ -1,24 +1,18 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/peterbourgon/mergemap"
|
|
||||||
|
|
||||||
sprig "github.com/Masterminds/sprig/v3"
|
sprig "github.com/Masterminds/sprig/v3"
|
||||||
yaml "gopkg.in/yaml.v3"
|
|
||||||
|
|
||||||
htl "gitlab.com/CRThaze/helm-tmpl-lang"
|
htl "gitlab.com/CRThaze/helm-tmpl-lang"
|
||||||
s "gitlab.com/CRThaze/sugar"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var Version string = "0.0.0"
|
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() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
var ht *htl.Templater
|
var ht *htl.Templater
|
||||||
|
116
templates.go
Normal file
116
templates.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user