fix(template): fail on invalid template path

This commit is contained in:
Kristian Borgwarth 2026-04-03 02:40:34 +02:00
parent 5e17d38349
commit 9775c27bbd
2 changed files with 13 additions and 14 deletions

View file

@ -30,7 +30,9 @@ func (h *CreateNoteHandler) Handle(ctx context.Context, raw json.RawMessage) (an
return nil, err return nil, err
} }
data, err := template.RenderTemplate(cmd.TemplatePath, cmd.Title) slug := frontmatter.Slugify(cmd.Title)
data, err := template.RenderTemplate(cmd.TemplatePath, cmd.Title, slug)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,11 +1,3 @@
package templatepackage template
import (
"os"
"strings"
"time"
)
package template package template
import ( import (
@ -15,7 +7,7 @@ import (
) )
func RenderTemplate(templatePath string, title string, slug string) ([]byte, error) { func RenderTemplate(templatePath string, title string, slug string) ([]byte, error) {
tmpl, err := readTemplate(templatePath, title) template, err := readTemplate(templatePath, title)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -26,17 +18,22 @@ func RenderTemplate(templatePath string, title string, slug string) ([]byte, err
"{{slug}}", slug, "{{slug}}", slug,
) )
return []byte(r.Replace(string(tmpl))), nil return []byte(r.Replace(string(template))), nil
} }
func readTemplate(templatePath string, title string) ([]byte, error) { func readTemplate(templatePath string, title string) ([]byte, error) {
if _, err := os.Stat(templatePath); os.IsNotExist(err) { if templatePath == "" {
return createDefaultTemplate(title), nil return createDefaultTemplate(title), nil
} }
return os.ReadFile(templatePath)
template, err := os.ReadFile(templatePath)
if err != nil {
return nil, err
}
return template, nil
} }
func createDefaultTemplate(title string) []byte { func createDefaultTemplate(title string) []byte {
return []byte("---\ntitle: " + title + "\ndate: " + time.Now().Format("2006-01-02") + "\n---\n") return []byte("---\ntitle: " + title + "\ndate: " + time.Now().Format("2006-01-02") + "\n---\n")
} }