diff --git a/.circleci/config.yml b/.circleci/config.yml index 7714a88eb..66e0cb0d1 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -49,6 +49,10 @@ jobs: name: use golangci-lint to check gocode of this project. command: | make golangci-lint + - run: + name: boilerplate check + command: | + make boilerplate-check unit-test-golang: docker: diff --git a/Makefile b/Makefile index fc6062b3d..551eb8ae7 100644 --- a/Makefile +++ b/Makefile @@ -105,13 +105,18 @@ integration-test: @go test ./test .PHONY: integration-test -check: +check: boilerplate-check @echo "Begin to check code formats." ./hack/check.sh @echo "Begin to check dockerd whether is startup" ./hack/check-docker.sh .PHONY: check +boilerplate-check: + @echo "Begin to check code boilerplate." + ./hack/boilerplate-check.sh +.PHONY: boilerplate-check + go-mod-tidy: @echo "Begin to tidy up go.mod and go.sum" @go mod tidy diff --git a/hack/boilerplate-check.sh b/hack/boilerplate-check.sh new file mode 100755 index 000000000..f4d0db156 --- /dev/null +++ b/hack/boilerplate-check.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -o nounset +set -o errexit +set -o pipefail + +curDir=$(cd "$(dirname "$0")" && pwd) +cd "${curDir}/../" || return + +check() { + # boilerplate check + echo "CHECK: boilerpalte, check code boilerplate" + result=$(git ls-files | xargs go run ./hack/boilerplate/check-boilerplate.go 2>&1) + if [[ ${#result} -gt 0 ]]; then + echo "${result}" + return 1 + fi +} + +check \ No newline at end of file diff --git a/hack/boilerplate/check-boilerplate.go b/hack/boilerplate/check-boilerplate.go index 96a972c85..2429910cd 100644 --- a/hack/boilerplate/check-boilerplate.go +++ b/hack/boilerplate/check-boilerplate.go @@ -17,7 +17,6 @@ package main import ( - "errors" "fmt" "io/ioutil" "os" @@ -78,7 +77,7 @@ func checkBoilerplate(content string) error { return fmt.Errorf("the file is missing a boilerplate") } if index < len(boilerplate) { - return errors.New("boilerplate has missing lines") + return fmt.Errorf("boilerplate has missing lines") } return nil } @@ -86,7 +85,7 @@ func checkBoilerplate(content string) error { // verifyFile verifies if a file contains the boilerplate func verifyFile(filePath string) error { if len(filePath) == 0 { - return errors.New("empty file name") + return fmt.Errorf("empty file name") } // check file extension is go @@ -112,18 +111,13 @@ func verifyFile(filePath string) error { func main() { if len(os.Args) < 2 { - fmt.Println("usage: go run check-boilerplate.go ...") + fmt.Fprintln(os.Stderr, "usage: go run check-boilerplate.go ...") 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 + fmt.Fprintf(os.Stderr, "error validating %q: %v\n", filePath, err) } } - if hasErr { - os.Exit(1) - } } diff --git a/hack/check.sh b/hack/check.sh index c5aa51bfc..045dc9e3f 100755 --- a/hack/check.sh +++ b/hack/check.sh @@ -10,7 +10,7 @@ cd "${curDir}/../" || return check() { # gofmt echo "CHECK: gofmt, check code formats" - result=$(find . -name '*.go' -print0 | xargs -0 gofmt -s -l -d 2>/dev/null) + result=$(find . ! -path './vendor*' -name '*.go' -print0 | xargs -0 gofmt -s -l -d 2>/dev/null) if [[ ${#result} -gt 0 ]]; then echo "${result}" echo "CHECK: please format Go code with 'gofmt -s -w .'" @@ -32,14 +32,6 @@ 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 diff --git a/supernode/daemon/mgr/gc/gc_dfget_task.go b/supernode/daemon/mgr/gc/gc_dfget_task.go index 833e1ad3f..d258c798e 100644 --- a/supernode/daemon/mgr/gc/gc_dfget_task.go +++ b/supernode/daemon/mgr/gc/gc_dfget_task.go @@ -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 gc import ( diff --git a/supernode/daemon/mgr/gc/gc_manager.go b/supernode/daemon/mgr/gc/gc_manager.go index 9e91ddc15..ec1dda195 100644 --- a/supernode/daemon/mgr/gc/gc_manager.go +++ b/supernode/daemon/mgr/gc/gc_manager.go @@ -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 gc import ( diff --git a/supernode/daemon/mgr/gc/gc_peer.go b/supernode/daemon/mgr/gc/gc_peer.go index 44724f8c1..f076979be 100644 --- a/supernode/daemon/mgr/gc/gc_peer.go +++ b/supernode/daemon/mgr/gc/gc_peer.go @@ -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 gc import ( diff --git a/supernode/daemon/mgr/gc/gc_task.go b/supernode/daemon/mgr/gc/gc_task.go index 9056aed9f..05a43be17 100644 --- a/supernode/daemon/mgr/gc/gc_task.go +++ b/supernode/daemon/mgr/gc/gc_task.go @@ -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 gc import ( diff --git a/supernode/daemon/mgr/gc_mgr.go b/supernode/daemon/mgr/gc_mgr.go index 71b5f51f4..d6e58f51c 100644 --- a/supernode/daemon/mgr/gc_mgr.go +++ b/supernode/daemon/mgr/gc_mgr.go @@ -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 mgr import ( diff --git a/supernode/daemon/mgr/progress/progress_delete.go b/supernode/daemon/mgr/progress/progress_delete.go index 44315cec5..2769e668d 100644 --- a/supernode/daemon/mgr/progress/progress_delete.go +++ b/supernode/daemon/mgr/progress/progress_delete.go @@ -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 progress import (