From 54adb1b181b5d7260a7cb56db15198e1b5dad723 Mon Sep 17 00:00:00 2001 From: xiadanni Date: Fri, 19 Nov 2021 10:13:58 +0800 Subject: [PATCH] rubik: support static check using "make check" to do static check Signed-off-by: xiadanni --- .golangci.yml | 119 +++++++++++++++++++++++++++++++++++++++++++ Makefile | 10 ++++ hack/static_check.sh | 55 ++++++++++++++++++++ 3 files changed, 184 insertions(+) create mode 100644 .golangci.yml create mode 100755 hack/static_check.sh diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..9cd7c0b --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,119 @@ +run: + deadline: 10m + issues-exit-code: 1 + tests: false + + skip-dirs: + - vendor + + skip-files: + - ".*\\.pb\\.go$" + - ".*\\.dbx\\.go$" + + +linters: + enable: + - gomnd + - govet + - golint + - staticcheck + - errcheck + - ineffassign + - varcheck + - structcheck + - deadcode + - bodyclose + - nakedret + - gofmt + - misspell + - unconvert + - scopelint + - gocritic + - dogsled + - gosec + - goimports + - gocyclo + - godox + - misspell + - goconst + disable: + - lll + - wsl + - depguard + - stylecheck + - dupl + - interfacer + - gosimple + - unused + - rowserrcheck + - goprintffuncname + fast: false + +output: + format: colored-line-number + print-issued-lines: true + print-linter-name: true + uniq-by-line: true + +linters-settings: + dogsled: + errcheck: + check-type-assertions: false + check-blank: false + ignore: fmt:.*,io/ioutil:^Read.* + govet: + check-shadowing: true + use-installed-packages: false + gomnd: + settings: + mnd: + checks: argument, case, condition, return + gocritic: + disabled-checks: + - ifElseChain + goimports: + local-prefixes: isula.org/rubik + golint: + min-confidence: 0.8 + gofmt: + simplify: true + gocyclo: + min-complexity: 20 + dupl: + threshold: 150 + goconst: + min-len: 3 + min-occurrences: 3 + misspell: + locale: US + lll: + line-length: 140 + tab-width: 1 + unparam: + algo: cha + check-exported: false + nakedret: + max-func-lines: 30 + prealloc: + simple: true + range-loops: true + for-loops: false + +issues: + exclude-rules: + path: _test\.go + linters: + - gocyclo + - scopelint + - errcheck + - dupl + - gosec + - structcheck + - staticcheck + - gomnd + - ineffassign + max-issues-per-linter: 0 + max-same-issues: 0 + new: true + new-from-rev: "HEAD~" + exclude-use-default: false diff --git a/Makefile b/Makefile index d7d8158..8b984b3 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,16 @@ release: rm -rf $(TMP_DIR) && mkdir -p $(ORG_PATH) $(TMP_DIR) $(GO_BUILD) -o rubik $(LD_FLAGS) rubik.go 2>/dev/null +check: + @echo "Static check start for last commit" + @./hack/static_check.sh last + @echo "Static check last commit finished" + +checkall: + @echo "Static check start for whole project" + @./hack/static_check.sh all + @echo "Static check project finished" + tests: test-unit test-integration test-unit: diff --git a/hack/static_check.sh b/hack/static_check.sh new file mode 100755 index 0000000..0d15c2d --- /dev/null +++ b/hack/static_check.sh @@ -0,0 +1,55 @@ +#!/bin/bash + +# Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved. +# rubik licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Create: 2021-04-15 +# Description: shell script for static checking + +workspace=$(cd "$(dirname "$0")" && cd .. && pwd) +config_file=".golangci.yml" +check_type=$1 +export GO111MODULE=off + +# check binary file golangci-lint and it's config exist +function pre() { + # check golangci-lint exist + lint=$(command -v golangci-lint) > /dev/null 2>&1 + if [ -z "${lint}" ]; then + echo "Could not find binary golangci-lint" + exit 1 + fi + + # check config exist + config_path=${workspace}/${config_file} + if [[ ! -f ${config_path} ]]; then + echo "Could not find config file for golangci-lint" + exit 1 + fi +} + +# last: only do static check for the very latest commit +# all : do static check for the whole project +function run() { + case ${check_type} in + last) + # ${lint} run --modules-download-mode vendor + ${lint} run + ;; + all) + ${lint} run --new=false --new-from-rev=false + ;; + *) + return + ;; + esac +} + +pre +run -- Gitee