feat!: rebuild entire cli and reorganize packages
This commit is contained in:
parent
31ff39d0c0
commit
b3b98bf15d
52
cmd/add.go
Normal file
52
cmd/add.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.brono.cloud/wzykubek/licensmith/internal"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var AuthorName string
|
||||||
|
var AuthorEmail string
|
||||||
|
var OutputFile string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(addCmd)
|
||||||
|
addCmd.Flags().StringVar(&AuthorName, "name", "", "Author name (read from Git by default)")
|
||||||
|
addCmd.Flags().StringVar(&AuthorEmail, "email", "", "Author email (read from Git by default)")
|
||||||
|
addCmd.Flags().StringVarP(&OutputFile, "output", "o", "LICENSE", "Output file")
|
||||||
|
}
|
||||||
|
|
||||||
|
var addCmd = &cobra.Command{
|
||||||
|
Use: "add [license]",
|
||||||
|
Short: "Add LICENSE based on SPDX ID",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
licenseID := args[0]
|
||||||
|
|
||||||
|
licenseCtx, err := internal.NewLicenseContext(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,
|
||||||
|
}
|
||||||
|
|
||||||
|
err = licenser.Generate()
|
||||||
|
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 {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
23
cmd/list.go
Normal file
23
cmd/list.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.brono.cloud/wzykubek/licensmith/internal"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
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, ", "))
|
||||||
|
},
|
||||||
|
}
|
21
cmd/root.go
Normal file
21
cmd/root.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var rootCmd = &cobra.Command{
|
||||||
|
Use: "licensmith",
|
||||||
|
Short: "Licensmith is a LICENSE generator",
|
||||||
|
Long: "Effortlessly craft the perfect LICENSE for your Git repo in seconds with a single command!",
|
||||||
|
}
|
||||||
|
|
||||||
|
func Execute() {
|
||||||
|
if err := rootCmd.Execute(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
10
go.mod
10
go.mod
@ -2,4 +2,12 @@ module git.brono.cloud/wzykubek/licensmith
|
|||||||
|
|
||||||
go 1.23.4
|
go 1.23.4
|
||||||
|
|
||||||
require gopkg.in/yaml.v3 v3.0.1 // indirect
|
require (
|
||||||
|
github.com/spf13/cobra v1.8.1
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
|
)
|
||||||
|
9
go.sum
9
go.sum
@ -1,3 +1,12 @@
|
|||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
|
||||||
|
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
|
||||||
|
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||||
|
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -19,7 +19,7 @@ type Licenser struct {
|
|||||||
|
|
||||||
func (l *Licenser) ParseTemplate() (LicenseTemplate, error) {
|
func (l *Licenser) ParseTemplate() (LicenseTemplate, error) {
|
||||||
tmplPath := "templates/" + l.LicenseID + ".tmpl"
|
tmplPath := "templates/" + l.LicenseID + ".tmpl"
|
||||||
data, err := TemplatesDir.ReadFile(tmplPath)
|
data, err := EmbedFS.ReadFile(tmplPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return LicenseTemplate{}, err
|
return LicenseTemplate{}, err
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package main
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
@ -6,8 +6,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed all:templates
|
//go:embed templates/*
|
||||||
var TemplatesDir embed.FS
|
var EmbedFS embed.FS
|
||||||
|
|
||||||
type LicenseTemplate struct {
|
type LicenseTemplate struct {
|
||||||
Title string `yaml:"title"`
|
Title string `yaml:"title"`
|
||||||
@ -19,8 +19,8 @@ type LicenseTemplate struct {
|
|||||||
Body string
|
Body string
|
||||||
}
|
}
|
||||||
|
|
||||||
func listTemplates() []string {
|
func ListTemplates() []string {
|
||||||
files, err := fs.ReadDir(TemplatesDir, "templates")
|
files, err := fs.ReadDir(EmbedFS, "templates")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -32,8 +32,3 @@ func listTemplates() []string {
|
|||||||
|
|
||||||
return tmplList
|
return tmplList
|
||||||
}
|
}
|
||||||
|
|
||||||
func listLicenses() {
|
|
||||||
tmplList := listTemplates()
|
|
||||||
println(strings.Join(tmplList, ", "))
|
|
||||||
}
|
|
47
main.go
47
main.go
@ -1,52 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"git.brono.cloud/wzykubek/licensmith/cmd"
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
OutputFile := flag.String("output", "LICENSE", "Specify different output file")
|
cmd.Execute()
|
||||||
LicenseID := flag.String("license", "", "Specify license by SPDX ID (e.g. BSD-3-Clause)")
|
|
||||||
AuthorName := flag.String("name", "", "Set the author name (read from Git by default)")
|
|
||||||
AuthorEmail := flag.String("email", "", "Set the author email (read from Git by default)")
|
|
||||||
ListLicenses := flag.Bool("list", false, "List available licenses")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if *ListLicenses {
|
|
||||||
listLicenses()
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
if *LicenseID == "" {
|
|
||||||
fmt.Printf("Error: No license specified\n\nUse --license LICENSE\n\nAvailable licenses:\n")
|
|
||||||
listLicenses()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
licenseCtx, err := NewLicenseContext(*AuthorName, *AuthorEmail)
|
|
||||||
if err != nil && err.Error() == "can't read Git config" {
|
|
||||||
fmt.Printf(
|
|
||||||
"Error: Can't read Git config.\n\nUse --name \"NAME\" and --email EMAIL instead.\n",
|
|
||||||
)
|
|
||||||
os.Exit(3)
|
|
||||||
}
|
|
||||||
|
|
||||||
licenser := Licenser{
|
|
||||||
LicenseID: *LicenseID,
|
|
||||||
LicenseContext: licenseCtx,
|
|
||||||
OutputFile: *OutputFile,
|
|
||||||
}
|
|
||||||
|
|
||||||
err = licenser.Generate()
|
|
||||||
if err != nil && err.Error() == "usupported license" {
|
|
||||||
fmt.Printf("Error: There is no '%s' license\n\nAvailable licenses:\n", *LicenseID)
|
|
||||||
listLicenses()
|
|
||||||
os.Exit(2)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = licenser.WriteFile(); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user