1
0

refactor: reorganize package structure

This commit is contained in:
Wiktor Zykubek 2025-01-01 20:52:09 +01:00
parent d519ea19c0
commit f9e2eb8a4e
Signed by: wzykubek
GPG Key ID: 2221881F957D89B9
56 changed files with 144 additions and 143 deletions

View File

@ -4,7 +4,7 @@ import (
"fmt"
"os"
"go.wzykubek.xyz/licensmith/internal"
l "go.wzykubek.xyz/licensmith/internal/license"
"github.com/spf13/cobra"
)
@ -27,25 +27,24 @@ var addCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
licenseID := args[0]
licenseCtx, err := internal.NewLicenseContext(AuthorName, AuthorEmail)
ctx, err := l.NewContext(AuthorName, AuthorEmail)
if err != nil && err.Error() == "can't read Git config" {
fmt.Println("Error: Can't read Git config")
os.Exit(3)
}
licenser := internal.Licenser{
LicenseID: licenseID,
LicenseContext: licenseCtx,
OutputFile: OutputFile,
license := l.License{
ID: licenseID,
Context: ctx,
}
err = licenser.Generate()
err = license.Gen()
if err != nil && err.Error() == "usupported license" {
fmt.Printf("Error: There is no '%s' license\n", licenseID)
os.Exit(2)
}
if err = licenser.WriteFile(); err != nil {
if err = license.Write(OutputFile); err != nil {
panic(err)
}
},

View File

@ -2,22 +2,22 @@ package cmd
import (
"fmt"
"strings"
"strings"
"go.wzykubek.xyz/licensmith/internal"
t "go.wzykubek.xyz/licensmith/internal/template"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(listCmd)
rootCmd.AddCommand(listCmd)
}
var listCmd = &cobra.Command{
Use: "list",
Short: "List available licenses",
Run: func(cmd *cobra.Command, args []string) {
tmplList := internal.ListTemplates()
fmt.Println(strings.Join(tmplList, ", "))
templates := t.List()
fmt.Println(strings.Join(templates, ", "))
},
}

View File

@ -0,0 +1,32 @@
package license
import (
"go.wzykubek.xyz/licensmith/pkg/utils"
"time"
)
type Context struct {
AuthorName string
AuthorEmail string
Year int
}
func NewContext(authorName string, authorEmail string) (Context, error) {
var err error
if authorName == "" {
authorName, err = utils.GitUserData("user.name")
}
if authorEmail == "" {
authorEmail, err = utils.GitUserData("user.email")
}
if err != nil {
return Context{}, err
}
return Context{
AuthorName: authorName,
AuthorEmail: authorEmail,
Year: time.Now().Year(),
}, nil
}

View File

@ -0,0 +1,47 @@
package license
import (
"bytes"
"errors"
"os"
"text/template"
t "go.wzykubek.xyz/licensmith/internal/template"
)
type License struct {
ID string
Context Context
Body string
}
func (l *License) Gen() error {
tmplPath := l.ID + ".tmpl"
tmpl, err := t.Parse(tmplPath)
if err != nil {
return errors.New("usupported license")
}
body, _ := template.New(l.ID).Parse(tmpl.Body)
var output bytes.Buffer
body.Execute(&output, l.Context)
l.Body = output.String()
return nil
}
func (l *License) Write(path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
if _, err := file.WriteString(l.Body); err != nil {
return err
}
return nil
}

View File

@ -1,30 +0,0 @@
package internal
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
}

View File

@ -1,64 +0,0 @@
package internal
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) {
tmplPath := "templates/" + l.LicenseID + ".tmpl"
data, err := EmbedFS.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("usupported 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
}

View File

@ -0,0 +1,51 @@
package templates
import (
"embed"
"io/fs"
"strings"
"gopkg.in/yaml.v3"
)
//go:embed *.tmpl
var FS embed.FS
type Template 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 Parse(path string) (Template, error) {
data, err := FS.ReadFile(path)
if err != nil {
return Template{}, err
}
parts := strings.SplitN(string(data), "---", 3)
var tmpl Template
yaml.Unmarshal([]byte(parts[1]), &tmpl)
tmpl.Body = strings.TrimSpace(parts[2])
return tmpl, nil
}
func List() []string {
files, err := fs.ReadDir(FS, ".")
if err != nil {
panic(err)
}
var templates []string
for _, v := range files {
templates = append(templates, strings.Replace(v.Name(), ".tmpl", "", 1))
}
return templates
}

View File

@ -1,34 +0,0 @@
package internal
import (
"embed"
"io/fs"
"strings"
)
//go:embed templates/*
var EmbedFS 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(EmbedFS, "templates")
if err != nil {
panic(err)
}
var tmplList []string
for _, v := range files {
tmplList = append(tmplList, strings.Replace(v.Name(), ".tmpl", "", 1))
}
return tmplList
}

View File

@ -1,4 +1,4 @@
package internal
package utils
import (
"errors"
@ -6,7 +6,7 @@ import (
"strings"
)
func gitUserData(key string) (string, error) {
func GitUserData(key string) (string, error) {
cmd := exec.Command("git", "config", "--get", key)
out, err := cmd.Output()
if err != nil {