-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
458 lines (402 loc) · 12.9 KB
/
main.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This program ensures source code files have copyright license headers.
// See usage with "addlicense -h".
package addlicense
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"text/template"
"time"
doublestar "github.com/bmatcuk/doublestar/v4"
"golang.org/x/sync/errgroup"
)
const helpText = `Usage: addlicense [flags] pattern [pattern ...]
The program ensures source code files have copyright license headers
by scanning directory patterns recursively.
It modifies all source files in place and avoids adding a license header
to any file that already has one.
The pattern argument can be provided multiple times, and may also refer
to single files.
Flags:
`
var (
skipExtensionFlags stringSlice
ignorePatterns stringSlice
spdx spdxFlag
holder = flag.String("c", "Google LLC", "copyright holder")
license = flag.String("l", "apache", "license type: apache, bsd, mit, mpl")
licensef = flag.String("f", "", "license file")
year = flag.String("y", fmt.Sprint(time.Now().Year()), "copyright year(s)")
verbose = flag.Bool("v", false, "verbose mode: print the name of the files that are modified")
checkonly = flag.Bool("check", false, "check only mode: verify presence of license headers and exit with non-zero code if missing")
)
func init() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, helpText)
flag.PrintDefaults()
}
flag.Var(&skipExtensionFlags, "skip", "[deprecated: see -ignore] file extensions to skip, for example: -skip rb -skip go")
flag.Var(&ignorePatterns, "ignore", "file patterns to ignore, for example: -ignore **/*.go -ignore vendor/**")
flag.Var(&spdx, "s", "Include SPDX identifier in license header. Set -s=only to only include SPDX identifier.")
}
// stringSlice stores the results of a repeated command line flag as a string slice.
type stringSlice []string
func (i *stringSlice) String() string {
return fmt.Sprint(*i)
}
func (i *stringSlice) Set(value string) error {
*i = append(*i, value)
return nil
}
// spdxFlag defines the line flag behavior for specifying SPDX support.
type spdxFlag string
const (
spdxOff spdxFlag = ""
spdxOn spdxFlag = "true" // value set by flag package on bool flag
spdxOnly spdxFlag = "only"
)
// IsBoolFlag causes a bare '-s' flag to be set as the string 'true'. This
// allows the use of the bare '-s' or setting a string '-s=only'.
func (i *spdxFlag) IsBoolFlag() bool { return true }
func (i *spdxFlag) String() string { return string(*i) }
func (i *spdxFlag) Set(value string) error {
v := spdxFlag(value)
if v != spdxOn && v != spdxOnly {
return fmt.Errorf("error: flag 's' expects '%v' or '%v'", spdxOn, spdxOnly)
}
*i = v
return nil
}
func main() {
flag.Parse()
if flag.NArg() == 0 {
flag.Usage()
os.Exit(1)
}
// Get non-flag command-line args
patterns := flag.Args()
// convert -skip flags to -ignore equivalents
for _, s := range skipExtensionFlags {
ignorePatterns = append(ignorePatterns, fmt.Sprintf("**/*.%s", s))
}
// map legacy license values
if t, ok := legacyLicenseTypes[*license]; ok {
*license = t
}
data := LicenseData{
Year: *year,
Holder: *holder,
SPDXID: *license,
}
// create logger to print updates to stdout
logger := log.Default()
// real main
err := Run(
ignorePatterns,
spdx,
data,
*licensef,
*verbose,
*checkonly,
patterns,
logger,
)
if err != nil {
if err.Error() == "missing license header" {
// this retains the historical behavior of addLicense, which is to give a
// non-zero exit code when the -check flag is used and headers are needed
os.Exit(1)
} else {
log.Fatal(err)
}
}
}
func validatePatterns(patterns []string) error {
invalidPatterns := []string{}
for _, p := range patterns {
if !doublestar.ValidatePattern(p) {
invalidPatterns = append(invalidPatterns, p)
}
}
if len(invalidPatterns) == 1 {
return fmt.Errorf("headerignore pattern %q is not valid", invalidPatterns[0])
} else if len(invalidPatterns) > 1 {
return fmt.Errorf("headerignore patterns %q are not valid", strings.Join(invalidPatterns[:], `, `))
}
return nil
}
// Run executes addLicense with supplied variables
func Run(
ignorePatternList []string,
spdx spdxFlag,
license LicenseData,
licenseFileOverride string, // Provide a file to use as the license header
verbose bool,
checkonly bool,
patterns []string,
logger *log.Logger,
) error {
// verify that all ignorePatterns are valid
err := validatePatterns(ignorePatternList)
if err != nil {
return err
}
ignorePatterns = ignorePatternList
tpl, err := fetchTemplate(license.SPDXID, licenseFileOverride, spdx)
if err != nil {
return err
}
t, err := template.New("").Parse(tpl)
if err != nil {
return err
}
// process at most 1000 files in parallel
ch := make(chan *file, 1000)
done := make(chan struct{})
var out error
go func() {
var wg errgroup.Group
for f := range ch {
f := f // https://golang.org/doc/faq#closures_and_goroutines
wg.Go(func() error {
err := processFile(f, t, license, checkonly, verbose, logger)
return err
})
}
out = wg.Wait()
close(done)
}()
for _, d := range patterns {
if err := walk(ch, d, logger); err != nil {
return err
}
}
close(ch)
<-done
return out
}
func processFile(f *file, t *template.Template, license LicenseData, checkonly bool, verbose bool, logger *log.Logger) error {
if checkonly {
// Check if file extension is known
lic, err := licenseHeader(f.path, t, license)
if err != nil {
logger.Printf("%s: %v", f.path, err)
return err
}
if lic == nil { // Unknown fileExtension
return nil
}
// Check if file has a license
hasLicense, err := fileHasLicense(f.path)
if err != nil {
logger.Printf("%s: %v", f.path, err)
return err
}
if !hasLicense {
logger.Printf("%s\n", f.path)
return errors.New("missing license header")
}
} else {
modified, err := addLicense(f.path, f.mode, t, license)
if err != nil {
logger.Printf("%s: %v", f.path, err)
return err
}
if verbose && modified {
logger.Printf("%s modified", f.path)
}
}
return nil
}
type file struct {
path string
mode os.FileMode
}
func walk(ch chan<- *file, start string, logger *log.Logger) error {
return filepath.Walk(start, func(path string, fi os.FileInfo, err error) error {
if err != nil {
logger.Printf("%s error: %v", path, err)
return nil
}
if fi.IsDir() {
return nil
}
if fileMatches(path, ignorePatterns) {
// The [DEBUG] level is inferred by go-hclog as a debug statement
logger.Printf("[DEBUG] skipping: %s", path)
return nil
}
ch <- &file{path, fi.Mode()}
return nil
})
}
// fileMatches determines if path matches one of the provided file patterns.
// Patterns are assumed to be valid.
func fileMatches(path string, patterns []string) bool {
for _, p := range patterns {
if runtime.GOOS == "windows" {
// If on windows, change path seperators to /
// in order for patterns to compare correctly
path = filepath.ToSlash(path)
}
// ignore error, since we assume patterns are valid
if match, _ := doublestar.Match(p, path); match {
return true
}
}
return false
}
// addLicense add a license to the file if missing.
//
// It returns true if the file was updated.
func addLicense(path string, fmode os.FileMode, tmpl *template.Template, data LicenseData) (bool, error) {
var lic []byte
var err error
lic, err = licenseHeader(path, tmpl, data)
if err != nil || lic == nil {
return false, err
}
b, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
if hasLicense(b) || isGenerated(b) {
return false, err
}
line := hashBang(b)
if len(line) > 0 {
b = b[len(line):]
if line[len(line)-1] != '\n' {
line = append(line, '\n')
}
lic = append(line, lic...)
}
b = append(lic, b...)
return true, ioutil.WriteFile(path, b, fmode)
}
// fileHasLicense reports whether the file at path contains a license header.
func fileHasLicense(path string) (bool, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
// If generated, we count it as if it has a license.
return hasLicense(b) || isGenerated(b), nil
}
// licenseHeader populates the provided license template with data, and returns
// it with the proper prefix for the file type specified by path. The file does
// not need to actually exist, only its name is used to determine the prefix.
func licenseHeader(path string, tmpl *template.Template, data LicenseData) ([]byte, error) {
var lic []byte
var err error
base := strings.ToLower(filepath.Base(path))
switch fileExtension(base) {
case ".c", ".h", ".gv", ".java", ".scala", ".kt", ".kts":
lic, err = executeTemplate(tmpl, data, "/*", " * ", " */")
case ".js", ".mjs", ".cjs", ".jsx", ".tsx", ".css", ".scss", ".sass", ".ts":
lic, err = executeTemplate(tmpl, data, "/**", " * ", " */")
case ".cc", ".cpp", ".cs", ".go", ".hh", ".hpp", ".m", ".mm", ".proto", ".rs", ".swift", ".dart", ".groovy", ".v", ".sv", ".lr":
lic, err = executeTemplate(tmpl, data, "", "// ", "")
case ".py", ".sh", ".bash", ".zsh", ".yaml", ".yml", ".dockerfile", "dockerfile", ".rb", "gemfile", ".ru", ".tcl", ".hcl", ".tf", ".tfvars", ".nomad", ".bzl", ".pl", ".pp", ".ps1", ".psd1", ".psm1":
lic, err = executeTemplate(tmpl, data, "", "# ", "")
case ".el", ".lisp":
lic, err = executeTemplate(tmpl, data, "", ";; ", "")
case ".erl":
lic, err = executeTemplate(tmpl, data, "", "% ", "")
case ".hs", ".sql", ".sdl":
lic, err = executeTemplate(tmpl, data, "", "-- ", "")
case ".hbs":
lic, err = executeTemplate(tmpl, data, "{{!", " ", "}}")
case ".html", ".htm", ".xml", ".vue", ".wxi", ".wxl", ".wxs":
lic, err = executeTemplate(tmpl, data, "<!--", " ", "-->")
case ".php":
lic, err = executeTemplate(tmpl, data, "", "// ", "")
case ".ml", ".mli", ".mll", ".mly":
lic, err = executeTemplate(tmpl, data, "(**", " ", "*)")
case ".ejs":
lic, err = executeTemplate(tmpl, data, "<%/*", " ", "*/%>")
default:
// handle various cmake files
if base == "cmakelists.txt" || strings.HasSuffix(base, ".cmake.in") || strings.HasSuffix(base, ".cmake") {
lic, err = executeTemplate(tmpl, data, "", "# ", "")
}
}
return lic, err
}
// fileExtension returns the file extension of name, or the full name if there
// is no extension.
func fileExtension(name string) string {
if v := filepath.Ext(name); v != "" {
return v
}
return name
}
var head = []string{
"#!", // shell script
"<?xml", // XML declaratioon
"<!doctype", // HTML doctype
"# encoding:", // Ruby encoding
"# frozen_string_literal:", // Ruby interpreter instruction
"#\\", // Ruby Rack directive https://github.com/rack/rack/wiki/(tutorial)-rackup-howto
"<?php", // PHP opening tag
"# escape", // Dockerfile directive https://docs.docker.com/engine/reference/builder/#parser-directives
"# syntax", // Dockerfile directive https://docs.docker.com/engine/reference/builder/#parser-directives
"/** @jest-environment", // Jest Environment string https://jestjs.io/docs/configuration#testenvironment-string
}
func hashBang(b []byte) []byte {
var line []byte
for _, c := range b {
line = append(line, c)
if c == '\n' {
break
}
}
first := strings.ToLower(string(line))
for _, h := range head {
if strings.HasPrefix(first, h) {
return line
}
}
return nil
}
// go generate: ^// Code generated .* DO NOT EDIT\.$
var goGenerated = regexp.MustCompile(`(?m)^.{1,2} Code generated .* DO NOT EDIT\.$`)
// cargo raze: ^DO NOT EDIT! Replaced on runs of cargo-raze$
var cargoRazeGenerated = regexp.MustCompile(`(?m)^DO NOT EDIT! Replaced on runs of cargo-raze$`)
// terraform init: ^# This file is maintained automatically by "terraform init"\.$
var terraformGenerated = regexp.MustCompile(`(?m)^# This file is maintained automatically by "terraform init"\.$`)
// isGenerated returns true if it contains a string that implies the file was
// generated.
func isGenerated(b []byte) bool {
return goGenerated.Match(b) || cargoRazeGenerated.Match(b) || terraformGenerated.Match(b)
}
func hasLicense(b []byte) bool {
n := 1000
if len(b) < 1000 {
n = len(b)
}
return bytes.Contains(bytes.ToLower(b[:n]), []byte("copyright")) ||
bytes.Contains(bytes.ToLower(b[:n]), []byte("mozilla public")) ||
bytes.Contains(bytes.ToLower(b[:n]), []byte("spdx-license-identifier"))
}