license/main.go

103 lines
1.8 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 (
appName = "license"
appUsage = "generate LICENSE quickly"
2021-01-21 07:32:21 +00:00
// flags
2021-01-21 08:41:40 +00:00
outputFlag = "output"
// commands
generateCommand = "generate"
// flag usages
outputUsage = "output directory"
// command usages
generateUsage = "generate LICENSE"
2021-01-21 08:41:40 +00:00
currentDir = "."
licenseFilename = "LICENSE"
2021-01-21 07:32:21 +00:00
)
var (
// command aliases
generateAliases = []string{"gen"}
// flag aliases
outputAliases = []string{"o"}
)
2021-01-21 07:32:21 +00:00
func main() {
a := action{}
app := &cli.App{
Name: appName,
Usage: appUsage,
Commands: []*cli.Command{
{
Name: generateCommand,
Aliases: generateAliases,
Usage: generateUsage,
Flags: []cli.Flag{
&cli.StringFlag{
Name: outputFlag,
Aliases: outputAliases,
Usage: outputUsage,
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 {
2021-04-16 17:36:06 +00:00
color.PrintAppError(appName, 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)
2021-03-16 10:09:54 +00:00
fmt.Printf("What LICENSE do you chose: ")
licenseName := ioe.ReadInput()
2021-03-16 10:09:54 +00:00
license, 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)
if err := os.WriteFile(outputFile, []byte(license), 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) {
2021-01-21 08:41:40 +00:00
a.flags.output = c.String(outputFlag)
2021-01-21 07:32:21 +00:00
}