feat: use template output filename

main
sudo pacman -Syu 2022-08-16 14:21:22 +07:00
parent 107f6d1f10
commit a17e3ff2a1
No known key found for this signature in database
GPG Key ID: D6CB5C6C567C47B0
2 changed files with 19 additions and 17 deletions

View File

@ -23,27 +23,29 @@ var embedFS embed.FS
// always use upercase for license name // always use upercase for license name
var templates = map[string]templateInfo{ var templates = map[string]templateInfo{
"MIT": { "MIT": {
filename: "mit.txt", templateFilename: "mit.txt",
outputFilename: "LICENSE",
args: []string{ args: []string{
"[year]", "[year]",
"[fullname]", "[fullname]",
}, },
}, },
"GNU GPLv3": { "GNU GPLv3": {
filename: "gnu_gplv3.txt", templateFilename: "gnu_gplv3.txt",
outputFilename: "COPYING",
}, },
} }
type templateInfo struct { type templateInfo struct {
filename string templateFilename string
args []string outputFilename string
args []string
} }
func generateLicense(name string) (string, error) { func generateLicense(name string) (string, string, error) {
if name == "" { if name == "" {
return "", fmt.Errorf("empty license name: %w", ErrInvalidLicense) return "", "", fmt.Errorf("empty license name: %w", ErrInvalidLicense)
} }
name = strings.ToUpper(name)
isSupportTemplate := false isSupportTemplate := false
var templateInfo templateInfo var templateInfo templateInfo
@ -55,26 +57,26 @@ func generateLicense(name string) (string, error) {
} }
if !isSupportTemplate { 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 // Get correct path of license
path := filepath.Join(templatesPath, templateInfo.filename) path := filepath.Join(templatesPath, templateInfo.templateFilename)
// Read template // Read template
templateRaw, err := embedFS.ReadFile(path) templateRaw, err := embedFS.ReadFile(path)
if err != nil { 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 // Replace template info args
template := string(templateRaw) licenseData := string(templateRaw)
for _, arg := range templateInfo.args { for _, arg := range templateInfo.args {
fmt.Printf("What is your %s: ", arg) fmt.Printf("What is your %s: ", arg)
value := ioe.ReadInput() value := ioe.ReadInput()
template = strings.ReplaceAll(template, arg, value) licenseData = strings.ReplaceAll(licenseData, arg, value)
} }
return template, nil return licenseData, templateInfo.outputFilename, nil
} }

View File

@ -78,13 +78,13 @@ func (a *action) RunGenerate(c *cli.Context) error {
fmt.Printf("What LICENSE do you chose: ") fmt.Printf("What LICENSE do you chose: ")
licenseName := ioe.ReadInput() licenseName := ioe.ReadInput()
license, err := generateLicense(licenseName) licenseData, outputFilename, err := generateLicense(licenseName)
if err != nil { if err != nil {
return fmt.Errorf("failed to generate license %s: %w", licenseName, err) return fmt.Errorf("failed to generate license %s: %w", licenseName, err)
} }
outputFile := filepath.Join(a.flags.output, licenseFilename) outputFile := filepath.Join(a.flags.output, outputFilename)
if err := os.WriteFile(outputFile, []byte(license), os.ModePerm); err != nil { if err := os.WriteFile(outputFile, []byte(licenseData), os.ModePerm); err != nil {
return fmt.Errorf("failed to write file %s: %w", outputFile, err) return fmt.Errorf("failed to write file %s: %w", outputFile, err)
} }