Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

vhdirk
Copy link

@vhdirk vhdirk commented May 10, 2024

First of: Thanks for this tool, it helps me clean up all variants of CI images I'm using.

I did miss include and tpl that I was used to from Helm, so here they are :)

@vhdirk vhdirk force-pushed the includes branch 2 times, most recently from f8efde2 to 43bf613 Compare May 10, 2024 09:27
@bossm8
Copy link
Owner

bossm8 commented May 14, 2024

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. fromJson, toJson, ...)

Copy link
Owner

@bossm8 bossm8 left a 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?

Comment on lines +52 to +61
// 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()
}
Copy link
Owner

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?

Comment on lines +63 to +91
// 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
}
Copy link
Owner

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

Comment on lines +93 to +110
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
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +112 to +120
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
}
}
Copy link
Owner

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 {
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants