From 9f05f45d59070ac4e47af95e03c1007869a88adb Mon Sep 17 00:00:00 2001 From: sincos Date: Sun, 2 Jun 2024 10:22:54 +0800 Subject: [PATCH] ci: add Makefile and github ci flow --- .github/workflows/ci.yml | 29 +++++++++++++++++++++++++ Makefile | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 Makefile diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..d3fb512 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,29 @@ +name: ci + +on: + push: + branches: [ "main" ] + tags: [ "v*.*.*" ] + pull_request: + branches: [ "main" ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.22' + + - name: Test + run: make cover + + - name: Coverage report + uses: codecov/codecov-action@v4 + with: + token: ${{secrets.CODECOV_TOKEN}} + file: ./cover.out diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7554e85 --- /dev/null +++ b/Makefile @@ -0,0 +1,47 @@ +PACKAGES=`go list ./... | grep -E -v 'example|proto'` +FORMAT_FILES=`find . -type f -name '*.go' | grep -E -v '_generated.go|.pb.go'` +XGO_OK=$(shell type xgo > /dev/null 2>&1 && echo $$?) + +xgo: + @if [ "${XGO_OK}" != "0" ]; then \ + echo "installing xgo for unit test"; \ + go install github.com/xhd2015/xgo/cmd/xgo@latest; \ + fi + +tidy: + go mod tidy + +cover: xgo tidy + xgo test -failfast ${PACKAGES} -coverprofile=cover.out -covermode=count + + +test: tidy + xgo test -race -failfast ${PACKAGES} + +report: + @echo ">>>static checking" + @go vet ./... + @echo "done\n" + @echo ">>>detecting ineffectual assignments" + @ineffassign ./... + @echo "done\n" + @echo ">>>detecting icyclomatic complexities over 10 and average" + @gocyclo -over 10 -avg -ignore '_test|vendor' . || true + @echo "done\n" + +MOD=$(shell cat go.mod | grep ^module -m 1 | awk '{ print $$2; }' || '') +FILE_LIST=$(shell find . -type f -name '*.go' | grep -E -v '_generated.go|.pb.go') + +.PHONY: fmt +fmt: + @echo ${MOD} + @for item in ${FILE_LIST} ; \ + do \ + if [ -z ${MOD} ]; then \ + goimports -d -w $$item ; \ + else \ + goimports -d -w -local "${MOD}" $$item ; \ + fi \ + done + +