From f9e2eb8a4ee8feee07e4e5207485d1b9f1006a9d Mon Sep 17 00:00:00 2001 From: Wiktor Zykubek Date: Wed, 1 Jan 2025 20:52:09 +0100 Subject: [PATCH] refactor: reorganize package structure --- cmd/add.go | 15 ++--- cmd/list.go | 10 +-- internal/license/context.go | 32 ++++++++++ internal/license/license.go | 47 ++++++++++++++ internal/license_context.go | 30 --------- internal/licenser.go | 64 ------------------- internal/{templates => template}/0BSD.tmpl | 0 internal/{templates => template}/AFL-3.0.tmpl | 0 .../{templates => template}/AGPL-3.0.tmpl | 0 .../{templates => template}/Apache-2.0.tmpl | 0 .../{templates => template}/Artistic-2.0.tmpl | 0 .../BSD-2-Clause-Patent.tmpl | 0 .../{templates => template}/BSD-2-Clause.tmpl | 0 .../BSD-3-Clause-Clear.tmpl | 0 .../{templates => template}/BSD-3-Clause.tmpl | 0 .../{templates => template}/BSD-4-Clause.tmpl | 0 internal/{templates => template}/BSL-1.0.tmpl | 0 .../BlueOak-1.0.0.tmpl | 0 .../{templates => template}/CC-BY-4.0.tmpl | 0 .../{templates => template}/CC-BY-SA-4.0.tmpl | 0 internal/{templates => template}/CC0-1.0.tmpl | 0 .../{templates => template}/CECILL-2.1.tmpl | 0 .../CERN-OHL-P-2.0.tmpl | 0 .../CERN-OHL-S-2.0.tmpl | 0 .../CERN-OHL-W-2.0.tmpl | 0 internal/{templates => template}/ECL-2.0.tmpl | 0 internal/{templates => template}/EPL-1.0.tmpl | 0 internal/{templates => template}/EPL-2.0.tmpl | 0 .../{templates => template}/EUPL-1.1.tmpl | 0 .../{templates => template}/EUPL-1.2.tmpl | 0 .../{templates => template}/GFDL-1.3.tmpl | 0 internal/{templates => template}/GPL-2.0.tmpl | 0 internal/{templates => template}/GPL-3.0.tmpl | 0 internal/{templates => template}/ISC.tmpl | 0 .../{templates => template}/LGPL-2.1.tmpl | 0 .../{templates => template}/LGPL-3.0.tmpl | 0 .../{templates => template}/LPPL-1.3c.tmpl | 0 internal/{templates => template}/MIT-0.tmpl | 0 internal/{templates => template}/MIT.tmpl | 0 internal/{templates => template}/MPL-2.0.tmpl | 0 internal/{templates => template}/MS-PL.tmpl | 0 internal/{templates => template}/MS-RL.tmpl | 0 .../{templates => template}/MulanPSL-2.0.tmpl | 0 internal/{templates => template}/NCSA.tmpl | 0 .../{templates => template}/ODbL-1.0.tmpl | 0 internal/{templates => template}/OFL-1.1.tmpl | 0 internal/{templates => template}/OSL-3.0.tmpl | 0 .../{templates => template}/PostgreSQL.tmpl | 0 internal/{templates => template}/UPL-1.0.tmpl | 0 .../{templates => template}/Unlicense.tmpl | 0 internal/{templates => template}/Vim.tmpl | 0 internal/{templates => template}/WTFPL.tmpl | 0 internal/{templates => template}/Zlib.tmpl | 0 internal/template/template.go | 51 +++++++++++++++ internal/templates.go | 34 ---------- internal/git_utils.go => pkg/utils/git.go | 4 +- 56 files changed, 144 insertions(+), 143 deletions(-) create mode 100644 internal/license/context.go create mode 100644 internal/license/license.go delete mode 100644 internal/license_context.go delete mode 100644 internal/licenser.go rename internal/{templates => template}/0BSD.tmpl (100%) rename internal/{templates => template}/AFL-3.0.tmpl (100%) rename internal/{templates => template}/AGPL-3.0.tmpl (100%) rename internal/{templates => template}/Apache-2.0.tmpl (100%) rename internal/{templates => template}/Artistic-2.0.tmpl (100%) rename internal/{templates => template}/BSD-2-Clause-Patent.tmpl (100%) rename internal/{templates => template}/BSD-2-Clause.tmpl (100%) rename internal/{templates => template}/BSD-3-Clause-Clear.tmpl (100%) rename internal/{templates => template}/BSD-3-Clause.tmpl (100%) rename internal/{templates => template}/BSD-4-Clause.tmpl (100%) rename internal/{templates => template}/BSL-1.0.tmpl (100%) rename internal/{templates => template}/BlueOak-1.0.0.tmpl (100%) rename internal/{templates => template}/CC-BY-4.0.tmpl (100%) rename internal/{templates => template}/CC-BY-SA-4.0.tmpl (100%) rename internal/{templates => template}/CC0-1.0.tmpl (100%) rename internal/{templates => template}/CECILL-2.1.tmpl (100%) rename internal/{templates => template}/CERN-OHL-P-2.0.tmpl (100%) rename internal/{templates => template}/CERN-OHL-S-2.0.tmpl (100%) rename internal/{templates => template}/CERN-OHL-W-2.0.tmpl (100%) rename internal/{templates => template}/ECL-2.0.tmpl (100%) rename internal/{templates => template}/EPL-1.0.tmpl (100%) rename internal/{templates => template}/EPL-2.0.tmpl (100%) rename internal/{templates => template}/EUPL-1.1.tmpl (100%) rename internal/{templates => template}/EUPL-1.2.tmpl (100%) rename internal/{templates => template}/GFDL-1.3.tmpl (100%) rename internal/{templates => template}/GPL-2.0.tmpl (100%) rename internal/{templates => template}/GPL-3.0.tmpl (100%) rename internal/{templates => template}/ISC.tmpl (100%) rename internal/{templates => template}/LGPL-2.1.tmpl (100%) rename internal/{templates => template}/LGPL-3.0.tmpl (100%) rename internal/{templates => template}/LPPL-1.3c.tmpl (100%) rename internal/{templates => template}/MIT-0.tmpl (100%) rename internal/{templates => template}/MIT.tmpl (100%) rename internal/{templates => template}/MPL-2.0.tmpl (100%) rename internal/{templates => template}/MS-PL.tmpl (100%) rename internal/{templates => template}/MS-RL.tmpl (100%) rename internal/{templates => template}/MulanPSL-2.0.tmpl (100%) rename internal/{templates => template}/NCSA.tmpl (100%) rename internal/{templates => template}/ODbL-1.0.tmpl (100%) rename internal/{templates => template}/OFL-1.1.tmpl (100%) rename internal/{templates => template}/OSL-3.0.tmpl (100%) rename internal/{templates => template}/PostgreSQL.tmpl (100%) rename internal/{templates => template}/UPL-1.0.tmpl (100%) rename internal/{templates => template}/Unlicense.tmpl (100%) rename internal/{templates => template}/Vim.tmpl (100%) rename internal/{templates => template}/WTFPL.tmpl (100%) rename internal/{templates => template}/Zlib.tmpl (100%) create mode 100644 internal/template/template.go delete mode 100644 internal/templates.go rename internal/git_utils.go => pkg/utils/git.go (79%) diff --git a/cmd/add.go b/cmd/add.go index 12aacfc..7d030ad 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -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) } }, diff --git a/cmd/list.go b/cmd/list.go index 476ec90..5968ca9 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -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, ", ")) }, } diff --git a/internal/license/context.go b/internal/license/context.go new file mode 100644 index 0000000..67a873a --- /dev/null +++ b/internal/license/context.go @@ -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 +} diff --git a/internal/license/license.go b/internal/license/license.go new file mode 100644 index 0000000..8cdddc6 --- /dev/null +++ b/internal/license/license.go @@ -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 +} diff --git a/internal/license_context.go b/internal/license_context.go deleted file mode 100644 index 01f789f..0000000 --- a/internal/license_context.go +++ /dev/null @@ -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 -} diff --git a/internal/licenser.go b/internal/licenser.go deleted file mode 100644 index 8181c0a..0000000 --- a/internal/licenser.go +++ /dev/null @@ -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 -} diff --git a/internal/templates/0BSD.tmpl b/internal/template/0BSD.tmpl similarity index 100% rename from internal/templates/0BSD.tmpl rename to internal/template/0BSD.tmpl diff --git a/internal/templates/AFL-3.0.tmpl b/internal/template/AFL-3.0.tmpl similarity index 100% rename from internal/templates/AFL-3.0.tmpl rename to internal/template/AFL-3.0.tmpl diff --git a/internal/templates/AGPL-3.0.tmpl b/internal/template/AGPL-3.0.tmpl similarity index 100% rename from internal/templates/AGPL-3.0.tmpl rename to internal/template/AGPL-3.0.tmpl diff --git a/internal/templates/Apache-2.0.tmpl b/internal/template/Apache-2.0.tmpl similarity index 100% rename from internal/templates/Apache-2.0.tmpl rename to internal/template/Apache-2.0.tmpl diff --git a/internal/templates/Artistic-2.0.tmpl b/internal/template/Artistic-2.0.tmpl similarity index 100% rename from internal/templates/Artistic-2.0.tmpl rename to internal/template/Artistic-2.0.tmpl diff --git a/internal/templates/BSD-2-Clause-Patent.tmpl b/internal/template/BSD-2-Clause-Patent.tmpl similarity index 100% rename from internal/templates/BSD-2-Clause-Patent.tmpl rename to internal/template/BSD-2-Clause-Patent.tmpl diff --git a/internal/templates/BSD-2-Clause.tmpl b/internal/template/BSD-2-Clause.tmpl similarity index 100% rename from internal/templates/BSD-2-Clause.tmpl rename to internal/template/BSD-2-Clause.tmpl diff --git a/internal/templates/BSD-3-Clause-Clear.tmpl b/internal/template/BSD-3-Clause-Clear.tmpl similarity index 100% rename from internal/templates/BSD-3-Clause-Clear.tmpl rename to internal/template/BSD-3-Clause-Clear.tmpl diff --git a/internal/templates/BSD-3-Clause.tmpl b/internal/template/BSD-3-Clause.tmpl similarity index 100% rename from internal/templates/BSD-3-Clause.tmpl rename to internal/template/BSD-3-Clause.tmpl diff --git a/internal/templates/BSD-4-Clause.tmpl b/internal/template/BSD-4-Clause.tmpl similarity index 100% rename from internal/templates/BSD-4-Clause.tmpl rename to internal/template/BSD-4-Clause.tmpl diff --git a/internal/templates/BSL-1.0.tmpl b/internal/template/BSL-1.0.tmpl similarity index 100% rename from internal/templates/BSL-1.0.tmpl rename to internal/template/BSL-1.0.tmpl diff --git a/internal/templates/BlueOak-1.0.0.tmpl b/internal/template/BlueOak-1.0.0.tmpl similarity index 100% rename from internal/templates/BlueOak-1.0.0.tmpl rename to internal/template/BlueOak-1.0.0.tmpl diff --git a/internal/templates/CC-BY-4.0.tmpl b/internal/template/CC-BY-4.0.tmpl similarity index 100% rename from internal/templates/CC-BY-4.0.tmpl rename to internal/template/CC-BY-4.0.tmpl diff --git a/internal/templates/CC-BY-SA-4.0.tmpl b/internal/template/CC-BY-SA-4.0.tmpl similarity index 100% rename from internal/templates/CC-BY-SA-4.0.tmpl rename to internal/template/CC-BY-SA-4.0.tmpl diff --git a/internal/templates/CC0-1.0.tmpl b/internal/template/CC0-1.0.tmpl similarity index 100% rename from internal/templates/CC0-1.0.tmpl rename to internal/template/CC0-1.0.tmpl diff --git a/internal/templates/CECILL-2.1.tmpl b/internal/template/CECILL-2.1.tmpl similarity index 100% rename from internal/templates/CECILL-2.1.tmpl rename to internal/template/CECILL-2.1.tmpl diff --git a/internal/templates/CERN-OHL-P-2.0.tmpl b/internal/template/CERN-OHL-P-2.0.tmpl similarity index 100% rename from internal/templates/CERN-OHL-P-2.0.tmpl rename to internal/template/CERN-OHL-P-2.0.tmpl diff --git a/internal/templates/CERN-OHL-S-2.0.tmpl b/internal/template/CERN-OHL-S-2.0.tmpl similarity index 100% rename from internal/templates/CERN-OHL-S-2.0.tmpl rename to internal/template/CERN-OHL-S-2.0.tmpl diff --git a/internal/templates/CERN-OHL-W-2.0.tmpl b/internal/template/CERN-OHL-W-2.0.tmpl similarity index 100% rename from internal/templates/CERN-OHL-W-2.0.tmpl rename to internal/template/CERN-OHL-W-2.0.tmpl diff --git a/internal/templates/ECL-2.0.tmpl b/internal/template/ECL-2.0.tmpl similarity index 100% rename from internal/templates/ECL-2.0.tmpl rename to internal/template/ECL-2.0.tmpl diff --git a/internal/templates/EPL-1.0.tmpl b/internal/template/EPL-1.0.tmpl similarity index 100% rename from internal/templates/EPL-1.0.tmpl rename to internal/template/EPL-1.0.tmpl diff --git a/internal/templates/EPL-2.0.tmpl b/internal/template/EPL-2.0.tmpl similarity index 100% rename from internal/templates/EPL-2.0.tmpl rename to internal/template/EPL-2.0.tmpl diff --git a/internal/templates/EUPL-1.1.tmpl b/internal/template/EUPL-1.1.tmpl similarity index 100% rename from internal/templates/EUPL-1.1.tmpl rename to internal/template/EUPL-1.1.tmpl diff --git a/internal/templates/EUPL-1.2.tmpl b/internal/template/EUPL-1.2.tmpl similarity index 100% rename from internal/templates/EUPL-1.2.tmpl rename to internal/template/EUPL-1.2.tmpl diff --git a/internal/templates/GFDL-1.3.tmpl b/internal/template/GFDL-1.3.tmpl similarity index 100% rename from internal/templates/GFDL-1.3.tmpl rename to internal/template/GFDL-1.3.tmpl diff --git a/internal/templates/GPL-2.0.tmpl b/internal/template/GPL-2.0.tmpl similarity index 100% rename from internal/templates/GPL-2.0.tmpl rename to internal/template/GPL-2.0.tmpl diff --git a/internal/templates/GPL-3.0.tmpl b/internal/template/GPL-3.0.tmpl similarity index 100% rename from internal/templates/GPL-3.0.tmpl rename to internal/template/GPL-3.0.tmpl diff --git a/internal/templates/ISC.tmpl b/internal/template/ISC.tmpl similarity index 100% rename from internal/templates/ISC.tmpl rename to internal/template/ISC.tmpl diff --git a/internal/templates/LGPL-2.1.tmpl b/internal/template/LGPL-2.1.tmpl similarity index 100% rename from internal/templates/LGPL-2.1.tmpl rename to internal/template/LGPL-2.1.tmpl diff --git a/internal/templates/LGPL-3.0.tmpl b/internal/template/LGPL-3.0.tmpl similarity index 100% rename from internal/templates/LGPL-3.0.tmpl rename to internal/template/LGPL-3.0.tmpl diff --git a/internal/templates/LPPL-1.3c.tmpl b/internal/template/LPPL-1.3c.tmpl similarity index 100% rename from internal/templates/LPPL-1.3c.tmpl rename to internal/template/LPPL-1.3c.tmpl diff --git a/internal/templates/MIT-0.tmpl b/internal/template/MIT-0.tmpl similarity index 100% rename from internal/templates/MIT-0.tmpl rename to internal/template/MIT-0.tmpl diff --git a/internal/templates/MIT.tmpl b/internal/template/MIT.tmpl similarity index 100% rename from internal/templates/MIT.tmpl rename to internal/template/MIT.tmpl diff --git a/internal/templates/MPL-2.0.tmpl b/internal/template/MPL-2.0.tmpl similarity index 100% rename from internal/templates/MPL-2.0.tmpl rename to internal/template/MPL-2.0.tmpl diff --git a/internal/templates/MS-PL.tmpl b/internal/template/MS-PL.tmpl similarity index 100% rename from internal/templates/MS-PL.tmpl rename to internal/template/MS-PL.tmpl diff --git a/internal/templates/MS-RL.tmpl b/internal/template/MS-RL.tmpl similarity index 100% rename from internal/templates/MS-RL.tmpl rename to internal/template/MS-RL.tmpl diff --git a/internal/templates/MulanPSL-2.0.tmpl b/internal/template/MulanPSL-2.0.tmpl similarity index 100% rename from internal/templates/MulanPSL-2.0.tmpl rename to internal/template/MulanPSL-2.0.tmpl diff --git a/internal/templates/NCSA.tmpl b/internal/template/NCSA.tmpl similarity index 100% rename from internal/templates/NCSA.tmpl rename to internal/template/NCSA.tmpl diff --git a/internal/templates/ODbL-1.0.tmpl b/internal/template/ODbL-1.0.tmpl similarity index 100% rename from internal/templates/ODbL-1.0.tmpl rename to internal/template/ODbL-1.0.tmpl diff --git a/internal/templates/OFL-1.1.tmpl b/internal/template/OFL-1.1.tmpl similarity index 100% rename from internal/templates/OFL-1.1.tmpl rename to internal/template/OFL-1.1.tmpl diff --git a/internal/templates/OSL-3.0.tmpl b/internal/template/OSL-3.0.tmpl similarity index 100% rename from internal/templates/OSL-3.0.tmpl rename to internal/template/OSL-3.0.tmpl diff --git a/internal/templates/PostgreSQL.tmpl b/internal/template/PostgreSQL.tmpl similarity index 100% rename from internal/templates/PostgreSQL.tmpl rename to internal/template/PostgreSQL.tmpl diff --git a/internal/templates/UPL-1.0.tmpl b/internal/template/UPL-1.0.tmpl similarity index 100% rename from internal/templates/UPL-1.0.tmpl rename to internal/template/UPL-1.0.tmpl diff --git a/internal/templates/Unlicense.tmpl b/internal/template/Unlicense.tmpl similarity index 100% rename from internal/templates/Unlicense.tmpl rename to internal/template/Unlicense.tmpl diff --git a/internal/templates/Vim.tmpl b/internal/template/Vim.tmpl similarity index 100% rename from internal/templates/Vim.tmpl rename to internal/template/Vim.tmpl diff --git a/internal/templates/WTFPL.tmpl b/internal/template/WTFPL.tmpl similarity index 100% rename from internal/templates/WTFPL.tmpl rename to internal/template/WTFPL.tmpl diff --git a/internal/templates/Zlib.tmpl b/internal/template/Zlib.tmpl similarity index 100% rename from internal/templates/Zlib.tmpl rename to internal/template/Zlib.tmpl diff --git a/internal/template/template.go b/internal/template/template.go new file mode 100644 index 0000000..2b5d743 --- /dev/null +++ b/internal/template/template.go @@ -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 +} diff --git a/internal/templates.go b/internal/templates.go deleted file mode 100644 index 148db6e..0000000 --- a/internal/templates.go +++ /dev/null @@ -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 -} diff --git a/internal/git_utils.go b/pkg/utils/git.go similarity index 79% rename from internal/git_utils.go rename to pkg/utils/git.go index d479ba0..ff8cd31 100644 --- a/internal/git_utils.go +++ b/pkg/utils/git.go @@ -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 {