/
generate.go
295 lines (253 loc) · 5.78 KB
/
generate.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package licrep
import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"go/build"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/kopoli/appkit"
)
// chunker is a writer which writes given data to output and emits a newline
// every lineLength bytes received.
type chunker struct {
output io.Writer
linePos int
lineLength int
}
// Write writes the given data to w.output and every w.lineLength bytes writes
// a newline character.
func (w *chunker) Write(p []byte) (int, error) {
if w.lineLength == 0 {
w.lineLength = 80
}
lines := 0
for idx := 0; idx < len(p); {
writeLen := w.lineLength - w.linePos
if writeLen+idx > len(p) {
writeLen = len(p) - idx
}
w.linePos = (w.linePos + writeLen) % w.lineLength
writeLen, err := w.output.Write(p[idx : idx+writeLen])
if err != nil {
return 0, err
}
idx += writeLen
if w.linePos == 0 {
_, err = w.output.Write([]byte("\n"))
if err != nil {
return 0, err
}
lines++
}
}
return len(p) + lines, nil
}
func encodeData(input string) (out string, err error) {
data := &bytes.Buffer{}
ch := &chunker{output: data}
bw := base64.NewEncoder(base64.StdEncoding, ch)
w, err := gzip.NewWriterLevel(bw, gzip.BestCompression)
if err != nil {
return "", err
}
_, err = w.Write([]byte(input))
err2 := w.Close()
err3 := bw.Close()
if err != nil {
return "", err
}
if err2 != nil {
err = err2
}
if err3 != nil {
err = err3
}
if err != nil {
return "", err
}
out = data.String()
return out, nil
}
// FilterPackages filters out the ignored packages from the list
func FilterPackages(opts appkit.Options, pkgs []Package) (ret []Package) {
// Prepare the list of ignored packages
ignorestr := opts.Get("ignore-packages", "")
var ignores []string
if ignorestr != "" {
ignores = strings.Split(ignorestr, " ")
}
// Check if the package has been ignored
var ignored bool
for i := range pkgs {
ignored = false
for j := range ignores {
re, err := regexp.Compile(ignores[j])
if err != nil {
continue
}
if re.MatchString(pkgs[i].Name) || re.MatchString(pkgs[i].ImportPath) {
ignored = true
}
}
if !ignored {
ret = append(ret, pkgs[i])
}
}
return
}
// determinePackage determines the package that is written to the generated
// file
func determinePackage(opts appkit.Options) (string, error) {
pkg := opts.Get("package", "")
// Return the package given explicitly in the command line
if pkg != "" {
return pkg, nil
}
outfile := opts.Get("output", "")
var dir string
var err error
if outfile == "" {
// If outfile is stdout, return the package in the current working
// directory
dir, err = os.Getwd()
if err != nil {
return "", err
}
} else {
// If outfile is a filename, get the package of that directory.
dir, err = filepath.Abs(outfile)
if err != nil {
return "", err
}
dir = filepath.Dir(dir)
}
p, err := build.Import(".", dir, 0)
if err != nil {
return "", err
}
return p.Name, nil
}
// GenerateEmbeddedLicenses creates a compressed license representation to a
// file or os.Stdout of the given Packages. The representation is go so it can
// be built as part of a program.
func GenerateEmbeddedLicenses(opts appkit.Options, pkgs []Package) error {
licenseData := &bytes.Buffer{}
var str string
var err error
for i := range pkgs {
str = ""
if pkgs[i].LicenseString != "" {
str, err = encodeData(pkgs[i].LicenseString)
if err != nil {
return err
}
}
licenseData.WriteString(fmt.Sprintf(`
"%s": {
Name: "%s",
Text: `+"`\n%s`"+`,
length: %d,
},`, pkgs[i].ImportPath, pkgs[i].License, str,
len(pkgs[i].LicenseString)))
}
pkg, err := determinePackage(opts)
if err != nil {
return err
}
argmap := map[string]string{
"programName": opts.Get("program-name", ""),
"programVersion": opts.Get("program-version", ""),
"programArgs": opts.Get("program-args", ""),
"prefix": opts.Get("prefix", ""),
"package": pkg,
"data": licenseData.String(),
}
var out io.Writer = os.Stdout
outfile := opts.Get("output", "")
if outfile != "" {
var fp *os.File
fp, err = os.Create(outfile)
if err != nil {
return err
}
out = fp
defer func() {
err = fp.Close()
}()
}
tmpl, err := template.New("licrep").Parse(licenseTemplate)
if err != nil {
return err
}
err = tmpl.Execute(out, argmap)
return nil
}
const (
licenseTemplate string = `// Generated with by {{.programName}} version {{.programVersion}}
// https://github.com/kopoli/licrep
// Called with: {{.programArgs}}
package {{.package}}
import (
"bytes"
"compress/gzip"
"encoding/base64"
"io"
"strings"
)
// {{.prefix}}License is a representation of an embedded license.
type {{.prefix}}License struct {
// Name of the license
Name string
// The text of the license
Text string
}
// {{.prefix}}GetLicenses gets a map of {{.prefix}}Licenses where the keys are
// the package names.
func {{.prefix}}GetLicenses() (map[string]{{.prefix}}License, error) {
type EncodedLicense struct {
Name string
Text string
length int64
}
data := map[string]EncodedLicense{
{{.data}}
}
decode := func(input string, length int64) (string, error) {
data := &bytes.Buffer{}
br := base64.NewDecoder(base64.StdEncoding, strings.NewReader(input))
r, err := gzip.NewReader(br)
if err != nil {
return "", err
}
_, err = io.CopyN(data, r, length)
if err != nil {
return "", err
}
// Make sure the gzip is decoded successfully
err = r.Close()
if err != nil {
return "", err
}
return data.String(), nil
}
ret := make(map[string]{{.prefix}}License)
for k := range data {
text, err := decode(data[k].Text, data[k].length)
if err != nil {
return nil, err
}
ret[k] = {{.prefix}}License{
Name: data[k].Name,
Text: text,
}
}
return ret, nil
}
`
)