Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dragonfly (new-807) $ git ls-files | xargs go run ./hack/boilerplate/check-boilerplate.go
Dragonfly (new-807) $ echo $?
0
version (new-807) $ vi version/version.go
Dragonfly (new-807) $ git ls-files | xargs go run ./hack/boilerplate/check-boilerplate.go
error validating "version/version.go": the file is missing a boilerplate
exit status 1

It works with the check file.

While I am afraid that make check fails since there is no docker daemon installed on my machine. I think make file should be friendly with developer's local machine, like windows, mac and so on. And there is no need to depend on the docker engine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I am also curious whether the ./hack/check-docker.sh can be stripped out from make check.
But hack/check.sh is OK, right?

check:
	@echo "Begin to check code formats."
	./hack/check.sh
	@echo "Begin to check dockerd whether is startup"
	./hack/check-docker.sh
.PHONY: check

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whether the ./hack/check-docker.sh can be stripped out from make check.

I think we should strip that from make check.

hack/hack.sh is OK.

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