diff --git a/git_utils.go b/git_utils.go new file mode 100644 index 0000000..94ce355 --- /dev/null +++ b/git_utils.go @@ -0,0 +1,18 @@ +package main + +import ( + "errors" + "os/exec" + "strings" +) + +func gitUserData(key string) (string, error) { + cmd := exec.Command("git", "config", "--get", key) + out, err := cmd.Output() + if err != nil { + return "", errors.New("Can't read Git config") + } + + value := strings.TrimSpace(string(out)) + return value, nil +} diff --git a/license_context.go b/license_context.go new file mode 100644 index 0000000..b8a885d --- /dev/null +++ b/license_context.go @@ -0,0 +1,30 @@ +package main + +import ( + "time" +) + +type LicenseContext struct { + AuthorName string + AuthorEmail string + Year int +} + +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 + } + + return LicenseContext{ + AuthorName: authorName, + AuthorEmail: authorEmail, + Year: time.Now().Year(), + }, nil +} diff --git a/licenser.go b/licenser.go new file mode 100644 index 0000000..4fd59f4 --- /dev/null +++ b/licenser.go @@ -0,0 +1,65 @@ +package main + +import ( + "bytes" + "errors" + "os" + "strings" + "text/template" + + "gopkg.in/yaml.v3" +) + +type Licenser struct { + LicenseID string + LicenseContext LicenseContext + OutputFile string + licenseBody string +} + +func (l *Licenser) ParseTemplate() (LicenseTemplate, error) { + licenseID := strings.ToUpper(l.LicenseID) + tmplPath := "templates/" + licenseID + ".tmpl" + data, err := TemplatesDir.ReadFile(tmplPath) + if err != nil { + return LicenseTemplate{}, err + } + + parts := strings.SplitN(string(data), "---", 3) + + var licenseTmpl LicenseTemplate + yaml.Unmarshal([]byte(parts[1]), &licenseTmpl) + licenseTmpl.Body = strings.TrimSpace(parts[2]) + + return licenseTmpl, nil +} + +func (l *Licenser) Generate() error { + license, err := l.ParseTemplate() + if err != nil { + return errors.New("Not supported license") + } + + tmpl, _ := template.New(l.LicenseID).Parse(license.Body) + + var output bytes.Buffer + tmpl.Execute(&output, l.LicenseContext) + + l.licenseBody = output.String() + + return nil +} + +func (l *Licenser) WriteFile() error { + outFile, err := os.Create(l.OutputFile) + if err != nil { + return err + } + defer outFile.Close() + + if _, err := outFile.WriteString(l.licenseBody); err != nil { + return err + } + + return nil +} diff --git a/main.go b/main.go index 8b2fe6e..f52437e 100644 --- a/main.go +++ b/main.go @@ -1,143 +1,11 @@ package main import ( - "bytes" - "embed" - "errors" "flag" "fmt" - "io/fs" "os" - "os/exec" - "strings" - "text/template" - "time" - - "gopkg.in/yaml.v3" ) -//go:embed all:templates -var TemplatesDir embed.FS - -type LicenseTemplate 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 -} - -type LicenseContext struct { - AuthorName string - AuthorEmail string - Year int -} - -type Licenser struct { - LicenseID string - LicenseContext LicenseContext - OutputFile string - licenseBody string -} - -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 - } - - return LicenseContext{ - AuthorName: authorName, - AuthorEmail: authorEmail, - Year: time.Now().Year(), - }, nil -} - -func (l *Licenser) ParseTemplate() (LicenseTemplate, error) { - licenseID := strings.ToUpper(l.LicenseID) - tmplPath := "templates/" + licenseID + ".tmpl" - data, err := TemplatesDir.ReadFile(tmplPath) - if err != nil { - return LicenseTemplate{}, err - } - - parts := strings.SplitN(string(data), "---", 3) - - var licenseTmpl LicenseTemplate - yaml.Unmarshal([]byte(parts[1]), &licenseTmpl) - licenseTmpl.Body = strings.TrimSpace(parts[2]) - - return licenseTmpl, nil -} - -func (l *Licenser) Generate() error { - license, err := l.ParseTemplate() - if err != nil { - return errors.New("Not supported license") - } - - tmpl, _ := template.New(l.LicenseID).Parse(license.Body) - - var output bytes.Buffer - tmpl.Execute(&output, l.LicenseContext) - - l.licenseBody = output.String() - - return nil -} - -func (l *Licenser) WriteFile() error { - outFile, err := os.Create(l.OutputFile) - if err != nil { - return err - } - defer outFile.Close() - - if _, err := outFile.WriteString(l.licenseBody); err != nil { - return err - } - - return nil -} - -func gitUserData(key string) (string, error) { - cmd := exec.Command("git", "config", "--get", key) - out, err := cmd.Output() - if err != nil { - return "", errors.New("Can't read Git config") - } - - value := strings.TrimSpace(string(out)) - return value, nil -} - -func templateList() []string { - files, err := fs.ReadDir(TemplatesDir, "templates") - if err != nil { - panic(err) - } - - 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, ", ")) -} - 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)") diff --git a/templates.go b/templates.go new file mode 100644 index 0000000..e6251fa --- /dev/null +++ b/templates.go @@ -0,0 +1,39 @@ +package main + +import ( + "embed" + "io/fs" + "strings" +) + +//go:embed all:templates +var TemplatesDir embed.FS + +type LicenseTemplate 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 listTemplates() []string { + files, err := fs.ReadDir(TemplatesDir, "templates") + if err != nil { + panic(err) + } + + var tmplList []string + for _, v := range files { + tmplList = append(tmplList, strings.Replace(v.Name(), ".tmpl", "", 1)) + } + + return tmplList +} + +func listLicenses() { + tmplList := listTemplates() + println(strings.Join(tmplList, ", ")) +}