118 lines
2.1 KiB
Go
118 lines
2.1 KiB
Go
package helm
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"helm.sh/helm/v3/pkg/chart"
|
|
"helm.sh/helm/v3/pkg/chartutil"
|
|
"helm.sh/helm/v3/pkg/engine"
|
|
)
|
|
|
|
type LangOpts struct {
|
|
Strict bool
|
|
LintMode bool
|
|
EnableDNS bool
|
|
}
|
|
|
|
var defaultLangOpts = LangOpts{
|
|
Strict: true,
|
|
LintMode: false,
|
|
EnableDNS: true,
|
|
}
|
|
|
|
type Templater struct {
|
|
engine engine.Engine
|
|
chart chart.Chart
|
|
}
|
|
|
|
func (t *Templater) Render(values map[string]interface{}) (map[string]string, error) {
|
|
t.chart.Values = values
|
|
return t.engine.Render(&t.chart, chartutil.Values(values))
|
|
}
|
|
|
|
func NewTemplaterWithLangOpts(
|
|
templates map[string][]byte,
|
|
langOpts *LangOpts,
|
|
) *Templater {
|
|
if langOpts == nil {
|
|
langOpts = &defaultLangOpts
|
|
}
|
|
|
|
if templates == nil {
|
|
templates = map[string][]byte{
|
|
"": []byte{},
|
|
}
|
|
}
|
|
|
|
ht := &Templater{
|
|
engine: engine.Engine{
|
|
Strict: langOpts.Strict,
|
|
LintMode: langOpts.LintMode,
|
|
EnableDNS: langOpts.EnableDNS,
|
|
},
|
|
chart: chart.Chart{
|
|
Raw: make([]*chart.File, 0, 0),
|
|
Metadata: &chart.Metadata{},
|
|
Lock: &chart.Lock{},
|
|
Templates: make([]*chart.File, len(templates), len(templates)),
|
|
Values: nil,
|
|
Schema: make([]byte, 0, 0),
|
|
Files: make([]*chart.File, 0, 0),
|
|
},
|
|
}
|
|
|
|
var i int
|
|
for name, data := range templates {
|
|
ht.chart.Templates[i] = &chart.File{
|
|
Name: name,
|
|
Data: data,
|
|
}
|
|
i++
|
|
}
|
|
return ht
|
|
}
|
|
|
|
func NewTemplater(templates map[string][]byte) *Templater {
|
|
return NewTemplaterWithLangOpts(templates, nil)
|
|
}
|
|
|
|
func NewTemplaterWithFiles(
|
|
tplFiles []string,
|
|
langOpts *LangOpts,
|
|
) (*Templater, error) {
|
|
tpls := map[string][]byte{}
|
|
var err error
|
|
for _, tf := range tplFiles {
|
|
tpls[filepath.Base(tf)], err = os.ReadFile(tf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return NewTemplaterWithLangOpts(tpls, langOpts), nil
|
|
}
|
|
|
|
func NewTemplaterWithStr(
|
|
tplStr string,
|
|
langOpts *LangOpts,
|
|
) *Templater {
|
|
return NewTemplaterWithLangOpts(
|
|
map[string][]byte{
|
|
"": []byte(tplStr),
|
|
},
|
|
langOpts,
|
|
)
|
|
}
|
|
|
|
func NewTemplaterWithBytes(
|
|
tplBytes []byte,
|
|
langOpts *LangOpts,
|
|
) *Templater {
|
|
return NewTemplaterWithLangOpts(
|
|
map[string][]byte{
|
|
"": tplBytes,
|
|
},
|
|
langOpts,
|
|
)
|
|
}
|