forked from DataDog/dd-trace-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkcopyright.go
60 lines (55 loc) · 1.58 KB
/
checkcopyright.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
//go:build ignore
// +build ignore
// This tool validates that all *.go files in the repository have the copyright text attached.
package main
import (
"io"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
var missing bool
// copyrightRegexp matches years or year ranges like "2016", "2016-2019",
// "2016,2018-2020", "2016-present" in the copyright header.
copyrightRegexp := regexp.MustCompile(`// Copyright 20[0-9]{2}[0-9,\-(present)]* Datadog, Inc.`)
generatedRegexp := regexp.MustCompile(`Code generated by.+DO NOT EDIT`)
if err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) != ".go" || info.IsDir() || strings.Contains(path, "vendor") {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
// read 1KB, header should be there
snip := make([]byte, 1024)
_, err = f.Read(snip)
if err != nil && err != io.EOF {
return err
}
if !copyrightRegexp.Match(snip) && !generatedRegexp.Match(snip) {
// report missing header
missing = true
log.Printf("Copyright header missing in %q.\n", path)
}
return nil
}); err != nil {
log.Fatal(err)
}
if missing {
// some files are missing the header, exit code 1 to fail CI
os.Exit(1)
}
log.Printf("All files satisfied the copyright check.")
}