-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add include and tpl template functions from Helm #17
base: main
Are you sure you want to change the base?
Conversation
f8efde2
to
43bf613
Compare
Thank your for your contribution! I'm happy that the tool is helpful. I will look at this in more detail in a few weeks. But at a first glance I see that now there are duplicated functions in the code which are already present in sprig an which I would not want to maintain too. Maybe you could check this already? (E.g. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the contribution, could you please take a look at the comments and in general document the added functions in the readme?
// https://github.com/helm/helm/blob/518c69281f42d9b3a5cf99bd959a08e048093e20/pkg/engine/funcs.go#L125 | ||
func toTOML(v interface{}) string { | ||
b := bytes.NewBuffer(nil) | ||
e := toml.NewEncoder(b) | ||
err := e.Encode(v) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
return b.String() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't think toml is really useful here, the templater does not support toml especially since it is only toTOML, could you highlight the use-case?
// https://github.com/helm/helm/blob/518c69281f42d9b3a5cf99bd959a08e048093e20/pkg/engine/funcs.go#L139 | ||
func toJSON(v interface{}) string { | ||
data, err := json.Marshal(v) | ||
if err != nil { | ||
// Swallow errors inside of a template. | ||
return "" | ||
} | ||
return string(data) | ||
} | ||
|
||
// https://github.com/helm/helm/blob/518c69281f42d9b3a5cf99bd959a08e048093e20/pkg/engine/funcs.go#L154 | ||
func fromJSON(str string) map[string]interface{} { | ||
m := make(map[string]interface{}) | ||
|
||
if err := json.Unmarshal([]byte(str), &m); err != nil { | ||
m["Error"] = err.Error() | ||
} | ||
return m | ||
} | ||
|
||
// https://github.com/helm/helm/blob/518c69281f42d9b3a5cf99bd959a08e048093e20/pkg/engine/funcs.go#L169 | ||
func fromJSONArray(str string) []interface{} { | ||
a := []interface{}{} | ||
|
||
if err := json.Unmarshal([]byte(str), &a); err != nil { | ||
a = []interface{}{err.Error()} | ||
} | ||
return a | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dito, also these functions can be found in sprig: http://masterminds.github.io/sprig/defaults.html
func mergeOverwriteAppendSlice(dst map[string]interface{}, srcs ...map[string]interface{}) interface{} { | ||
for _, src := range srcs { | ||
if err := mergo.MergeWithOverwrite(&dst, src, mergo.WithAppendSlice); err != nil { | ||
// Swallow errors inside of a template. | ||
return "" | ||
} | ||
} | ||
return dst | ||
} | ||
|
||
func mustMergeOverwriteAppendSlice(dst map[string]interface{}, srcs ...map[string]interface{}) (interface{}, error) { | ||
for _, src := range srcs { | ||
if err := mergo.MergeWithOverwrite(&dst, src, mergo.WithAppendSlice); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return dst, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also available in sprig: http://masterminds.github.io/sprig/lists.html
func definedTemplates(t *template.Template) func() []string { | ||
return func() []string { | ||
var names []string | ||
for _, tpl := range t.Templates() { | ||
names = append(names, tpl.Name()) | ||
} | ||
return names | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please elaborate and document the use(-case) of this function
@@ -49,6 +217,31 @@ func ParseTemplate( | |||
return tpl | |||
} | |||
|
|||
// Loads the includable template definitions. | |||
func InitTemplateDirs(template *template.Template, templateDirs []string) *template.Template { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably does not need to be exported since it's only used in utils now.
First of: Thanks for this tool, it helps me clean up all variants of CI images I'm using.
I did miss
include
andtpl
that I was used to from Helm, so here they are :)