From d14da90a31d495cf9256f78a2dc96d617bf2e311 Mon Sep 17 00:00:00 2001 From: moocss Date: Thu, 16 Nov 2023 10:55:14 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E5=A4=87=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 23 ++++++++++++++++++----- scripts/fmt.sh | 3 ++- scripts/setup.sh | 30 ++++++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index b2d7bd6..765bebc 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,22 @@ +APP_PATH:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) +SCRIPTS_PATH:=$(APP_PATH)/scripts + .PHONY: setup fmt lint test mocks test_coverage tidy check docker_up docker_down GO_PKGS := $(shell go list -f {{.Dir}} ./...) # 初始化环境 setup: - @sh ./scripts/setup.sh + @echo "初始化开发环境......" + @find "$(SCRIPTS_PATH)" -type f -name '*.sh' -exec chmod +x {} \; + @sh ${SCRIPTS_PATH}/setup.sh + @make tidy +# 代码风格 fmt: - @sh ./scripts/fmt.sh + @sh ${SCRIPTS_PATH}/fmt.sh +# 静态扫描 lint: @golangci-lint run -c .golangci.yml @@ -22,16 +30,21 @@ test_coverage: mocks: @go generate ./... +# 依赖清理 tidy: @go mod tidy -v check: + @echo "整理项目依赖中......" + @$(MAKE) --no-print-directory tidy + @echo "代码风格检查中......" @$(MAKE) --no-print-directory fmt - @$(MAKE) --no-print-directory tidy + @echo "代码静态扫描中......" + @$(MAKE) lint -# docker 安装并运行服务 +# 启动本地研发 docker 依赖 docker_up: docker-compose -f scripts/docker/docker-compose.yml up -d -# docker 停止服务 +# 停止本地研发 docker docker_down: docker-compose -f scripts/docker/docker-compose.yml down -v \ No newline at end of file diff --git a/scripts/fmt.sh b/scripts/fmt.sh index ee98115..f9cd096 100644 --- a/scripts/fmt.sh +++ b/scripts/fmt.sh @@ -1,3 +1,4 @@ #!/bin/sh -goimports -l -w $(find . -type f -name '*.go' -not -path "./.idea/*") \ No newline at end of file +goimports -l -w $(find . -type f -name '*.go' -not -path "./.idea/*") +gofumpt -l -w $(find . -type f -name '*.go' -not -path "./.idea/*") \ No newline at end of file diff --git a/scripts/setup.sh b/scripts/setup.sh index 6bfe852..f0d3df6 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,8 +1,27 @@ #!/bin/sh -SOURCE_COMMIT=.github/pre-commit +# Check Go installation +echo "检查 Go版本、Docker、Docker Compose V2......" +GO_VERSION="1.21" # Specify the required Go version +if ! command -v go >/dev/null || [[ ! "$(go version | awk '{print $3}')" == *"$GO_VERSION"* ]]; then + echo "Go $GO_VERSION 未安装或者版本不正确" + exit 1 # 退出并返回错误代码 +fi + +if ! command -v docker >/dev/null; then + echo "Docker 未安装" + exit 1 # Exit with an error code +fi + +if ! command -v docker compose >/dev/null; then + echo "Docker Compose V2未安装" + exit 1 # Exit with an error code +fi + +DIR="$(cd "$(dirname "$0")" && pwd)" +SOURCE_COMMIT=$DIR/git/pre-commit TARGET_COMMIT=.git/hooks/pre-commit -SOURCE_PUSH=.github/pre-push +SOURCE_PUSH=$DIR/git/pre-push TARGET_PUSH=.git/hooks/pre-push # copy pre-commit file if not exist. @@ -22,7 +41,10 @@ test -x $TARGET_PUSH || chmod +x $TARGET_PUSH test -x $TARGET_COMMIT || chmod +x $TARGET_COMMIT echo "安装 golangci-lint..." -go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.52.2 +go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.54.2 echo "安装 goimports..." -go install golang.org/x/tools/cmd/goimports@latest \ No newline at end of file +go install golang.org/x/tools/cmd/goimports@latest + +echo "安装 gofumpt......" +go install mvdan.cc/gofumpt@latest \ No newline at end of file