From 2fc328a6d2a7ddfd516f687f21eda06ded29da27 Mon Sep 17 00:00:00 2001 From: Marina Biryukova Date: Fri, 25 Aug 2023 17:16:24 +0300 Subject: [PATCH] [#195] Add log constants linter Signed-off-by: Marina Biryukova --- .forgejo/workflows/tests.yml | 13 ++++++++++--- .golangci.yml | 11 +++++++++++ .pre-commit-config.yaml | 29 ++++++++++++++++++----------- Makefile | 23 +++++++++++++++++++++-- cmd/s3-gw/app_settings.go | 2 +- internal/logs/logs.go | 1 + 6 files changed, 62 insertions(+), 17 deletions(-) diff --git a/.forgejo/workflows/tests.yml b/.forgejo/workflows/tests.yml index 4ca09ea..b9d4ac6 100644 --- a/.forgejo/workflows/tests.yml +++ b/.forgejo/workflows/tests.yml @@ -7,10 +7,17 @@ jobs: steps: - uses: actions/checkout@v3 - - name: golangci-lint - uses: https://github.com/golangci/golangci-lint-action@v2 + - name: Set up Go + uses: actions/setup-go@v3 with: - version: latest + go-version: '1.20' + cache: true + + - name: Install linters + run: make lint-install + + - name: Run linters + run: make lint tests: name: Tests diff --git a/.golangci.yml b/.golangci.yml index a271450..6174a52 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -24,6 +24,16 @@ linters-settings: govet: # report about shadowed variables check-shadowing: false + custom: + truecloudlab-linters: + path: bin/external_linters.so + original-url: git.frostfs.info/TrueCloudLab/linters.git + settings: + noliteral: + enable: true + target-methods: ["Fatal"] + disable-packages: ["codes", "tc"] + constants-package: "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs" linters: enable: @@ -45,6 +55,7 @@ linters: - gofmt - whitespace - goimports + - truecloudlab-linters disable-all: true fast: false diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4fde2a0..3c963be 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -30,16 +30,23 @@ repos: hooks: - id: shellcheck - - repo: https://github.com/golangci/golangci-lint - rev: v1.51.2 - hooks: - - id: golangci-lint - - repo: local hooks: - - id: go-unit-tests - name: go unit tests - entry: make test - pass_filenames: false - types: [go] - language: system + - id: make-lint-install + name: install linters + entry: make lint-install + language: system + pass_filenames: false + + - id: make-lint + name: run linters + entry: make lint + language: system + pass_filenames: false + + - id: go-unit-tests + name: go unit tests + entry: make test + pass_filenames: false + types: [go] + language: system diff --git a/Makefile b/Makefile index b9bdf04..954ffb6 100755 --- a/Makefile +++ b/Makefile @@ -4,7 +4,8 @@ REPO ?= $(shell go list -m) VERSION ?= $(shell git describe --tags --dirty --match "v*" --always --abbrev=8 2>/dev/null || cat VERSION 2>/dev/null || echo "develop") GO_VERSION ?= 1.19 -LINT_VERSION ?= 1.49.0 +LINT_VERSION ?= 1.54.0 +TRUECLOUDLAB_LINT_VERSION ?= 0.0.2 BINDIR = bin METRICS_DUMP_OUT ?= ./metrics-dump.json @@ -18,6 +19,10 @@ REPO_BASENAME = $(shell basename `go list -m`) HUB_IMAGE ?= "truecloudlab/$(REPO_BASENAME)" HUB_TAG ?= "$(shell echo ${VERSION} | sed 's/^v//')" +OUTPUT_LINT_DIR ?= $(shell pwd)/bin +LINT_DIR = $(OUTPUT_LINT_DIR)/golangci-lint-$(LINT_VERSION)-v$(TRUECLOUDLAB_LINT_VERSION) +TMP_DIR := .cache + .PHONY: all $(BINS) $(BINDIR) dep docker/ test cover format image image-push dirty-image lint docker/lint pre-commit unpre-commit version clean protoc # .deb package versioning @@ -101,9 +106,23 @@ dirty-image: -f .docker/Dockerfile.dirty \ -t $(HUB_IMAGE)-dirty:$(HUB_TAG) . +# Install linters +lint-install: + @mkdir -p $(TMP_DIR) + @rm -rf $(TMP_DIR)/linters + @git -c advice.detachedHead=false clone --branch v$(TRUECLOUDLAB_LINT_VERSION) https://git.frostfs.info/TrueCloudLab/linters.git $(TMP_DIR)/linters + @@make -C $(TMP_DIR)/linters lib CGO_ENABLED=1 OUT_DIR=$(OUTPUT_LINT_DIR) + @rm -rf $(TMP_DIR)/linters + @rmdir $(TMP_DIR) 2>/dev/null || true + @CGO_ENABLED=1 GOBIN=$(LINT_DIR) go install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(LINT_VERSION) + # Run linters lint: - @golangci-lint --timeout=5m run + @if [ ! -d "$(LINT_DIR)" ]; then \ + echo "Run make lint-install"; \ + exit 1; \ + fi + $(LINT_DIR)/golangci-lint --timeout=5m run # Run linters in Docker docker/lint: diff --git a/cmd/s3-gw/app_settings.go b/cmd/s3-gw/app_settings.go index b6c3a2c..3b89d9b 100644 --- a/cmd/s3-gw/app_settings.go +++ b/cmd/s3-gw/app_settings.go @@ -244,7 +244,7 @@ func fetchDefaultPolicy(l *zap.Logger, cfg *viper.Viper) netmap.PlacementPolicy } if err := policy.DecodeString(defaultPlacementPolicy); err != nil { - l.Fatal("failed to parse default 'default' location constraint", zap.String("policy", defaultPlacementPolicy)) + l.Fatal(logs.FailedToParseDefaultDefaultLocationConstraint, zap.String("policy", defaultPlacementPolicy)) } return policy diff --git a/internal/logs/logs.go b/internal/logs/logs.go index 46081aa..e9cce3c 100644 --- a/internal/logs/logs.go +++ b/internal/logs/logs.go @@ -40,6 +40,7 @@ const ( FailedToParseLocationConstraint = "failed to parse location constraint, it cannot be used" // Warn in cmd/s3-gw/app_settings.go FailedToParseDefaultCopiesNumbers = "failed to parse 'default' copies numbers, default one will be used" // Warn in cmd/s3-gw/app_settings.go FailedToParseCopiesNumbers = "failed to parse copies numbers, skip" // Warn in cmd/s3-gw/app_settings.go + FailedToParseDefaultDefaultLocationConstraint = "failed to parse default 'default' location constraint" // Fatal in cmd/s3-gw/app_settings.go ConstraintAdded = "constraint added" // Info in ../../cmd/s3-gw/app_settings.go SkipEmptyAddress = "skip, empty address" // Warn in ../../cmd/s3-gw/app_settings.go AddedStoragePeer = "added storage peer" // Info in ../../cmd/s3-gw/app_settings.go