feat: add outputFlag
parent
f03d806f8e
commit
729ed9e4af
10
license.go
10
license.go
|
@ -14,7 +14,7 @@ const (
|
||||||
// map template name with filename
|
// map template name with filename
|
||||||
var templates = map[string]templateInfo{
|
var templates = map[string]templateInfo{
|
||||||
"MIT": {
|
"MIT": {
|
||||||
filename: "mit",
|
filename: "mit.txt",
|
||||||
args: []string{
|
args: []string{
|
||||||
"[year]",
|
"[year]",
|
||||||
"[fullname]",
|
"[fullname]",
|
||||||
|
@ -27,7 +27,7 @@ type templateInfo struct {
|
||||||
args []string
|
args []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateLicense(name string, values map[string]string) (string, error) {
|
func generateLicense(name string) (string, error) {
|
||||||
if name == "" {
|
if name == "" {
|
||||||
return "", fmt.Errorf("empty license name")
|
return "", fmt.Errorf("empty license name")
|
||||||
}
|
}
|
||||||
|
@ -47,10 +47,8 @@ func generateLicense(name string, values map[string]string) (string, error) {
|
||||||
// Replace template
|
// Replace template
|
||||||
template := string(templateRaw)
|
template := string(templateRaw)
|
||||||
for _, arg := range templateInfo.args {
|
for _, arg := range templateInfo.args {
|
||||||
value, ok := values[arg]
|
fmt.Printf("What is your %s: ", arg)
|
||||||
if !ok {
|
value := readStdin()
|
||||||
return "", fmt.Errorf("missing arg %s", arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
template = strings.ReplaceAll(template, arg, value)
|
template = strings.ReplaceAll(template, arg, value)
|
||||||
}
|
}
|
||||||
|
|
28
main.go
28
main.go
|
@ -2,7 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
|
@ -11,7 +13,11 @@ import (
|
||||||
const (
|
const (
|
||||||
appName = "license"
|
appName = "license"
|
||||||
|
|
||||||
nameFlag = "name"
|
nameFlag = "name"
|
||||||
|
outputFlag = "output"
|
||||||
|
|
||||||
|
currentDir = "."
|
||||||
|
licenseFilename = "LICENSE"
|
||||||
)
|
)
|
||||||
|
|
||||||
var fmtErr = color.New(color.FgRed)
|
var fmtErr = color.New(color.FgRed)
|
||||||
|
@ -28,6 +34,12 @@ func main() {
|
||||||
Aliases: []string{"n"},
|
Aliases: []string{"n"},
|
||||||
Usage: "which `LICENSE`",
|
Usage: "which `LICENSE`",
|
||||||
},
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: outputFlag,
|
||||||
|
Aliases: []string{"o"},
|
||||||
|
Usage: "output directory",
|
||||||
|
DefaultText: currentDir,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Action: a.Run,
|
Action: a.Run,
|
||||||
}
|
}
|
||||||
|
@ -41,7 +53,8 @@ func main() {
|
||||||
|
|
||||||
type action struct {
|
type action struct {
|
||||||
flags struct {
|
flags struct {
|
||||||
name string
|
name string
|
||||||
|
output string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,9 +66,20 @@ func (a *action) Run(c *cli.Context) error {
|
||||||
|
|
||||||
a.getFlags(c)
|
a.getFlags(c)
|
||||||
|
|
||||||
|
license, err := generateLicense(a.flags.name)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to generate license %s: %w", a.flags.name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
outputFile := filepath.Join(a.flags.output, licenseFilename)
|
||||||
|
if err := ioutil.WriteFile(outputFile, []byte(license), os.ModePerm); err != nil {
|
||||||
|
return fmt.Errorf("failed to write file %s: %w", outputFile, err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *action) getFlags(c *cli.Context) {
|
func (a *action) getFlags(c *cli.Context) {
|
||||||
a.flags.name = c.String(nameFlag)
|
a.flags.name = c.String(nameFlag)
|
||||||
|
a.flags.output = c.String(outputFlag)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue