1
0

refactor: simplify errors

This commit is contained in:
Wiktor Zykubek 2024-12-28 21:11:35 +01:00
parent 80d32c4c2d
commit f61dd691a6
Signed by: wzykubek
GPG Key ID: 2221881F957D89B9

39
main.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"embed" "embed"
"errors"
"flag" "flag"
"fmt" "fmt"
"io/fs" "io/fs"
@ -33,17 +32,13 @@ type LicenseData struct {
//go:embed all:templates //go:embed all:templates
var TemplatesDir embed.FS var TemplatesDir 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) { func getGitUserData() (string, string, error) {
var userData [2]string var userData [2]string
for i, v := range []string{"user.name", "user.email"} { for i, v := range []string{"user.name", "user.email"} {
cmd := exec.Command("git", "config", "--get", v) cmd := exec.Command("git", "config", "--get", v)
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
return "", "", GitConfigError return "", "", fmt.Errorf("Can't read Git config: %w", err)
} }
userData[i] = strings.TrimSpace(string(out)) userData[i] = strings.TrimSpace(string(out))
@ -74,19 +69,13 @@ func listLicenses() {
func parseFrontMatter(tmplPath string) (LicenseData, string, error) { func parseFrontMatter(tmplPath string) (LicenseData, string, error) {
data, err := TemplatesDir.ReadFile(tmplPath) data, err := TemplatesDir.ReadFile(tmplPath)
if err != nil { if err != nil {
panic(err) return LicenseData{}, "", err
} }
parts := strings.SplitN(string(data), "---", 3) parts := strings.SplitN(string(data), "---", 3)
if len(parts) < 3 {
return LicenseData{}, "", InvalidFrontMatter
}
var licData LicenseData var licData LicenseData
err = yaml.Unmarshal([]byte(parts[1]), &licData) yaml.Unmarshal([]byte(parts[1]), &licData)
if err != nil {
return LicenseData{}, "", InvalidFrontMatter
}
return licData, strings.TrimSpace(parts[2]), nil return licData, strings.TrimSpace(parts[2]), nil
} }
@ -95,12 +84,12 @@ func genLicense(licName string, inputData InputData, outFileName string) error {
tmplPath := "templates/" + licName + ".tmpl" tmplPath := "templates/" + licName + ".tmpl"
_, lcnsBody, err := parseFrontMatter(tmplPath) _, lcnsBody, err := parseFrontMatter(tmplPath)
if err != nil { if err != nil {
panic(err) return err
} }
tmpl, err := template.New(licName).Parse(lcnsBody) tmpl, err := template.New(licName).Parse(lcnsBody)
if err != nil { if err != nil {
return NotSupportedError return fmt.Errorf("Not supported license")
} }
outFile, err := os.Create(outFileName) outFile, err := os.Create(outFileName)
@ -142,12 +131,10 @@ func main() {
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) { fmt.Printf(
fmt.Printf( "Error: Can't read Git config.\n\nUse --name \"NAME\" and --email EMAIL instead.\n",
"Error: Can't read Git config.\n\nUse --name \"NAME\" and --email EMAIL instead.\n", )
) os.Exit(3)
os.Exit(3)
}
} }
} }
@ -159,10 +146,8 @@ func main() {
err := genLicense(*LicenseName, inputData, *OutputFile) err := genLicense(*LicenseName, inputData, *OutputFile)
if err != nil { if err != nil {
if errors.Is(err, NotSupportedError) { fmt.Printf("Error: There is no '%s' license\n\nAvailable licenses:\n", *LicenseName)
fmt.Printf("Error: There is no '%s' license\n\nAvailable licenses:\n", *LicenseName) listLicenses()
listLicenses() os.Exit(2)
os.Exit(2)
}
} }
} }