1
0

style: rename some identifiers

This commit is contained in:
Wiktor Zykubek 2024-12-28 20:56:07 +01:00
parent 582aa6845c
commit 80d32c4c2d
Signed by: wzykubek
GPG Key ID: 2221881F957D89B9

70
main.go
View File

@ -15,7 +15,7 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
type LicensingData struct { type InputData struct {
AuthorName string AuthorName string
AuthorEmail string AuthorEmail string
Year int Year int
@ -31,7 +31,7 @@ type LicenseData struct {
} }
//go:embed all:templates //go:embed all:templates
var Templates embed.FS var TemplatesDir embed.FS
var GitConfigError = errors.New("Can't read Git config") var GitConfigError = errors.New("Can't read Git config")
var NotSupportedError = errors.New("Not supported license") var NotSupportedError = errors.New("Not supported license")
@ -53,7 +53,7 @@ func getGitUserData() (string, string, error) {
} }
func getTemplateList() []string { func getTemplateList() []string {
files, err := fs.ReadDir(Templates, "templates") files, err := fs.ReadDir(TemplatesDir, "templates")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -66,13 +66,13 @@ func getTemplateList() []string {
return tmplList return tmplList
} }
func listTemplates() { func listLicenses() {
tmplList := getTemplateList() licList := getTemplateList()
fmt.Println(strings.Join(tmplList, ", ")) fmt.Println(strings.Join(licList, ", "))
} }
func parseFrontMatter(tmplPath string) (LicenseData, string, error) { func parseFrontMatter(tmplPath string) (LicenseData, string, error) {
data, err := Templates.ReadFile(tmplPath) data, err := TemplatesDir.ReadFile(tmplPath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -82,23 +82,23 @@ func parseFrontMatter(tmplPath string) (LicenseData, string, error) {
return LicenseData{}, "", InvalidFrontMatter return LicenseData{}, "", InvalidFrontMatter
} }
var licenseData LicenseData var licData LicenseData
err = yaml.Unmarshal([]byte(parts[1]), &licenseData) err = yaml.Unmarshal([]byte(parts[1]), &licData)
if err != nil { if err != nil {
return LicenseData{}, "", InvalidFrontMatter return LicenseData{}, "", InvalidFrontMatter
} }
return licenseData, strings.TrimSpace(parts[2]), nil return licData, strings.TrimSpace(parts[2]), nil
} }
func genLicense(lcnsName string, lcnsData LicensingData, outFileName string) error { func genLicense(licName string, inputData InputData, outFileName string) error {
tmplFile := "templates/" + lcnsName + ".tmpl" tmplPath := "templates/" + licName + ".tmpl"
_, lcnsBody, err := parseFrontMatter(tmplFile) _, lcnsBody, err := parseFrontMatter(tmplPath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
tmpl, err := template.New(lcnsName).Parse(lcnsBody) tmpl, err := template.New(licName).Parse(lcnsBody)
if err != nil { if err != nil {
return NotSupportedError return NotSupportedError
} }
@ -109,7 +109,7 @@ func genLicense(lcnsName string, lcnsData LicensingData, outFileName string) err
} }
defer outFile.Close() defer outFile.Close()
err = tmpl.Execute(outFile, lcnsData) err = tmpl.Execute(outFile, inputData)
if err != nil { if err != nil {
return err return err
} }
@ -119,28 +119,28 @@ func genLicense(lcnsName string, lcnsData LicensingData, outFileName string) err
func main() { func main() {
OutputFile := flag.String("output", "LICENSE", "Specify different output file") OutputFile := flag.String("output", "LICENSE", "Specify different output file")
License := flag.String("license", "", "Choose a license") LicenseName := flag.String("license", "", "Choose a license")
authorName := flag.String("name", "", "Set the author name") AuthorName := flag.String("name", "", "Set the author name")
authorEmail := flag.String("email", "", "Set the author email") AuthorEmail := flag.String("email", "", "Set the author email")
ListTemplates := flag.Bool("list", false, "List available licenses") ListLicenses := flag.Bool("list", false, "List available licenses")
flag.Parse() flag.Parse()
*License = strings.ToUpper(*License) *LicenseName = strings.ToUpper(*LicenseName)
if *ListTemplates { if *ListLicenses {
listTemplates() listLicenses()
os.Exit(0) os.Exit(0)
} }
if *License == "" { if *LicenseName == "" {
fmt.Printf("Error: No license specified\n\nUse --license LICENSE\n\nAvailable licenses:\n") fmt.Printf("Error: No license specified\n\nUse --license LICENSE\n\nAvailable licenses:\n")
listTemplates() listLicenses()
os.Exit(1) os.Exit(1)
} }
if *authorName == "" || *authorEmail == "" { if *AuthorName == "" || *AuthorEmail == "" {
var err error var err error
*authorName, *authorEmail, err = getGitUserData() *AuthorName, *AuthorEmail, err = getGitUserData()
if err != nil { if err != nil {
if errors.Is(err, GitConfigError) { if errors.Is(err, GitConfigError) {
fmt.Printf( fmt.Printf(
@ -151,17 +151,17 @@ func main() {
} }
} }
lcnsData := LicensingData{ inputData := InputData{
AuthorName: *authorName, AuthorName: *AuthorName,
AuthorEmail: *authorEmail, AuthorEmail: *AuthorEmail,
Year: time.Now().Year(), Year: time.Now().Year(),
} }
err := genLicense(*License, lcnsData, *OutputFile) err := genLicense(*LicenseName, inputData, *OutputFile)
if err != nil { if err != nil {
if errors.Is(err, NotSupportedError) { if errors.Is(err, NotSupportedError) {
fmt.Printf("Error: There is no '%s' license\n\nAvailable licenses:\n", *License) fmt.Printf("Error: There is no '%s' license\n\nAvailable licenses:\n", *LicenseName)
listTemplates() listLicenses()
os.Exit(2) os.Exit(2)
} }
} }