forked from DataDog/dd-trace-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkcopyright.go
56 lines (52 loc) · 1.3 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
// 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-2019 Datadog, Inc.
// +build ignore
// This tool validates that all *.go files in the repository have the copyright text attached.
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
)
func main() {
var missing bool
copyrightText := []byte(fmt.Sprintf("// Copyright 2016-%s Datadog, Inc.", time.Now().Format("2006")))
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 !bytes.Contains(snip, copyrightText) {
// 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)
}
}