-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicenses.go
90 lines (73 loc) · 2.09 KB
/
licenses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package gitgen
import (
"io"
"strings"
)
// GetLicenseText returns the text of a license
// The key is its SPDX identifier
func GetLicenseText(key string) string {
// Get raw embeded bytes
raw, _ := asset("licenses/" + key + ".txt")
// Make them a string
return string(raw)
}
// WriteLicense writes a license to a writer. It can be a file, a
// http response, etc
func WriteLicense(key string, w io.Writer) (n int, err error) {
// get the data from the embeded file
data, err := asset("licenses/" + key + ".txt")
if err != nil {
return
}
return w.Write(data)
}
// GetLicWithParams gets a license and adds the fullname and the
// year parameters to the license. Not all the licenses
// allow these fields
func GetLicWithParams(key, fullname, year string) string {
txt := GetLicenseText(key)
// This is a different function for testability
return replaceString(txt, fullname, year)
}
func replaceString(fullText, fullname, year string) string {
// Create a Replacer
r := makeLicenseReplacer(fullname, year)
// Execute the replacer
return r.Replace(fullText)
}
func WriteLicWithParams(key, fullname, year string,
w io.Writer) (int, error) {
// Get the text
txt := GetLicenseText(key)
// Call the helper function
return replaceWrite(txt, fullname, year, w)
}
func replaceWrite(fullText, fullname, year string,
w io.Writer) (int, error) {
// Create a Replacer
r := makeLicenseReplacer(fullname, year)
// Write it
return r.WriteString(w, fullText)
}
// A helper function to create a replacer.
// It is upgradable so more parameters could be aded
// in the future
func makeLicenseReplacer(fullname, year string) *strings.Replacer {
// More things like [YY] could be added here
return strings.NewReplacer(
// Normal style
"[year]", year,
"[yyyy]", year,
// Long style used by apache
"[fullname]", fullname,
"[name of copyright owner]", fullname,
// Style used by GNU
"<year>", year,
"<name of author>", fullname,
)
}
// ListLicenses returns a slice of strings containing
// the names of all available license templates
func ListLicenses() []string {
return listAssets("licenses")
}