2023-10-12 16:35:46 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2023-10-13 06:22:36 -04:00
|
|
|
"time"
|
2023-10-12 16:35:46 -04:00
|
|
|
|
2023-10-13 07:07:10 -04:00
|
|
|
"github.com/peterbourgon/mergemap"
|
|
|
|
|
2023-10-12 16:35:46 -04:00
|
|
|
sprig "github.com/Masterminds/sprig/v3"
|
2023-10-13 06:22:36 -04:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2023-10-12 16:35:46 -04:00
|
|
|
)
|
|
|
|
|
2023-10-13 08:01:12 -04:00
|
|
|
type StrSet map[string]struct{}
|
|
|
|
|
|
|
|
func (s *StrSet) Add(str string) {
|
|
|
|
(*s)[str] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StrSet) Has(str string) bool {
|
|
|
|
if _, ok := (*s)[str]; ok {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StrSet) AddHas(str string) bool {
|
|
|
|
if ok := s.Has(str); ok {
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
s.Add(str)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:35:46 -04:00
|
|
|
type valFileSlice []string
|
|
|
|
|
|
|
|
func (v *valFileSlice) String() string {
|
|
|
|
return fmt.Sprintf("%v", *v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *valFileSlice) Set(value string) error {
|
|
|
|
*v = append(*v, value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type valEnt struct {
|
|
|
|
address []string
|
|
|
|
v string
|
|
|
|
}
|
|
|
|
|
|
|
|
type valSlice []valEnt
|
|
|
|
|
|
|
|
func (v *valSlice) String() string {
|
|
|
|
return fmt.Sprintf("%v", *v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *valSlice) Set(value string) error {
|
|
|
|
pieces := strings.Split(value, "=")
|
|
|
|
if len(pieces) < 2 {
|
|
|
|
return fmt.Errorf("Invalid value setting.")
|
|
|
|
}
|
|
|
|
*v = append(*v, valEnt{
|
|
|
|
strings.Split(pieces[0], "."),
|
|
|
|
pieces[1],
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var flags struct {
|
|
|
|
valuesFiles valFileSlice
|
|
|
|
values valSlice
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.Var(&flags.values, "set", "")
|
|
|
|
flag.Var(&flags.valuesFiles, "f", "")
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "ERROR: %+v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandFiles(files []string) (expFiles []string) {
|
|
|
|
expFiles = []string{}
|
2023-10-13 08:01:12 -04:00
|
|
|
fileSet := 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)
|
|
|
|
}
|
|
|
|
|
2023-10-12 16:35:46 -04:00
|
|
|
for _, path := range files {
|
|
|
|
fileInfo, err := os.Stat(path)
|
|
|
|
checkErr(err)
|
|
|
|
|
|
|
|
if fileInfo.IsDir() {
|
2023-10-13 08:01:12 -04:00
|
|
|
// 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 {
|
2023-10-12 16:35:46 -04:00
|
|
|
filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "WARN: %+v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if d.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-13 08:01:12 -04:00
|
|
|
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)
|
|
|
|
}
|
2023-10-12 16:35:46 -04:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeValues() (result map[string]interface{}) {
|
|
|
|
result = map[string]interface{}{}
|
|
|
|
for _, vf := range flags.valuesFiles {
|
2023-10-13 07:07:10 -04:00
|
|
|
vfContent, err := os.ReadFile(vf)
|
2023-10-12 16:35:46 -04:00
|
|
|
checkErr(err)
|
2023-10-13 06:22:36 -04:00
|
|
|
if strings.HasSuffix(vf, ".json") {
|
2023-10-13 07:07:10 -04:00
|
|
|
err = json.Unmarshal(vfContent, &result)
|
2023-10-13 06:22:36 -04:00
|
|
|
checkErr(err)
|
|
|
|
} else {
|
|
|
|
err = yaml.Unmarshal(vfContent, result)
|
|
|
|
checkErr(err)
|
|
|
|
}
|
2023-10-12 16:35:46 -04:00
|
|
|
}
|
2023-10-13 07:07:10 -04:00
|
|
|
for _, v := range flags.values {
|
|
|
|
valueMap := map[string]interface{}{}
|
|
|
|
subMap := valueMap
|
|
|
|
for i, node := range v.address {
|
2023-10-12 16:35:46 -04:00
|
|
|
if i == len(v.address)-1 {
|
|
|
|
subMap[node] = v.v
|
2023-10-13 07:07:10 -04:00
|
|
|
break
|
2023-10-12 16:35:46 -04:00
|
|
|
}
|
2023-10-13 07:07:10 -04:00
|
|
|
subMap[node] = map[string]interface{}{}
|
|
|
|
subMap = subMap[node].(map[string]interface{})
|
2023-10-12 16:35:46 -04:00
|
|
|
}
|
2023-10-13 07:07:10 -04:00
|
|
|
mergemap.Merge(result, valueMap)
|
2023-10-12 16:35:46 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var err error
|
|
|
|
tpl := template.New("base").Funcs(sprig.FuncMap())
|
2023-10-13 08:01:12 -04:00
|
|
|
|
2023-10-13 06:22:36 -04:00
|
|
|
if len(flag.Args()) == 0 {
|
2023-10-13 08:01:12 -04:00
|
|
|
// With no provided arguments read from STDIN if anything is immediately available through it.
|
|
|
|
// otherwise print the usage and exit.
|
2023-10-13 06:22:36 -04:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
return
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
var data []byte
|
|
|
|
data, err = io.ReadAll(os.Stdin)
|
2023-10-13 08:01:12 -04:00
|
|
|
// Tell the waiting routine that we got input on STDIN.
|
2023-10-13 06:22:36 -04:00
|
|
|
ch <- struct{}{}
|
|
|
|
checkErr(err)
|
|
|
|
tpl, err = tpl.Parse(string(data))
|
|
|
|
checkErr(err)
|
|
|
|
} else if len(flag.Args()) == 1 && flag.Args()[0] == "-" {
|
2023-10-13 08:01:12 -04:00
|
|
|
// If only a single non-flag argument is provided and it's a -, wait for STDIN indefinitely.
|
2023-10-12 16:35:46 -04:00
|
|
|
var data []byte
|
|
|
|
data, err = io.ReadAll(os.Stdin)
|
|
|
|
checkErr(err)
|
|
|
|
tpl, err = tpl.Parse(string(data))
|
|
|
|
checkErr(err)
|
|
|
|
} else {
|
2023-10-13 08:01:12 -04:00
|
|
|
// Otherwise use the provided files/directories for templates to render.
|
|
|
|
// fmt.Fprintf(os.Stderr, "%+v\n", expandFiles(flag.Args()))
|
2023-10-12 16:35:46 -04:00
|
|
|
tpl, err = tpl.ParseFiles(expandFiles(flag.Args())...)
|
|
|
|
checkErr(err)
|
|
|
|
}
|
2023-10-13 08:01:12 -04:00
|
|
|
// fmt.Fprintf(os.Stderr, "%+v\n", mergeValues())
|
2023-10-12 16:35:46 -04:00
|
|
|
tpl.Execute(os.Stdout, mergeValues())
|
|
|
|
}
|