refactor template parsing logic
Move the logic for selecting a license template based on user input into a standalone func (fetchTemplate), and add test cases for all code paths. Delay parsing predefined license templates. This allows the new fetchTemplate method to modify these templates before returning in the future (to add SPDX license information). Add tests to ensure that these templates must always parse properly. Rename copyrightData type to licenseData, since we will soon begin to add more than just copyright data here (SPDX ID). Rename prefix func to executeTemplate, since this better describes what the function is doing. These are all refactoring and cleanup changes; no behavioral changes.
This commit is contained in:
47
tmpl.go
47
tmpl.go
@@ -19,27 +19,50 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var licenseTemplate = make(map[string]*template.Template)
|
||||
|
||||
func init() {
|
||||
licenseTemplate["apache"] = template.Must(template.New("").Parse(tmplApache))
|
||||
licenseTemplate["mit"] = template.Must(template.New("").Parse(tmplMIT))
|
||||
licenseTemplate["bsd"] = template.Must(template.New("").Parse(tmplBSD))
|
||||
licenseTemplate["mpl"] = template.Must(template.New("").Parse(tmplMPL))
|
||||
var licenseTemplate = map[string]string{
|
||||
"apache": tmplApache,
|
||||
"mit": tmplMIT,
|
||||
"bsd": tmplBSD,
|
||||
"mpl": tmplMPL,
|
||||
}
|
||||
|
||||
type copyrightData struct {
|
||||
Year string
|
||||
Holder string
|
||||
// licenseData specifies the data used to fill out a license template.
|
||||
type licenseData struct {
|
||||
Year string // Copyright year(s).
|
||||
Holder string // Name of the copyright holder.
|
||||
}
|
||||
|
||||
// prefix will execute a license template t with data d
|
||||
// fetchTemplate returns the license template for the specified license and
|
||||
// optional templateFile. If templateFile is provided, the license is read
|
||||
// from the specified file. Otherwise, a template is loaded for the specified
|
||||
// license, if recognized.
|
||||
func fetchTemplate(license string, templateFile string) (string, error) {
|
||||
var t string
|
||||
if templateFile != "" {
|
||||
d, err := ioutil.ReadFile(templateFile)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("license file: %w", err)
|
||||
}
|
||||
|
||||
t = string(d)
|
||||
} else {
|
||||
t = licenseTemplate[license]
|
||||
if t == "" {
|
||||
return "", fmt.Errorf("unknown license: %q", license)
|
||||
}
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// executeTemplate will execute a license template t with data d
|
||||
// and prefix the result with top, middle and bottom.
|
||||
func prefix(t *template.Template, d *copyrightData, top, mid, bot string) ([]byte, error) {
|
||||
func executeTemplate(t *template.Template, d licenseData, top, mid, bot string) ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := t.Execute(&buf, d); err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user