From 3149d9d9d36b83b524b80e43d2fc4f0d062a0f5a Mon Sep 17 00:00:00 2001 From: yeya24 Date: Thu, 15 Aug 2019 18:58:21 +0800 Subject: [PATCH] support run golangci-lint locally and in docker Signed-off-by: yeya24 --- .circleci/config.yml | 20 ++---- .golangci.yml | 3 - Makefile | 4 ++ .../p2p_downloader/p2p_downloader.go | 5 +- hack/build.sh | 2 +- hack/golangci-lint.sh | 67 +++++++++++++++++++ 6 files changed, 78 insertions(+), 23 deletions(-) create mode 100755 hack/golangci-lint.sh diff --git a/.circleci/config.yml b/.circleci/config.yml index 94183be23..7714a88eb 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,11 +3,11 @@ # Check https://circleci.com/docs/2.0/language-go/ for more details version: 2 jobs: - markdownlint-misspell-shellcheck: + linters-check: docker: # this image is build from Dockerfile # https://github.com/pouchcontainer/pouchlinter/blob/master/Dockerfile - - image: pouchcontainer/pouchlinter:v0.2.2 + - image: pouchcontainer/pouchlinter:v0.2.3 working_directory: /go/src/github.com/dragonflyoss/Dragonfly steps: - checkout @@ -32,7 +32,6 @@ jobs: - run: name: use opensource tool client9/misspell to correct commonly misspelled English words command: | - go get -u github.com/client9/misspell/cmd/misspell find ./* -name "*" | xargs misspell -error - run: name: use ShellCheck (https://github.com/koalaman/shellcheck) to check the validateness of shell scripts in pouch repo @@ -42,16 +41,6 @@ jobs: name: validate go mod files command: | make check-go-mod - - gocode-check-via-golangci-lint-swagger: - docker: - - image: pouchcontainer/pouchlinter:v0.2.2 - environment: - GO111MODULE: "on" - GOPROXY: "https://goproxy.io" - working_directory: /go/src/github.com/dragonflyoss/Dragonfly - steps: - - checkout - run: name: validate swagger.yml command: | @@ -59,7 +48,7 @@ jobs: - run: name: use golangci-lint to check gocode of this project. command: | - golangci-lint run + make golangci-lint unit-test-golang: docker: @@ -99,7 +88,6 @@ workflows: version: 2 ci: jobs: - - markdownlint-misspell-shellcheck + - linters-check - unit-test-golang - - gocode-check-via-golangci-lint-swagger - api-integration-test diff --git a/.golangci.yml b/.golangci.yml index b02e47769..5a1302838 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,9 +5,6 @@ run: linters-settings: gocyclo: min-complexity: 20 - goconst: - min-len: 3 - min-occurrences: 5 linters: disable-all: true diff --git a/Makefile b/Makefile index 6cd7ef2a4..fc6062b3d 100644 --- a/Makefile +++ b/Makefile @@ -153,3 +153,7 @@ df.crt: df.key openssl req -new -key df.key -out df.csr openssl x509 -req -sha256 -days 365 -in df.csr -signkey df.key -out df.crt rm df.csr + +golangci-lint: + ./hack/golangci-lint.sh +.PHONY: golangci-lint diff --git a/dfget/core/downloader/p2p_downloader/p2p_downloader.go b/dfget/core/downloader/p2p_downloader/p2p_downloader.go index d69e82911..58ca18d38 100644 --- a/dfget/core/downloader/p2p_downloader/p2p_downloader.go +++ b/dfget/core/downloader/p2p_downloader/p2p_downloader.go @@ -31,7 +31,6 @@ import ( "github.com/dragonflyoss/Dragonfly/dfget/core/regist" "github.com/dragonflyoss/Dragonfly/dfget/types" "github.com/dragonflyoss/Dragonfly/pkg/constants" - "github.com/dragonflyoss/Dragonfly/pkg/errortypes" "github.com/dragonflyoss/Dragonfly/pkg/fileutils" "github.com/dragonflyoss/Dragonfly/pkg/httputils" "github.com/dragonflyoss/Dragonfly/pkg/printer" @@ -258,8 +257,8 @@ func (p2p *P2PDownloader) pullPieceTask(item *Piece) ( logrus.Errorf("pull piece task fail:%v and will migrate", res) var registerRes *regist.RegisterResult - var registerErr *errortypes.DfError - if registerRes, registerErr = p2p.Register.Register(p2p.cfg.RV.PeerPort); registerErr != nil { + registerRes, registerErr := p2p.Register.Register(p2p.cfg.RV.PeerPort) + if registerErr != nil { return nil, registerErr } p2p.pieceSizeHistory[1] = registerRes.PieceSize diff --git a/hack/build.sh b/hack/build.sh index f9d9b12f1..34f609e04 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -63,7 +63,7 @@ build-docker() { -e GOARCH="${GOARCH}" \ -e CGO_ENABLED=0 \ -e GO111MODULE=on \ - -e GOPROXY=https://goproxy.io \ + -e GOPROXY="${GOPROXY}" \ -w /go/src/${PKG} \ ${BUILD_IMAGE} \ go build -o "/go/bin/$1" -ldflags "${LDFLAGS}" ./cmd/"$2" diff --git a/hack/golangci-lint.sh b/hack/golangci-lint.sh new file mode 100755 index 000000000..f1d05f603 --- /dev/null +++ b/hack/golangci-lint.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +set -o nounset +set -o errexit +set -o pipefail + +# golangci-lint binary version v1.17.1 + +PKG=github.com/dragonflyoss/Dragonfly +LINT_IMAGE=pouchcontainer/pouchlinter:v0.2.3 +USE_DOCKER=${USE_DOCKER:-"0"} + +curDir=$(cd "$(dirname "$0")" && pwd) +cd "${curDir}" || return +LINT_SOURCE_HOME=$(cd ".." && pwd) + +. ./env.sh + +golangci-lint-docker() { + cd "${LINT_SOURCE_HOME}" || return + docker run \ + --rm \ + -ti \ + -v "$(pwd)"/.go/pkg:/go/pkg \ + -v "$(pwd)":/go/src/${PKG} \ + -e GO111MODULE=on \ + -e GOPROXY="${GOPROXY}" \ + -w /go/src/${PKG} \ + ${LINT_IMAGE} \ + golangci-lint run +} + +check-golangci-lint() { + has_installed="$(command -v golangci-lint || echo false)" + if [[ "${has_installed}" = "false" ]]; then + echo false + return + fi + echo true +} + +golangci-lint-local() { + has_installed="$(check-golangci-lint)" + if [[ "${has_installed}" = "true" ]]; then + echo "Detected that golangci-lint has already been installed. Start linting..." + cd "${LINT_SOURCE_HOME}" || return + golangci-lint run + return + else + echo "Detected that golangci-lint has not been installed. You have to install golangci-lint first." + return 1 + fi +} + +main() { + if [[ "1" == "${USE_DOCKER}" ]] + then + echo "Begin to check gocode with golangci-lint in docker." + golangci-lint-docker + else + echo "Begin to check gocode with golangci-lint locally" + golangci-lint-local + fi + echo "Check gocode with golangci-lint successfully " +} + +main "$@"