From a17e3ff2a181a7b978f4472c78a69e32decb86be Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Tue, 16 Aug 2022 14:21:22 +0700 Subject: [PATCH] feat: use template output filename --- license.go | 30 ++++++++++++++++-------------- main.go | 6 +++--- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/license.go b/license.go index c53c121..3ff41dd 100644 --- a/license.go +++ b/license.go @@ -23,27 +23,29 @@ var embedFS embed.FS // always use upercase for license name var templates = map[string]templateInfo{ "MIT": { - filename: "mit.txt", + templateFilename: "mit.txt", + outputFilename: "LICENSE", args: []string{ "[year]", "[fullname]", }, }, "GNU GPLv3": { - filename: "gnu_gplv3.txt", + templateFilename: "gnu_gplv3.txt", + outputFilename: "COPYING", }, } type templateInfo struct { - filename string - args []string + templateFilename string + outputFilename string + args []string } -func generateLicense(name string) (string, error) { +func generateLicense(name string) (string, string, error) { if name == "" { - return "", fmt.Errorf("empty license name: %w", ErrInvalidLicense) + return "", "", fmt.Errorf("empty license name: %w", ErrInvalidLicense) } - name = strings.ToUpper(name) isSupportTemplate := false var templateInfo templateInfo @@ -55,26 +57,26 @@ func generateLicense(name string) (string, error) { } if !isSupportTemplate { - return "", fmt.Errorf("not support license %s: %w", name, ErrInvalidLicense) + return "", "", fmt.Errorf("not support license %s: %w", name, ErrInvalidLicense) } // Get correct path of license - path := filepath.Join(templatesPath, templateInfo.filename) + path := filepath.Join(templatesPath, templateInfo.templateFilename) // Read template templateRaw, err := embedFS.ReadFile(path) if err != nil { - return "", fmt.Errorf("failed to read file %s: %w", path, err) + return "", "", fmt.Errorf("failed to read file %s: %w", path, err) } - // Replace template - template := string(templateRaw) + // Replace template info args + licenseData := string(templateRaw) for _, arg := range templateInfo.args { fmt.Printf("What is your %s: ", arg) value := ioe.ReadInput() - template = strings.ReplaceAll(template, arg, value) + licenseData = strings.ReplaceAll(licenseData, arg, value) } - return template, nil + return licenseData, templateInfo.outputFilename, nil } diff --git a/main.go b/main.go index 21a99bd..2e2ef88 100644 --- a/main.go +++ b/main.go @@ -78,13 +78,13 @@ func (a *action) RunGenerate(c *cli.Context) error { fmt.Printf("What LICENSE do you chose: ") licenseName := ioe.ReadInput() - license, err := generateLicense(licenseName) + licenseData, outputFilename, err := generateLicense(licenseName) if err != nil { return fmt.Errorf("failed to generate license %s: %w", licenseName, err) } - outputFile := filepath.Join(a.flags.output, licenseFilename) - if err := os.WriteFile(outputFile, []byte(license), os.ModePerm); err != nil { + outputFile := filepath.Join(a.flags.output, outputFilename) + if err := os.WriteFile(outputFile, []byte(licenseData), os.ModePerm); err != nil { return fmt.Errorf("failed to write file %s: %w", outputFile, err) }