From 729ed9e4afaddd2b2f722a8f2bb449ab40e70d6b Mon Sep 17 00:00:00 2001 From: Tran Hau Date: Thu, 21 Jan 2021 15:41:40 +0700 Subject: [PATCH] feat: add outputFlag --- license.go | 10 ++++------ main.go | 28 ++++++++++++++++++++++++++-- stdin.go | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 stdin.go diff --git a/license.go b/license.go index e34889d..6916624 100644 --- a/license.go +++ b/license.go @@ -14,7 +14,7 @@ const ( // map template name with filename var templates = map[string]templateInfo{ "MIT": { - filename: "mit", + filename: "mit.txt", args: []string{ "[year]", "[fullname]", @@ -27,7 +27,7 @@ type templateInfo struct { args []string } -func generateLicense(name string, values map[string]string) (string, error) { +func generateLicense(name string) (string, error) { if name == "" { return "", fmt.Errorf("empty license name") } @@ -47,10 +47,8 @@ func generateLicense(name string, values map[string]string) (string, error) { // Replace template template := string(templateRaw) for _, arg := range templateInfo.args { - value, ok := values[arg] - if !ok { - return "", fmt.Errorf("missing arg %s", arg) - } + fmt.Printf("What is your %s: ", arg) + value := readStdin() template = strings.ReplaceAll(template, arg, value) } diff --git a/main.go b/main.go index 4aa49d7..d85ff40 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "io/ioutil" "os" + "path/filepath" "github.com/fatih/color" "github.com/urfave/cli/v2" @@ -11,7 +13,11 @@ import ( const ( appName = "license" - nameFlag = "name" + nameFlag = "name" + outputFlag = "output" + + currentDir = "." + licenseFilename = "LICENSE" ) var fmtErr = color.New(color.FgRed) @@ -28,6 +34,12 @@ func main() { Aliases: []string{"n"}, Usage: "which `LICENSE`", }, + &cli.StringFlag{ + Name: outputFlag, + Aliases: []string{"o"}, + Usage: "output directory", + DefaultText: currentDir, + }, }, Action: a.Run, } @@ -41,7 +53,8 @@ func main() { type action struct { flags struct { - name string + name string + output string } } @@ -53,9 +66,20 @@ func (a *action) Run(c *cli.Context) error { 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 } func (a *action) getFlags(c *cli.Context) { a.flags.name = c.String(nameFlag) + a.flags.output = c.String(outputFlag) } diff --git a/stdin.go b/stdin.go new file mode 100644 index 0000000..9ed466a --- /dev/null +++ b/stdin.go @@ -0,0 +1,22 @@ +package main + +import ( + "bufio" + "os" + "strings" +) + +func readStdin() string { + bs := bufio.NewScanner(os.Stdin) + for bs.Scan() { + line := bs.Text() + line = strings.TrimSpace(line) + if line == "" { + continue + } + + return line + } + + return "" +}