license/main.go

96 lines
1.9 KiB
Go
Raw Normal View History

2021-01-21 07:32:21 +00:00
package main
import (
"fmt"
"os"
2021-01-21 08:41:40 +00:00
"path/filepath"
2021-01-21 07:32:21 +00:00
2021-12-02 07:08:28 +00:00
"github.com/make-go-great/color-go"
"github.com/make-go-great/ioe-go"
2021-01-21 07:32:21 +00:00
"github.com/urfave/cli/v2"
)
const (
name = "license"
usage = "generate LICENSE quickly"
2021-01-21 07:32:21 +00:00
commandGenerateName = "generate"
commandGenerateUsage = "generate LICENSE"
2021-01-21 08:41:40 +00:00
flagOutputName = "output"
flagOutputUsage = "output directory"
2022-08-16 07:33:37 +00:00
currentDir = "."
2021-01-21 07:32:21 +00:00
)
2022-08-16 07:29:59 +00:00
var commandGenerateAliases = []string{"gen", "g"}
2021-01-21 07:32:21 +00:00
func main() {
a := action{}
app := &cli.App{
Name: name,
Usage: usage,
Commands: []*cli.Command{
{
Name: commandGenerateName,
2022-08-16 07:29:59 +00:00
Aliases: commandGenerateAliases,
Usage: commandGenerateUsage,
Flags: []cli.Flag{
&cli.StringFlag{
Name: flagOutputName,
Usage: flagOutputUsage,
DefaultText: currentDir,
},
},
Action: a.RunGenerate,
2021-01-21 08:41:40 +00:00
},
2021-01-21 07:32:21 +00:00
},
Action: a.RunHelp,
2021-01-21 07:32:21 +00:00
}
if err := app.Run(os.Args); err != nil {
color.PrintAppError(name, err.Error())
2021-01-21 07:32:21 +00:00
}
}
type action struct {
flags struct {
2021-01-21 08:41:40 +00:00
output string
2021-01-21 07:32:21 +00:00
}
}
func (a *action) RunHelp(c *cli.Context) error {
return cli.ShowAppHelp(c)
}
2021-01-21 07:32:21 +00:00
func (a *action) RunGenerate(c *cli.Context) error {
2021-01-21 07:32:21 +00:00
a.getFlags(c)
2022-08-16 07:25:01 +00:00
fmt.Println("What LICENSE do you chose: ")
fmt.Println("Currently support: ")
for templateName := range templates {
fmt.Println("-", templateName)
}
licenseName := ioe.ReadInput()
2021-03-16 10:09:54 +00:00
licenseData, licenseFilename, err := generateLicense(licenseName)
2021-01-21 08:41:40 +00:00
if err != nil {
2021-03-16 10:09:54 +00:00
return fmt.Errorf("failed to generate license %s: %w", licenseName, err)
2021-01-21 08:41:40 +00:00
}
outputFile := filepath.Join(a.flags.output, licenseFilename)
2022-08-16 07:21:22 +00:00
if err := os.WriteFile(outputFile, []byte(licenseData), os.ModePerm); err != nil {
2021-01-21 08:41:40 +00:00
return fmt.Errorf("failed to write file %s: %w", outputFile, err)
}
2021-01-21 07:32:21 +00:00
return nil
}
func (a *action) getFlags(c *cli.Context) {
a.flags.output = c.String(flagOutputName)
2022-08-16 07:29:59 +00:00
if a.flags.output == "" {
a.flags.output = currentDir
}
2021-01-21 07:32:21 +00:00
}