-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for jinja2 like templates
- Loading branch information
Showing
15 changed files
with
161 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package templateprovider | ||
|
||
import ( | ||
"github.com/flosch/pongo2/v6" | ||
"github.com/palantir/stacktrace" | ||
"io" | ||
) | ||
|
||
type JinjaTemplate struct { | ||
templateFilePath string | ||
Check failure on line 10 in internal/templateprovider/jinjatemplate.go GitHub Actions / Golangci-lint
|
||
templateContent string | ||
variables map[string]interface{} | ||
pongo2Template *pongo2.Template | ||
} | ||
|
||
func NewJinjaTemplate(name string) *JinjaTemplate { | ||
Check warning on line 16 in internal/templateprovider/jinjatemplate.go GitHub Actions / Golangci-lint
|
||
return &JinjaTemplate{} | ||
} | ||
|
||
func (t *JinjaTemplate) FromString(templateContent string) error { | ||
t.templateContent = templateContent | ||
tpl, err := pongo2.FromString(t.templateContent) | ||
if err != nil { | ||
return stacktrace.Propagate(err, "failed to parse template") | ||
} | ||
t.pongo2Template = tpl | ||
return nil | ||
} | ||
|
||
func (t *JinjaTemplate) FromFile(templateFilePath string) error { | ||
tpl, err := pongo2.FromFile(templateFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
t.pongo2Template = tpl | ||
return nil | ||
} | ||
|
||
func (t *JinjaTemplate) Execute(fp io.Writer, withVariables map[string]interface{}) error { | ||
t.variables = withVariables | ||
var context = make(pongo2.Context) | ||
for k, v := range t.variables { | ||
context[k] = v.(any) | ||
Check failure on line 43 in internal/templateprovider/jinjatemplate.go GitHub Actions / Golangci-lint
|
||
} | ||
renderedContent, err := t.pongo2Template.Execute(context) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = fp.Write([]byte(renderedContent)) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (t *JinjaTemplate) WithErrorOnMissingKey(_ bool) { | ||
} | ||
|
||
func (t *JinjaTemplate) IsMissingKeyError(err error) bool { | ||
return err.Error() == "TokenError" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package templateprovider | ||
|
||
func ByType(templateType TemplateType, name string) TemplateService { | ||
var ts TemplateService | ||
switch templateType { | ||
case SprigTemplateService: | ||
ts = NewSprigTemplate(name) | ||
case JinjaTemplateService: | ||
ts = NewJinjaTemplate(name) | ||
} | ||
return ts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package templateprovider | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
type TemplateService interface { | ||
// FromFile Load template from file | ||
FromFile(path string) error | ||
// FromString Load template from string | ||
FromString(templateContent string) error | ||
// Execute Execute the template | ||
Execute(fp io.Writer, withVariables map[string]interface{}) error | ||
// WithErrorOnMissingKey Set error on missing key | ||
WithErrorOnMissingKey(key bool) | ||
IsMissingKeyError(err error) bool | ||
} | ||
|
||
type TemplateType int | ||
|
||
const ( | ||
SprigTemplateService TemplateType = iota | ||
JinjaTemplateService | ||
) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.