1
0
licensmith/main.go

185 lines
4.0 KiB
Go
Raw Normal View History

2024-12-27 05:34:15 +01:00
package main
import (
"bytes"
2024-12-27 06:12:12 +01:00
"embed"
"errors"
2024-12-27 05:34:15 +01:00
"flag"
"fmt"
2024-12-27 06:12:12 +01:00
"io/fs"
2024-12-27 05:34:15 +01:00
"os"
"os/exec"
"strings"
"text/template"
"time"
2024-12-28 20:44:10 +01:00
"gopkg.in/yaml.v3"
2024-12-27 05:34:15 +01:00
)
//go:embed all:templates
var TemplatesDir embed.FS
2024-12-27 05:34:15 +01:00
type LicenseTemplate struct {
Title string `yaml:"title"`
2024-12-28 20:44:10 +01:00
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
2024-12-28 20:44:10 +01:00
}
type LicenseContext struct {
AuthorName string
AuthorEmail string
Year int
}
2024-12-27 06:12:12 +01:00
type Licenser struct {
LicenseID string
LicenseContext LicenseContext
OutputFile string
licenseBody string
}
2024-12-27 05:34:15 +01:00
func NewLicenseContext(authorName string, authorEmail string) (LicenseContext, error) {
var err error
if authorName == "" {
authorName, err = gitUserData("user.name")
}
if authorEmail == "" {
authorEmail, err = gitUserData("user.email")
}
if err != nil {
return LicenseContext{}, err
2024-12-27 05:34:15 +01:00
}
return LicenseContext{
AuthorName: authorName,
AuthorEmail: authorEmail,
Year: time.Now().Year(),
}, nil
2024-12-27 05:34:15 +01:00
}
func (l *Licenser) ParseTemplate() (LicenseTemplate, error) {
licenseID := strings.ToUpper(l.LicenseID)
tmplPath := "templates/" + licenseID + ".tmpl"
data, err := TemplatesDir.ReadFile(tmplPath)
2024-12-27 05:34:15 +01:00
if err != nil {
return LicenseTemplate{}, err
2024-12-27 05:34:15 +01:00
}
parts := strings.SplitN(string(data), "---", 3)
2024-12-27 05:34:15 +01:00
var licenseTmpl LicenseTemplate
yaml.Unmarshal([]byte(parts[1]), &licenseTmpl)
licenseTmpl.Body = strings.TrimSpace(parts[2])
2024-12-27 05:34:15 +01:00
return licenseTmpl, nil
2024-12-27 05:34:15 +01:00
}
func (l *Licenser) Generate() error {
license, err := l.ParseTemplate()
2024-12-28 20:44:10 +01:00
if err != nil {
return errors.New("Not supported license")
2024-12-28 20:44:10 +01:00
}
tmpl, _ := template.New(l.LicenseID).Parse(license.Body)
var output bytes.Buffer
tmpl.Execute(&output, l.LicenseContext)
2024-12-28 20:44:10 +01:00
l.licenseBody = output.String()
2024-12-28 20:44:10 +01:00
return nil
2024-12-28 20:44:10 +01:00
}
func (l *Licenser) WriteFile() error {
outFile, err := os.Create(l.OutputFile)
2024-12-28 20:56:07 +01:00
if err != nil {
2024-12-28 21:11:35 +01:00
return err
2024-12-28 20:56:07 +01:00
}
defer outFile.Close()
2024-12-28 20:44:10 +01:00
if _, err := outFile.WriteString(l.licenseBody); err != nil {
return err
2024-12-27 05:34:15 +01:00
}
return nil
}
func gitUserData(key string) (string, error) {
cmd := exec.Command("git", "config", "--get", key)
out, err := cmd.Output()
2024-12-27 05:34:15 +01:00
if err != nil {
return "", errors.New("Can't read Git config")
2024-12-27 05:34:15 +01:00
}
value := strings.TrimSpace(string(out))
return value, nil
}
func templateList() []string {
files, err := fs.ReadDir(TemplatesDir, "templates")
2024-12-27 05:34:15 +01:00
if err != nil {
panic(err)
2024-12-27 05:34:15 +01:00
}
var tmplList []string
for _, v := range files {
tmplList = append(tmplList, strings.Replace(v.Name(), ".tmpl", "", 1))
}
return tmplList
}
func listLicenses() {
tmplList := templateList()
fmt.Println(strings.Join(tmplList, ", "))
2024-12-27 05:34:15 +01:00
}
func main() {
OutputFile := flag.String("output", "LICENSE", "Specify different output file")
LicenseID := flag.String("license", "", "Specify license by SPDX ID (e.g. BSD-3-Clause)")
AuthorName := flag.String("name", "", "Set the author name (read from Git by default)")
AuthorEmail := flag.String("email", "", "Set the author email (read from Git by default)")
2024-12-28 20:56:07 +01:00
ListLicenses := flag.Bool("list", false, "List available licenses")
2024-12-27 05:34:15 +01:00
flag.Parse()
2024-12-28 20:56:07 +01:00
if *ListLicenses {
listLicenses()
2024-12-27 05:34:15 +01:00
os.Exit(0)
}
if *LicenseID == "" {
2024-12-27 05:34:15 +01:00
fmt.Printf("Error: No license specified\n\nUse --license LICENSE\n\nAvailable licenses:\n")
2024-12-28 20:56:07 +01:00
listLicenses()
2024-12-27 05:34:15 +01:00
os.Exit(1)
}
licenseCtx, err := NewLicenseContext(*AuthorName, *AuthorEmail)
if err != nil && err.Error() == "Can't read Git config" {
fmt.Printf(
"Error: Can't read Git config.\n\nUse --name \"NAME\" and --email EMAIL instead.\n",
)
os.Exit(3)
2024-12-27 05:34:15 +01:00
}
licenser := Licenser{
LicenseID: *LicenseID,
LicenseContext: licenseCtx,
OutputFile: *OutputFile,
2024-12-27 05:34:15 +01:00
}
err = licenser.Generate()
if err != nil && err.Error() == "Not supported license" {
fmt.Printf("Error: There is no '%s' license\n\nAvailable licenses:\n", *LicenseID)
2024-12-28 21:11:35 +01:00
listLicenses()
os.Exit(2)
2024-12-27 05:34:15 +01:00
}
if err = licenser.WriteFile(); err != nil {
panic(err)
}
2024-12-27 05:34:15 +01:00
}