-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_license.go
77 lines (67 loc) · 1.46 KB
/
gen_license.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
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package main
import (
"bytes"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"time"
)
var (
year = strconv.FormatInt(int64(time.Now().Year()), 10)
licenseheader = []byte(`// Copyright ` + year + ` The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
`)
)
func license(path string, fi os.FileInfo, err error) error {
if fi.IsDir() {
switch filepath.Base(path) {
case "testdata":
return filepath.SkipDir
case "vendor":
return filepath.SkipDir
}
return nil
}
switch filepath.Ext(path) {
case ".go":
default:
return nil
}
changed := false
cmp, err := ioutil.ReadFile(path)
if err != nil {
return err
}
lhn := append(licenseheader, '\n')
if !bytes.Equal([]byte("// Copyright"), cmp[:12]) {
cmp = append(lhn, cmp...)
changed = true
}
if changed {
if *check {
return errors.New(fmt.Sprintf("Missing license in %s", path))
}
log.Println("Added license to", path)
return ioutil.WriteFile(path, cmp, fi.Mode().Perm())
}
return nil
}
var (
scan = flag.String("scan", "./", "set scan path")
check = flag.Bool("check", false, "just check if all files have license")
)
func main() {
flag.Parse()
if err := filepath.Walk(*scan, license); err != nil {
log.Fatal(err)
}
}