diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-09-03 16:01:20 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-09-03 16:01:20 +0200 |
commit | a21602a450217333a27419d8168865b21fae6e7e (patch) | |
tree | bcb454130105de02dd97864dc1c977bf30044c38 /template.go | |
parent | 3b21449468a1b20b3ff706fe00a04556a804e627 (diff) | |
download | apkdoc-a21602a450217333a27419d8168865b21fae6e7e.tar.gz apkdoc-a21602a450217333a27419d8168865b21fae6e7e.tar.bz2 apkdoc-a21602a450217333a27419d8168865b21fae6e7e.zip |
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.
Diffstat (limited to 'template.go')
-rw-r--r-- | template.go | 44 |
1 files changed, 44 insertions, 0 deletions
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") + } +} |