This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add boilerplate check script
Signed-off-by: SataQiu <[email protected]>
- Loading branch information
Showing
56 changed files
with
1,035 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
var ( | ||
start = " * Copyright " | ||
end = " * limitations under the License." | ||
boilerplate = []string{ | ||
` * Copyright The Dragonfly Authors.`, | ||
` *`, | ||
` * Licensed under the Apache License, Version 2.0 (the "License");`, | ||
` * you may not use this file except in compliance with the License.`, | ||
` * You may obtain a copy of the License at`, | ||
` *`, | ||
` * http://www.apache.org/licenses/LICENSE-2.0`, | ||
` *`, | ||
` * Unless required by applicable law or agreed to in writing, software`, | ||
` * distributed under the License is distributed on an "AS IS" BASIS,`, | ||
` * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.`, | ||
` * See the License for the specific language governing permissions and`, | ||
` * limitations under the License.`, | ||
} | ||
) | ||
|
||
// checkBoilerplate checks if the input string contains the boilerplate | ||
func checkBoilerplate(content string) error { | ||
// ignore generated files | ||
if strings.Contains(content, "DO NOT EDIT") { | ||
return nil | ||
} | ||
|
||
index := 0 | ||
foundStart := false | ||
lines := strings.Split(content, "\n") | ||
for _, line := range lines { | ||
// find the start of the boilerplate | ||
bpLine := boilerplate[index] | ||
if strings.Contains(line, start) { | ||
foundStart = true | ||
} | ||
|
||
// match line by line | ||
if foundStart { | ||
if line != bpLine { | ||
return fmt.Errorf("boilerplate line %d does not match\nexpected: %q\ngot: %q", index+1, bpLine, line) | ||
} | ||
index++ | ||
// exit after the last line is found | ||
if strings.Index(line, end) == 0 { | ||
break | ||
} | ||
} | ||
} | ||
|
||
if !foundStart { | ||
return fmt.Errorf("the file is missing a boilerplate") | ||
} | ||
if index < len(boilerplate) { | ||
return errors.New("boilerplate has missing lines") | ||
} | ||
return nil | ||
} | ||
|
||
// verifyFile verifies if a file contains the boilerplate | ||
func verifyFile(filePath string) error { | ||
if len(filePath) == 0 { | ||
return errors.New("empty file name") | ||
} | ||
|
||
// check file extension is go | ||
idx := strings.LastIndex(filePath, ".") | ||
if idx == -1 { | ||
return nil | ||
} | ||
|
||
// check if the file has a supported extension | ||
ext := filePath[idx : idx+len(filePath)-idx] | ||
if ext != ".go" { | ||
return nil | ||
} | ||
|
||
// read the file | ||
b, err := ioutil.ReadFile(filePath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return checkBoilerplate(string(b)) | ||
} | ||
|
||
func main() { | ||
if len(os.Args) < 2 { | ||
fmt.Println("usage: go run check-boilerplate.go <path-to-file> <path-to-file> ...") | ||
os.Exit(1) | ||
} | ||
|
||
hasErr := false | ||
for _, filePath := range os.Args[1:] { | ||
if err := verifyFile(filePath); err != nil { | ||
fmt.Printf("error validating %q: %v\n", filePath, err) | ||
hasErr = true | ||
} | ||
} | ||
if hasErr { | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestVerifyBoilerPlate(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
bp string | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "valid: boilerplate is valid", | ||
bp: `/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/`, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "invalid: missing lines", | ||
bp: ` | ||
* Copyright The Dragonfly Authors. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
`, | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
if err := checkBoilerplate(tc.bp); err != nil != tc.expectedError { | ||
t.Errorf("expected error: %v, got: %v, error: %v", tc.expectedError, err != nil, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.