Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Use golangci-lint for linting #121

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ updates:
schedule:
interval: "daily"

- package-ecosystem: "gomod"
directory: "/tools"
schedule:
interval: "daily"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
Expand Down
26 changes: 24 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,37 @@ jobs:
go-version: 1.21.x
cache: true

- name: Lint
run: make lint
- name: Build
run: make build

- name: Test
run: make cover

- name: Upload coverage to codecov.io
uses: codecov/codecov-action@v3

lint:
name: Lint
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
name: Check out repository
- uses: actions/setup-go@v4
name: Set up Go
with:
go-version: 1.21.x
cache: false # managed by golangci-lint

- uses: golangci/golangci-lint-action@v3
name: Install golangci-lint
with:
version: latest
args: --version # make lint will run the linter

- run: make lint
name: Lint

docker:
runs-on: ubuntu-latest
steps:
Expand Down
28 changes: 28 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
output:
# Make output more digestible with quickfix in vim/emacs/etc.
sort-results: true
print-issued-lines: false

linters:
enable:
- gofumpt
- nolintlint
- revive

linters-settings:
govet:
# These govet checks are disabled by default, but they're useful.
enable:
- niliness
- reflectvaluecompare
- sortslice
- unusedwrite

issues:
# Print all issues reported by all linters.
max-issues-per-linter: 0
max-same-issues: 0

# Don't ignore some of the issues that golangci-lint considers okay.
# This includes documenting all exported entities.
exclude-use-default: false
60 changes: 23 additions & 37 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,61 +1,47 @@
export GOBIN = $(shell pwd)/bin
# Directory containing the Makefile.
PROJECT_ROOT = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))

export GOBIN = $(PROJECT_ROOT)/bin
export PATH := $(GOBIN):$(PATH)

GO_FILES = $(shell find . \
-path '*/.*' -prune -o \
'(' -type f -a -name '*.go' ')' -print)

REVIVE = bin/revive
STATICCHECK = bin/staticcheck
TOOLS = $(REVIVE) $(STATICCHECK)

TEST_FLAGS ?= -race

.PHONY: all
all: lint install test
all: lint build test

.PHONY: lint
lint: gofmt revive staticcheck

.PHONY: gofmt
gofmt:
$(eval FMT_LOG := $(shell mktemp -t gofmt.XXXXX))
@gofmt -e -s -l $(GO_FILES) > $(FMT_LOG) || true
@[ ! -s "$(FMT_LOG)" ] || \
(echo "gofmt failed. Please reformat the following files:" | \
cat - $(FMT_LOG) && false)

.PHONY: staticcheck
staticcheck: $(STATICCHECK)
$(STATICCHECK) ./...

$(STATICCHECK): tools/go.mod
cd tools && go install honnef.co/go/tools/cmd/staticcheck

.PHONY: revive
revive: $(REVIVE)
$(REVIVE) -set_exit_status ./...
lint: golangci-lint tidy-lint

$(REVIVE): tools/go.mod
cd tools && go install github.com/mgechev/revive

.PHONY: install
install:
.PHONY: build
build:
go install .

.PHONY: test
test:
go test $(TEST_FLAGS) ./...

.PHONY: build
run: build
sally

.PHONY: cover
cover:
go test $(TEST_FLAGS) -coverprofile=cover.out -covermode=atomic -coverpkg=./... ./...
go tool cover -html=cover.out -o cover.html

.PHONY: clean
clean:
rm -rf _tmp
.PHONY: golangci-lint
golangci-lint:
golangci-lint run

.PHONY: install
run: install
sally
.PHONY: tidy
tidy:
go mod tidy

.PHONY: tidy-lint
tidy-lint:
go mod tidy
git diff --exit-code -- go.mod go.sum
4 changes: 3 additions & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ func TestPostRejected(t *testing.T) {

res, err := http.Post(srv.URL+tt.path, "text/plain", strings.NewReader("foo"))
require.NoError(t, err)
defer res.Body.Close()
defer func() {
assert.NoError(t, res.Body.Close())
}()

body, err := io.ReadAll(res.Body)
require.NoError(t, err)
Expand Down
3 changes: 0 additions & 3 deletions tools/doc.go

This file was deleted.

27 changes: 0 additions & 27 deletions tools/go.mod

This file was deleted.

58 changes: 0 additions & 58 deletions tools/go.sum

This file was deleted.

9 changes: 0 additions & 9 deletions tools/tools.go

This file was deleted.