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 (
|
2022-08-16 05:54:20 +00:00
|
|
|
name = "license"
|
|
|
|
usage = "generate LICENSE quickly"
|
2021-01-21 07:32:21 +00:00
|
|
|
|
2022-08-16 05:54:20 +00:00
|
|
|
commandGenerateName = "generate"
|
|
|
|
commandGenerateUsage = "generate LICENSE"
|
2021-01-21 08:41:40 +00:00
|
|
|
|
2022-08-16 05:54:20 +00:00
|
|
|
flagOutputName = "output"
|
|
|
|
flagOutputUsage = "output directory"
|
2021-04-16 17:42:12 +00:00
|
|
|
|
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-04-16 17:34:53 +00:00
|
|
|
|
2021-01-21 07:32:21 +00:00
|
|
|
func main() {
|
|
|
|
a := action{}
|
|
|
|
|
|
|
|
app := &cli.App{
|
2022-08-16 05:54:20 +00:00
|
|
|
Name: name,
|
|
|
|
Usage: usage,
|
2021-04-16 17:42:12 +00:00
|
|
|
Commands: []*cli.Command{
|
|
|
|
{
|
2022-08-16 05:54:20 +00:00
|
|
|
Name: commandGenerateName,
|
2022-08-16 07:29:59 +00:00
|
|
|
Aliases: commandGenerateAliases,
|
2022-08-16 05:54:20 +00:00
|
|
|
Usage: commandGenerateUsage,
|
2021-04-16 17:42:12 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
2022-08-16 05:54:20 +00:00
|
|
|
Name: flagOutputName,
|
|
|
|
Usage: flagOutputUsage,
|
2021-04-16 17:42:12 +00:00
|
|
|
DefaultText: currentDir,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: a.RunGenerate,
|
2021-01-21 08:41:40 +00:00
|
|
|
},
|
2021-01-21 07:32:21 +00:00
|
|
|
},
|
2021-04-16 17:42:12 +00:00
|
|
|
Action: a.RunHelp,
|
2021-01-21 07:32:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
2022-08-16 05:54:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 17:42:12 +00:00
|
|
|
func (a *action) RunHelp(c *cli.Context) error {
|
|
|
|
return cli.ShowAppHelp(c)
|
|
|
|
}
|
2021-01-21 07:32:21 +00:00
|
|
|
|
2021-04-16 17:42:12 +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)
|
|
|
|
}
|
2021-04-16 17:31:57 +00:00
|
|
|
licenseName := ioe.ReadInput()
|
2021-03-16 10:09:54 +00:00
|
|
|
|
2022-08-16 07:22:21 +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
|
|
|
}
|
|
|
|
|
2022-08-16 07:22:21 +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) {
|
2022-08-16 05:54:20 +00:00
|
|
|
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
|
|
|
}
|