The current state of custom command templating covers most use cases, but it does prove to be inadequate in some very specific scenarios, where additional functions would be nice to have in template expressions.
I came across this need when I tried adding this custom command for containers:
customCommands:
containers:
- name: commit
shell: true
attach: false
command: 'docker commit {{ .Container.ID }} "{{ .Container.Name }}:{{ now | date "2006-01-02_15-04-05" }}"'
The now and date functions are provided by sprig, and the integration is fairly simple.
I would like if the templating logic included passing the template through sprig so we can have custom commands such as this.
The code change itself is very simple, since all templates are executed by the utils.ApplyTemplate function. We can use the Template.Funcs function to pass the funcmap provided by sprig:
func ApplyTemplate(str string, object interface{}) string {
var buf bytes.Buffer
- _ = template.Must(template.New("").Parse(str)).Execute(&buf, object)
+ _ = template.Must(template.New("").Funcs(sprig.FuncMap()).Parse(str)).Execute(&buf, object)
return buf.String()
}
Of course it might make sense to consider making this solution more extensible, in case of required future support for other templating engines/funcmaps, but for this basic use case, this works as expected.
A preview of this change can be found in my fork of this repository here, especially on this commit.
Manual testing confirms that this change is sufficient, its result can be seen in the custom command popup, where the templated command is showing up correctly. When using my previously provided example:
I would be more than happy to work on this myself, if we can pin down the scope of the required changes (unless my fork is already sufficient in both quality and in the changes themselves). I would also happily accept any guidance/tips.
The current state of custom command templating covers most use cases, but it does prove to be inadequate in some very specific scenarios, where additional functions would be nice to have in template expressions.
I came across this need when I tried adding this custom command for containers:
The
nowanddatefunctions are provided bysprig, and the integration is fairly simple.I would like if the templating logic included passing the template through
sprigso we can have custom commands such as this.The code change itself is very simple, since all templates are executed by the
utils.ApplyTemplatefunction. We can use theTemplate.Funcsfunction to pass the funcmap provided bysprig:func ApplyTemplate(str string, object interface{}) string { var buf bytes.Buffer - _ = template.Must(template.New("").Parse(str)).Execute(&buf, object) + _ = template.Must(template.New("").Funcs(sprig.FuncMap()).Parse(str)).Execute(&buf, object) return buf.String() }Of course it might make sense to consider making this solution more extensible, in case of required future support for other templating engines/funcmaps, but for this basic use case, this works as expected.
A preview of this change can be found in my fork of this repository here, especially on this commit.
Manual testing confirms that this change is sufficient, its result can be seen in the custom command popup, where the templated command is showing up correctly. When using my previously provided example:
I would be more than happy to work on this myself, if we can pin down the scope of the required changes (unless my fork is already sufficient in both quality and in the changes themselves). I would also happily accept any guidance/tips.