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
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
}

View File

@ -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)
}