1
0

52 lines
1005 B
Go
Raw Permalink Normal View History

2025-01-01 20:52:09 +01:00
package templates
import (
"embed"
"io/fs"
"strings"
"gopkg.in/yaml.v3"
)
//go:embed *.tmpl
var FS embed.FS
type Template struct {
Title string `yaml:"title"`
ID string `yaml:"spdx-id"`
Description string `yaml:"description"` // TODO
Permissions []string `yaml:"permissions"` // TODO
Limitations []string `yaml:"limitations"` // TODO
Conditions []string `yaml:"conditions"` // TODO
Body string
}
func Parse(path string) (Template, error) {
data, err := FS.ReadFile(path)
if err != nil {
return Template{}, err
}
parts := strings.SplitN(string(data), "---", 3)
var tmpl Template
yaml.Unmarshal([]byte(parts[1]), &tmpl)
tmpl.Body = strings.TrimSpace(parts[2])
return tmpl, nil
}
2025-01-01 21:01:11 +01:00
func List() ([]string, error) {
2025-01-01 20:52:09 +01:00
files, err := fs.ReadDir(FS, ".")
if err != nil {
2025-01-01 21:01:11 +01:00
return []string{}, err
2025-01-01 20:52:09 +01:00
}
var templates []string
for _, v := range files {
templates = append(templates, strings.Replace(v.Name(), ".tmpl", "", 1))
}
2025-01-01 21:01:11 +01:00
return templates, nil
2025-01-01 20:52:09 +01:00
}