Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit 13322ca

Browse files
authored
Merge pull request #807 from SataQiu/feature-add-boilerplate-check
feature: add boilerplate check script
2 parents de3fc03 + 0ea6869 commit 13322ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1035
-1
lines changed

cmd/dfdaemon/app/init.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package app
218

319
import (

cmd/dfdaemon/app/root_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package app
218

319
import (

cmd/dfdaemon/app/version.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package app
218

319
import (

cmd/supernode/app/version.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package app
218

319
import (

dfdaemon/proxy/proxy.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package proxy
218

319
import (

dfdaemon/proxy/proxy_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package proxy
218

319
import (

dfdaemon/server.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package dfdaemon
218

319
import (
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"io/ioutil"
23+
"os"
24+
"strings"
25+
)
26+
27+
var (
28+
start = " * Copyright "
29+
end = " * limitations under the License."
30+
boilerplate = []string{
31+
` * Copyright The Dragonfly Authors.`,
32+
` *`,
33+
` * Licensed under the Apache License, Version 2.0 (the "License");`,
34+
` * you may not use this file except in compliance with the License.`,
35+
` * You may obtain a copy of the License at`,
36+
` *`,
37+
` * http://www.apache.org/licenses/LICENSE-2.0`,
38+
` *`,
39+
` * Unless required by applicable law or agreed to in writing, software`,
40+
` * distributed under the License is distributed on an "AS IS" BASIS,`,
41+
` * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.`,
42+
` * See the License for the specific language governing permissions and`,
43+
` * limitations under the License.`,
44+
}
45+
)
46+
47+
// checkBoilerplate checks if the input string contains the boilerplate
48+
func checkBoilerplate(content string) error {
49+
// ignore generated files
50+
if strings.Contains(content, "DO NOT EDIT") {
51+
return nil
52+
}
53+
54+
index := 0
55+
foundStart := false
56+
lines := strings.Split(content, "\n")
57+
for _, line := range lines {
58+
// find the start of the boilerplate
59+
bpLine := boilerplate[index]
60+
if strings.Contains(line, start) {
61+
foundStart = true
62+
}
63+
64+
// match line by line
65+
if foundStart {
66+
if line != bpLine {
67+
return fmt.Errorf("boilerplate line %d does not match\nexpected: %q\ngot: %q", index+1, bpLine, line)
68+
}
69+
index++
70+
// exit after the last line is found
71+
if strings.Index(line, end) == 0 {
72+
break
73+
}
74+
}
75+
}
76+
77+
if !foundStart {
78+
return fmt.Errorf("the file is missing a boilerplate")
79+
}
80+
if index < len(boilerplate) {
81+
return errors.New("boilerplate has missing lines")
82+
}
83+
return nil
84+
}
85+
86+
// verifyFile verifies if a file contains the boilerplate
87+
func verifyFile(filePath string) error {
88+
if len(filePath) == 0 {
89+
return errors.New("empty file name")
90+
}
91+
92+
// check file extension is go
93+
idx := strings.LastIndex(filePath, ".")
94+
if idx == -1 {
95+
return nil
96+
}
97+
98+
// check if the file has a supported extension
99+
ext := filePath[idx : idx+len(filePath)-idx]
100+
if ext != ".go" {
101+
return nil
102+
}
103+
104+
// read the file
105+
b, err := ioutil.ReadFile(filePath)
106+
if err != nil {
107+
return err
108+
}
109+
110+
return checkBoilerplate(string(b))
111+
}
112+
113+
func main() {
114+
if len(os.Args) < 2 {
115+
fmt.Println("usage: go run check-boilerplate.go <path-to-file> <path-to-file> ...")
116+
os.Exit(1)
117+
}
118+
119+
hasErr := false
120+
for _, filePath := range os.Args[1:] {
121+
if err := verifyFile(filePath); err != nil {
122+
fmt.Printf("error validating %q: %v\n", filePath, err)
123+
hasErr = true
124+
}
125+
}
126+
if hasErr {
127+
os.Exit(1)
128+
}
129+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"testing"
21+
)
22+
23+
func TestVerifyBoilerPlate(t *testing.T) {
24+
testcases := []struct {
25+
name string
26+
bp string
27+
expectedError bool
28+
}{
29+
{
30+
name: "valid: boilerplate is valid",
31+
bp: `/*
32+
* Copyright The Dragonfly Authors.
33+
*
34+
* Licensed under the Apache License, Version 2.0 (the "License");
35+
* you may not use this file except in compliance with the License.
36+
* You may obtain a copy of the License at
37+
*
38+
* http://www.apache.org/licenses/LICENSE-2.0
39+
*
40+
* Unless required by applicable law or agreed to in writing, software
41+
* distributed under the License is distributed on an "AS IS" BASIS,
42+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43+
* See the License for the specific language governing permissions and
44+
* limitations under the License.
45+
*/`,
46+
expectedError: false,
47+
},
48+
{
49+
name: "invalid: missing lines",
50+
bp: `
51+
* Copyright The Dragonfly Authors.
52+
* Licensed under the Apache License, Version 2.0 (the "License");
53+
`,
54+
expectedError: true,
55+
},
56+
}
57+
58+
for _, tc := range testcases {
59+
t.Run(tc.name, func(t *testing.T) {
60+
if err := checkBoilerplate(tc.bp); err != nil != tc.expectedError {
61+
t.Errorf("expected error: %v, got: %v, error: %v", tc.expectedError, err != nil, err)
62+
}
63+
})
64+
}
65+
}

hack/check.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ check() {
3232
echo "CHECK: go vet, check code syntax"
3333
packages=$(go list ./... | sed 's/^_//')
3434
echo "${packages}" | xargs go vet 2>&1
35+
36+
# boilerplate check
37+
echo "CHECK: boilerpalte, check code boilerplate"
38+
result=$(git ls-files | xargs go run ./hack/boilerplate/check-boilerplate.go)
39+
if [[ ${#result} -gt 0 ]]; then
40+
echo "${result}"
41+
return 1
42+
fi
3543
}
3644

3745
check

0 commit comments

Comments
 (0)