2024-12-27 05:34:15 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-12-27 06:12:12 +01:00
|
|
|
"embed"
|
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
|
|
|
)
|
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
type InputData struct {
|
2024-12-27 05:34:15 +01:00
|
|
|
AuthorName string
|
|
|
|
AuthorEmail string
|
|
|
|
Year int
|
|
|
|
}
|
|
|
|
|
2024-12-28 20:44:10 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-12-27 06:12:12 +01:00
|
|
|
//go:embed all:templates
|
2024-12-28 20:56:07 +01:00
|
|
|
var TemplatesDir embed.FS
|
2024-12-27 06:12:12 +01:00
|
|
|
|
2024-12-27 05:34:15 +01:00
|
|
|
func getGitUserData() (string, string, error) {
|
|
|
|
var userData [2]string
|
|
|
|
for i, v := range []string{"user.name", "user.email"} {
|
|
|
|
cmd := exec.Command("git", "config", "--get", v)
|
|
|
|
out, err := cmd.Output()
|
|
|
|
if err != nil {
|
2024-12-28 21:11:35 +01:00
|
|
|
return "", "", fmt.Errorf("Can't read Git config: %w", err)
|
2024-12-27 05:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
userData[i] = strings.TrimSpace(string(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
return userData[0], userData[1], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTemplateList() []string {
|
2024-12-28 20:56:07 +01:00
|
|
|
files, err := fs.ReadDir(TemplatesDir, "templates")
|
2024-12-27 05:34:15 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tmplList []string
|
|
|
|
for _, v := range files {
|
|
|
|
tmplList = append(tmplList, strings.Replace(v.Name(), ".tmpl", "", 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmplList
|
|
|
|
}
|
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
func listLicenses() {
|
|
|
|
licList := getTemplateList()
|
|
|
|
fmt.Println(strings.Join(licList, ", "))
|
2024-12-27 05:34:15 +01:00
|
|
|
}
|
|
|
|
|
2024-12-28 20:44:10 +01:00
|
|
|
func parseFrontMatter(tmplPath string) (LicenseData, string, error) {
|
2024-12-28 20:56:07 +01:00
|
|
|
data, err := TemplatesDir.ReadFile(tmplPath)
|
2024-12-28 20:44:10 +01:00
|
|
|
if err != nil {
|
2024-12-28 21:11:35 +01:00
|
|
|
return LicenseData{}, "", err
|
2024-12-28 20:44:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(string(data), "---", 3)
|
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
var licData LicenseData
|
2024-12-28 21:11:35 +01:00
|
|
|
yaml.Unmarshal([]byte(parts[1]), &licData)
|
2024-12-28 20:44:10 +01:00
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
return licData, strings.TrimSpace(parts[2]), nil
|
2024-12-28 20:44:10 +01:00
|
|
|
}
|
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
func genLicense(licName string, inputData InputData, outFileName string) error {
|
|
|
|
tmplPath := "templates/" + licName + ".tmpl"
|
|
|
|
_, lcnsBody, err := parseFrontMatter(tmplPath)
|
|
|
|
if err != nil {
|
2024-12-28 21:11:35 +01:00
|
|
|
return err
|
2024-12-28 20:56:07 +01:00
|
|
|
}
|
2024-12-28 20:44:10 +01:00
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
tmpl, err := template.New(licName).Parse(lcnsBody)
|
2024-12-27 05:34:15 +01:00
|
|
|
if err != nil {
|
2024-12-28 21:11:35 +01:00
|
|
|
return fmt.Errorf("Not supported license")
|
2024-12-27 05:34:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
outFile, err := os.Create(outFileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
err = tmpl.Execute(outFile, inputData)
|
2024-12-27 05:34:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
OutputFile := flag.String("output", "LICENSE", "Specify different output file")
|
2024-12-28 20:56:07 +01:00
|
|
|
LicenseName := flag.String("license", "", "Choose a license")
|
2024-12-28 21:16:00 +01:00
|
|
|
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
|
|
|
*LicenseName = strings.ToUpper(*LicenseName)
|
2024-12-28 02:16:58 +01:00
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
if *ListLicenses {
|
|
|
|
listLicenses()
|
2024-12-27 05:34:15 +01:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
if *LicenseName == "" {
|
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)
|
|
|
|
}
|
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
if *AuthorName == "" || *AuthorEmail == "" {
|
2024-12-27 05:34:15 +01:00
|
|
|
var err error
|
2024-12-28 20:56:07 +01:00
|
|
|
*AuthorName, *AuthorEmail, err = getGitUserData()
|
2024-12-27 05:34:15 +01:00
|
|
|
if err != nil {
|
2024-12-28 21:11:35 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
inputData := InputData{
|
|
|
|
AuthorName: *AuthorName,
|
|
|
|
AuthorEmail: *AuthorEmail,
|
2024-12-27 05:34:15 +01:00
|
|
|
Year: time.Now().Year(),
|
|
|
|
}
|
|
|
|
|
2024-12-28 20:56:07 +01:00
|
|
|
err := genLicense(*LicenseName, inputData, *OutputFile)
|
2024-12-27 05:34:15 +01:00
|
|
|
if err != nil {
|
2024-12-28 21:11:35 +01:00
|
|
|
fmt.Printf("Error: There is no '%s' license\n\nAvailable licenses:\n", *LicenseName)
|
|
|
|
listLicenses()
|
|
|
|
os.Exit(2)
|
2024-12-27 05:34:15 +01:00
|
|
|
}
|
|
|
|
}
|