package main import ( "errors" html "html/template" "io" "os" "strings" 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 }, "Format": func(e *Entry, format string) string { p := e.Properties() p["commit"] = strings.Replace(*e.Commit, "-dirty", "", -1) return tsprintf(format, p) }, } ) 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: return nil, errors.New("Invalid template type") } } func tsprintf(format string, params map[string]string) string { for key, val := range params { format = strings.Replace(format, "%{"+key+"}s", val, -1) } return format }