package pwreset
import (
"bytes"
"html/template"
)
//TemplateRepository is responsible for retrieving and executing template files
type TemplateRepository interface {
Template(username, fromEmail, toEmail, code string) ([]byte, error)
}
//NewFSTemplateRepository returns a file system backed template repository.
func NewFSTemplateRepository(templateFilePath string) TemplateRepository {
return &templateRepository{templateFilePath}
}
type templateRepository struct {
templateFilePath string
}
type templateData struct {
Username string
FromMail string
ToEmail string
ResetCode string
}
//Template finds and executes a template and returns the resulting []byte.
func (tr *templateRepository) Template(username, fromEmail, toEmail, code string) ([]byte, error) {
tmpl, err := template.ParseFiles(tr.templateFilePath)
if err != nil {
return nil, err
}
var b bytes.Buffer
err = tmpl.Execute(&b, templateData{username, fromEmail, toEmail, code})
if err != nil {
return nil, err
}
return b.Bytes(), nil
}