From a21602a450217333a27419d8168865b21fae6e7e Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 3 Sep 2023 16:01:20 +0200 Subject: feat: Add option for inputing template Now a template file is required to run the cli command. That gives the user an option to provide its own template file. An default will be provided later. --- template.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 template.go (limited to 'template.go') diff --git a/template.go b/template.go new file mode 100644 index 0000000..503af64 --- /dev/null +++ b/template.go @@ -0,0 +1,44 @@ +package main + +import ( + html "html/template" + "io" + "os" + text "text/template" +) + +type Templater interface { + Execute(wr io.Writer, data any) error +} + +var ( + templateFunc = map[string]any{ + "DerefI": func(i *int) int { return *i }, + "DerefS": func(i *string) string { return *i }, + } +) + +func GetTemplate(templateType, filePath string) (Templater, error) { + file, err := os.Open(filePath) + if err != nil { + return nil, err + } + + tmpl, err := io.ReadAll(file) + if err != nil { + return nil, err + } + + switch templateType { + case "text": + return text.New("text"). + Funcs(templateFunc). + Parse(string(tmpl)) + case "html": + return html.New("html"). + Funcs(templateFunc). + Parse(string(tmpl)) + default: + panic("Invalid template-type") + } +} -- cgit v1.2.3