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

Commit

Permalink
feature: add boilerplate check script
Browse files Browse the repository at this point in the history
Signed-off-by: SataQiu <[email protected]>
  • Loading branch information
SataQiu committed Aug 12, 2019
1 parent 4e4cc17 commit 0ea6869
Show file tree
Hide file tree
Showing 56 changed files with 1,035 additions and 1 deletion.
16 changes: 16 additions & 0 deletions cmd/dfdaemon/app/init.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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 app

import (
Expand Down
16 changes: 16 additions & 0 deletions cmd/dfdaemon/app/root_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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 app

import (
Expand Down
16 changes: 16 additions & 0 deletions cmd/dfdaemon/app/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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 app

import (
Expand Down
16 changes: 16 additions & 0 deletions cmd/supernode/app/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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 app

import (
Expand Down
16 changes: 16 additions & 0 deletions dfdaemon/proxy/proxy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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 proxy

import (
Expand Down
16 changes: 16 additions & 0 deletions dfdaemon/proxy/proxy_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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 proxy

import (
Expand Down
16 changes: 16 additions & 0 deletions dfdaemon/server.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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 dfdaemon

import (
Expand Down
129 changes: 129 additions & 0 deletions hack/boilerplate/check-boilerplate.go
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)
}
}
65 changes: 65 additions & 0 deletions hack/boilerplate/check-boilerplate_test.go
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)
}
})
}
}
8 changes: 8 additions & 0 deletions hack/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ check() {
echo "CHECK: go vet, check code syntax"
packages=$(go list ./... | sed 's/^_//')
echo "${packages}" | xargs go vet 2>&1

# boilerplate check
echo "CHECK: boilerpalte, check code boilerplate"
result=$(git ls-files | xargs go run ./hack/boilerplate/check-boilerplate.go)
if [[ ${#result} -gt 0 ]]; then
echo "${result}"
return 1
fi
}

check
2 changes: 1 addition & 1 deletion pkg/ratelimiter/ratelimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright The Dragonfly Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use rl file except in compliance with 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
Expand Down
16 changes: 16 additions & 0 deletions pkg/util/metrics_util.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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 util

import (
Expand Down
16 changes: 16 additions & 0 deletions supernode/config/config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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 config

import (
Expand Down
Loading

0 comments on commit 0ea6869

Please sign in to comment.