2021-01-21 07:32:21 +00:00
|
|
|
package main
|
2021-01-21 08:15:34 +00:00
|
|
|
|
|
|
|
import (
|
2021-03-16 09:57:29 +00:00
|
|
|
"embed"
|
2022-08-16 05:59:42 +00:00
|
|
|
"errors"
|
2021-01-21 08:15:34 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-04-16 17:31:57 +00:00
|
|
|
|
2021-12-02 07:08:28 +00:00
|
|
|
"github.com/make-go-great/ioe-go"
|
2021-01-21 08:15:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
templatesPath = "templates"
|
|
|
|
)
|
|
|
|
|
2022-08-16 05:59:42 +00:00
|
|
|
var ErrInvalidLicense = errors.New("invalid license")
|
|
|
|
|
2021-03-16 09:57:29 +00:00
|
|
|
//go:embed templates/*
|
|
|
|
var embedFS embed.FS
|
|
|
|
|
2021-01-21 08:15:34 +00:00
|
|
|
// map template name with filename
|
2021-03-16 10:14:05 +00:00
|
|
|
// always use upercase for license name
|
2021-01-21 08:15:34 +00:00
|
|
|
var templates = map[string]templateInfo{
|
|
|
|
"MIT": {
|
2021-01-21 08:41:40 +00:00
|
|
|
filename: "mit.txt",
|
2021-01-21 08:15:34 +00:00
|
|
|
args: []string{
|
|
|
|
"[year]",
|
|
|
|
"[fullname]",
|
|
|
|
},
|
|
|
|
},
|
2022-08-16 06:41:48 +00:00
|
|
|
"GNU GPLv3": {
|
|
|
|
filename: "gnu_gplv3.txt",
|
|
|
|
},
|
2021-01-21 08:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type templateInfo struct {
|
|
|
|
filename string
|
|
|
|
args []string
|
|
|
|
}
|
|
|
|
|
2021-01-21 08:41:40 +00:00
|
|
|
func generateLicense(name string) (string, error) {
|
2021-01-21 08:15:34 +00:00
|
|
|
if name == "" {
|
2022-08-16 05:59:42 +00:00
|
|
|
return "", fmt.Errorf("empty license name: %w", ErrInvalidLicense)
|
2021-01-21 08:15:34 +00:00
|
|
|
}
|
2021-03-16 10:14:05 +00:00
|
|
|
name = strings.ToUpper(name)
|
2021-01-21 08:15:34 +00:00
|
|
|
|
2022-08-16 06:41:48 +00:00
|
|
|
isSupportTemplate := false
|
|
|
|
var templateInfo templateInfo
|
|
|
|
for templateName := range templates {
|
|
|
|
if strings.EqualFold(templateName, name) {
|
|
|
|
isSupportTemplate = true
|
|
|
|
templateInfo = templates[templateName]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isSupportTemplate {
|
2022-08-16 05:59:42 +00:00
|
|
|
return "", fmt.Errorf("not support license %s: %w", name, ErrInvalidLicense)
|
2021-01-21 08:15:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-16 09:57:29 +00:00
|
|
|
// Get correct path of license
|
2021-01-21 08:15:34 +00:00
|
|
|
path := filepath.Join(templatesPath, templateInfo.filename)
|
2021-03-16 09:57:29 +00:00
|
|
|
|
|
|
|
// Read template
|
|
|
|
templateRaw, err := embedFS.ReadFile(path)
|
2021-01-21 08:15:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to read file %s: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace template
|
|
|
|
template := string(templateRaw)
|
|
|
|
for _, arg := range templateInfo.args {
|
2021-01-21 08:41:40 +00:00
|
|
|
fmt.Printf("What is your %s: ", arg)
|
2021-04-16 17:31:57 +00:00
|
|
|
value := ioe.ReadInput()
|
2021-01-21 08:15:34 +00:00
|
|
|
|
|
|
|
template = strings.ReplaceAll(template, arg, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return template, nil
|
|
|
|
}
|