feat: use template output filename
parent
107f6d1f10
commit
a17e3ff2a1
28
license.go
28
license.go
|
@ -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
|
||||||
|
outputFilename string
|
||||||
args []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
|
||||||
}
|
}
|
||||||
|
|
6
main.go
6
main.go
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue