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

Commit

Permalink
support run golangci-lint locally and in docker
Browse files Browse the repository at this point in the history
Signed-off-by: yeya24 <[email protected]>
  • Loading branch information
yeya24 committed Aug 16, 2019
1 parent fb1dbae commit 3149d9d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 23 deletions.
20 changes: 4 additions & 16 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -42,24 +41,14 @@ 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: |
swagger validate "/go/src/github.com/dragonflyoss/Dragonfly/apis/swagger.yml"
- run:
name: use golangci-lint to check gocode of this project.
command: |
golangci-lint run
make golangci-lint
unit-test-golang:
docker:
Expand Down Expand Up @@ -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
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ run:
linters-settings:
gocyclo:
min-complexity: 20
goconst:
min-len: 3
min-occurrences: 5

linters:
disable-all: true
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions dfget/core/downloader/p2p_downloader/p2p_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
67 changes: 67 additions & 0 deletions hack/golangci-lint.sh
Original file line number Diff line number Diff line change
@@ -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 "$@"

0 comments on commit 3149d9d

Please sign in to comment.