1
0

feat: handle new template format

This commit is contained in:
Wiktor Zykubek 2024-12-28 20:44:10 +01:00
parent dd693152c6
commit 8dbd0248f2
Signed by: wzykubek
GPG Key ID: 2221881F957D89B9
3 changed files with 44 additions and 2 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module git.brono.cloud/wzykubek/licensmith
go 1.23.4
require gopkg.in/yaml.v3 v3.0.1 // indirect

3
go.sum Normal file
View File

@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

41
main.go
View File

@ -11,6 +11,8 @@ import (
"strings"
"text/template"
"time"
"gopkg.in/yaml.v3"
)
type LicensingData struct {
@ -19,11 +21,21 @@ type LicensingData struct {
Year int
}
type LicenseData struct {
FullName 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
}
//go:embed all:templates
var Templates embed.FS
var GitConfigError = errors.New("Can't read Git config")
var NotSupportedError = errors.New("Not supported license")
var InvalidFrontMatter = errors.New("Template front matter is invalid")
func getGitUserData() (string, string, error) {
var userData [2]string
@ -59,9 +71,34 @@ func listTemplates() {
fmt.Println(strings.Join(tmplList, ", "))
}
func parseFrontMatter(tmplPath string) (LicenseData, string, error) {
data, err := Templates.ReadFile(tmplPath)
if err != nil {
panic(err)
}
parts := strings.SplitN(string(data), "---", 3)
if len(parts) < 3 {
return LicenseData{}, "", InvalidFrontMatter
}
var licenseData LicenseData
err = yaml.Unmarshal([]byte(parts[1]), &licenseData)
if err != nil {
return LicenseData{}, "", InvalidFrontMatter
}
return licenseData, strings.TrimSpace(parts[2]), nil
}
func genLicense(lcnsName string, lcnsData LicensingData, outFileName string) error {
tmplFile := "templates/" + lcnsName + ".tmpl"
tmpl, err := template.ParseFS(Templates, tmplFile)
_, lcnsBody, err := parseFrontMatter(tmplFile)
if err != nil {
panic(err)
}
tmpl, err := template.New(lcnsName).Parse(lcnsBody)
if err != nil {
return NotSupportedError
}
@ -88,7 +125,7 @@ func main() {
ListTemplates := flag.Bool("list", false, "List available licenses")
flag.Parse()
*License = strings.ToUpper(*License)
*License = strings.ToUpper(*License)
if *ListTemplates {
listTemplates()