golangci-lint
是一個 Go linters 聚合器,而 linter
是使用工具來對程式碼提供一些檢查,保證提交程式碼的品質。
需要手動執行,在之前使用的過程中,由於專案是多人活動,總是會忘記執行golangci-lint
進行程式碼檢查,當前我自己也是。所以我們希望採用一種隱式的方式來自動執行。那麼經過多番思考,採用 git 的 hooks 可以來自動執行一些腳本。
這段期間還有一些其他的嘗試,不過這篇文章主要說在git中的使用ci-lint,有興趣的可以移步到為什麼最終採用git 本地hooks 的方式執行golangci-lint?
前提條件:
請設定 goland 的預設終端為 bash
,不然後面執行腳本的時候,可能會不支援。
由於現在採用git 作為我們的版本管理系統(VCS),而git
在執行一些操作之前,允許執行腳本,這可以讓我們來執行一些操作前的程式碼檢查。
專案程式碼目錄:
├── .githooks │ ├── applypatch-msg.sample │ ├── commit-msg.sample │ ├── fsmonitor-watchman.sample │ ├── post-update.sample │ ├── pre-applypatch.sample │ ├── pre-commit │ ├── pre-merge-commit.sample │ ├── pre-push │ ├── pre-rebase.sample │ ├── pre-receive.sample │ ├── prepare-commit-msg.sample │ ├── push-to-checkout.sample │ └── update.sample ├── .golangci.yml ├── go.mod ├── golangci-lint.sh └── init.sh
可以透過專案結構看到,需要在專案根目錄增加一個.githooks
資料夾,
然後增加.golangci.yml
golangci-lint 使用的配置文件,
增加一個手動執行goalngci-lint
的執行腳本golangci-lint.sh
,
最後就是專案應用程式git hooks
的腳本init.sh
,用來初始化這個專案的腳本。
說了這麼多,還不知道這個到底是乾啥的,先來看一下效果圖
在commit
的時候會幫助我們進行文件的fmt
:
#在push
的時候會檢查整個專案是否有問題的地方:
如果项目存在可能的问题,那么是不会让你 push 的。通过这种方式来保证服务器上的代码都是符合规则的。
首次通过执行 init.sh
脚本进行项目初始化设置。
这会检查你的环境,如果一些工具不存在,它会自动下载。并会修改默认 git
钩子指向当前项目的 .githooks
文件夹。
好了,就这样,就是这么简单。
这都是小问题,复制内容过去吧。
但是在复制这些之前,你一定已经是在一个git 管理的根目录下。
那么下面就开始你的复制吧。
.githooks/pre-commit:
#!/bin/sh # # An example hook script to verify what is about to be committed. # Called by "git commit" with no arguments. The hook should # exit with non-zero status after issuing an appropriate message if # it wants to stop the commit. # # To enable this hook, rename this file to "pre-commit". # 获取所有变化的go文件 STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep .go$) if [ "$STAGED_GO_FILES" = "" ]; then exit 0 fi echo "check gofmt ..." CHECK_FMT=$(gofmt -s -w -l $STAGED_GO_FILES) if [ -n "${CHECK_FMT##* }" ]; then echo echo -e "these files will be\033[32m gofmt\033[0m formatted:" for file in ${CHECK_FMT[*]}; do echo -e "\033[36m\t$file\033[0m" done git add ${CHECK_FMT//\/n/ } echo fi echo "check goimports ..." CHECK_GOPLS=$(goimports -l -w $STAGED_GO_FILES) if [ -n "${CHECK_GOPLS##* }" ]; then echo echo -e "these files will be\033[32m goimports\033[0m formatted:" for file in ${CHECK_GOPLS[*]}; do echo -e "\033[36m\t$file\033[0m" done git add ${CHECK_GOPLS//\/n/ } echo fi printf "\033[32m COMMIT SUCCEEDED \033[0m\n" echo exit 0
.githooks/pre-push:
#!/bin/sh # An example hook script to verify what is about to be pushed. Called by "git # push" after it has checked the remote status, but before anything has been # pushed. If this script exits with a non-zero status nothing will be pushed. # # This hook is called with the following parameters: # # $1 -- Name of the remote to which the push is being done # $2 -- URL to which the push is being done # # If pushing without using a named remote those arguments will be equal. # # Information about the commits which are being pushed is supplied as lines to # the standard input in the form: # # <local ref> <local oid> <remote ref> <remote oid> # # This sample shows how to prevent push of commits where the log message starts # with "WIP" (work in progress). #remote="$1" #url="$2" printf "\033[32m 推送前需要检查当前项目可以 go build 通过 \033[0m\n" echo "run golangci-lint..." echo # 运行 golangci-lint 检查工具 golangci-lint run ./... if [ $? -ne 0 ]; then printf "\033[31m PUSH FAILED \033[0m\n" exit 1 fi printf "\033[32m PUSH SUCCEEDED \033[0m\n" echo exit 0
golangci-lint.sh
#!/bin/sh if ! command -v golangci-lint &>/dev/null; then echo "golangci-lint not installed or available in the PATH" >&2 echo "install golangci-lint ..." >&2 go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1 fi #goland 可直接定位文件 golangci-lint run ./... |sed 's/\\/\//g'
init.sh
#!/bin/sh # 检查 go 是否安装 checkGoEnv() { # go是否安装 if ! command -v go &>/dev/null; then echo "go not installed or available in the PATH" >&2 echo "please check https://golang.google.cn" >&2 exit 1 fi # go proxy 是否设置 if [ -z $GOPROXY ]; then echo "go proxy not set in the PATH" >&2 echo "please set GOPROXY, https://goproxy.cn,direct || https://goproxy.io,direct" >&2 exit 1 fi echo "go env installed ..." } # 检查 go 相关工具包是否安装 checkGoLintEnv() { if ! command -v goimports &>/dev/null; then echo "goimports not installed or available in the PATH" >&2 echo "install goimports ..." >&2 go install golang.org/x/tools/cmd/goimports@latest checkGoLintEnv return fi echo "goimports installed ..." } # 检查 golangci-lint 是否安装 checkCiLintEnv() { if ! command -v golangci-lint &>/dev/null; then echo "golangci-lint not installed or available in the PATH" >&2 echo "install golangci-lint ..." >&2 go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1 checkCiLintEnv fi echo "golangci-lint installed ..." } # 初始化钩子配置 initHooks() { # 如果当前目录不存在 .githooks 目录,说明位置不对 if [ ! -d ".githooks" ]; then echo "exec incorrect position" exit 1 fi # 检查是否已经设置了 exist=$(git config core.hooksPath) if [ -z $exist ]; then # 设置 hooks 默认位置 git config core.hooksPath .githooks echo "init git hooks ..." >&2 fi } main() { checkGoEnv checkGoLintEnv checkCiLintEnv initHooks } main
.golangci.yml
run: timeout: 2m tests: false linters: disable-all: true enable: - typecheck - staticcheck - govet - gocritic linters-settings: govet: check-shadowing: true disable-all: true enable: - asmdecl - assign - atomic - atomicalign - bools - buildtag - cgocall - composites - copylocks - httpresponse - loopclosure - lostcancel - nilfunc - nilness - printf - shadow - shift - stdmethods - structtag - tests - unmarshal - unreachable - unsafeptr - unusedresult staticcheck: go: "1.17" checks: [ "all", "-SA3*", "-SA6000", "-SA6001", "-SA6003", "-ST*", "ST1006", "ST1008", "ST1016", "-QF1" ] gocritic: enabled-tags: - diagnostic - experimental - opinionated - style enabled-checks: - sliceClear disabled-tags: - performance disabled-checks: - assignOp - badLock - badRegexp - codegenComment - commentFormatting - commentedOutCode - docStub - dupArg - dupBranchBody - dupCase - dupImport - exitAfterDefer - externalErrorReassign - flagDeref - hexLiteral - ifElseChain - importShadow - initClause - mapKey - nestingReduce - newDeref - redundantSprint - regexpMust - regexpPattern - regexpSimplify - ruleguard - sloppyLen - sloppyTypeAssert - sortSlice - sprintfQuotedString - sqlQuery - stringConcatSimplify - syncMapLoadAndDelete - tooManyResultsChecker - typeDefFirst - typeUnparen - underef - unlabelStmt - unlambda - unnecessaryBlock - unnecessaryDefer - yodaStyleExpr - whyNoLint - paramTypeCombine - emptyStringTest
好了,现在你应该是这样的结构了吧
项目代码目录:
├── .githooks │ ├── pre-commit │ └── pre-push ├── .golangci.yml ├── golangci-lint.sh └── init.sh
如果不是,请返回到上面在看一下步骤。
这个时候可以执行 init.sh
脚本来初始化了。
最後可以在 https://github.com/ywanbing/golangci
倉庫中取得程式碼。
以上是golangci-lint應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!