forked from AplaProject/go-apla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copyright.go
69 lines (63 loc) · 1.54 KB
/
copyright.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
// The program inserts copyright notice at the beginning of .go files.
var (
copyright []byte
)
// ProcessDir proceeds the specified directory and inserts the copyright at the beginning of the files
func ProcessDir(dir string, recurse bool) {
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
}
for _, file := range files {
fname := file.Name()
fullName := filepath.Join(dir, fname)
if strings.HasSuffix(fname, `.go`) {
fmt.Printf(fullName)
if fdata, err := ioutil.ReadFile(fullName); err != nil {
fmt.Println(err)
os.Exit(1)
} else {
if len(fdata) <= len(copyright) || bytes.Compare(fdata[:len(copyright)], copyright) != 0 {
off := bytes.Index(fdata, []byte(`package`))
if off == -1 {
fmt.Println(`...package has not been found`)
} else {
out := copyright
out = append(out, fdata[off:]...)
if err := ioutil.WriteFile(fullName, out, 0644); err == nil {
fmt.Println(`...Overwrited`)
} else {
fmt.Println(`...` + err.Error())
}
}
} else {
fmt.Println(`...Skipped`)
}
}
}
if recurse && file.IsDir() {
ProcessDir(fullName, recurse)
}
}
}
func main() {
var err error
if copyright, err = ioutil.ReadFile(`copyright.txt`); err == nil && len(copyright) > 0 {
if copyright[len(copyright)-1] != 0xa {
copyright = append(copyright, 0xa)
}
ProcessDir("../..", false)
ProcessDir("../../packages", true)
} else {
fmt.Println(err)
}
}