license/main.go

81 lines
1.4 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
"github.com/fatih/color"
"github.com/haunt98/ioe-go"
2021-01-21 07:32:21 +00:00
"github.com/urfave/cli/v2"
)
const (
appName = "license"
2021-01-21 08:41:40 +00:00
outputFlag = "output"
currentDir = "."
licenseFilename = "LICENSE"
2021-01-21 07:32:21 +00:00
)
var fmtErr = color.New(color.FgRed)
func main() {
a := action{}
app := &cli.App{
Name: appName,
Usage: "generate LICENSE",
Flags: []cli.Flag{
2021-01-21 08:41:40 +00:00
&cli.StringFlag{
Name: outputFlag,
Aliases: []string{"o"},
Usage: "output directory",
DefaultText: currentDir,
},
2021-01-21 07:32:21 +00:00
},
Action: a.Run,
}
if err := app.Run(os.Args); err != nil {
// Highlight error
fmtErr.Printf("[%s error]: ", appName)
fmt.Printf("%s\n", err.Error())
}
}
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) Run(c *cli.Context) error {
// Show help if there is nothing
if c.NArg() == 0 && c.NumFlags() == 0 {
return cli.ShowAppHelp(c)
}
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
}