Skip to content

Commit

Permalink
Fix liting; use golangci lint (#508)
Browse files Browse the repository at this point in the history
* add base golangci-lint

* fix linting errors: unused var t, unexported-return

```
runner/util_test.go:237:34: unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
func TestGetStructureFieldTagMap(t *testing.T) {
```

```
main.go:54:1: exported function GetVersionInfo should have comment or be unexported
main.go:54:23: exported func GetVersionInfo returns unexported type main.versionInfo, which can be annoying to use
```
  • Loading branch information
lukmdo authored Dec 18, 2023
1 parent eb5b1b5 commit bd33bda
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 12 deletions.
16 changes: 16 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
run:
timeout: 2m

linters:
disable-all: true
enable:
- errcheck # Errcheck is a program for checking for unchecked errors in go programs.
- gci # Gci controls Go package import order and makes it always deterministic
- goimports # checks that goimports was run
- ineffassign # Detects when assignments to existing variables are not used
- misspell # spell checker
- revive # configurable linter for Go. Drop-in replacement of golint
- staticcheck # go vet on steroids
- stylecheck # static analysis, finds bugs and performance issues, offers simplifications, and enforces style rules
- unconvert # Remove unnecessary type conversions
- unused # Checks Go code for unused constants, variables, functions and types
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ LDFLAGS += -X "main.airVersion=$(AIRVER)"
LDFLAGS += -X "main.goVersion=$(shell go version | sed -r 's/go version go(.*)\ .*/\1/')"

GO := GO111MODULE=on CGO_ENABLED=0 go
GOLANGCI_LINT_VERSION = 1.55.2

.PHONY: init
init:
go install golang.org/x/lint/golint@latest
init: install-golangci-lint
go install golang.org/x/tools/cmd/goimports@latest
@echo "Install pre-commit hook"
@ln -s $(shell pwd)/hooks/pre-commit $(shell pwd)/.git/hooks/pre-commit || true
@chmod +x ./hack/check.sh

.PHONY: install-golangci-lint
install-golangci-lint:
ifeq (, $(shell which golangci-lintx))
@$(shell curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION))
endif

.PHONY: setup
setup: init
git init
Expand Down
17 changes: 8 additions & 9 deletions hack/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ exit_code=0
check_scope=$1
if [[ "${check_scope}" = "all" ]]; then
echo "all"
files=($(git ls-files | grep "\.go" | grep -v -e "^third_party" -e "^vendor"))
files=($(git ls-files | grep "\.go$" | grep -v -e "^third_party" -e "^vendor"))
else
files=($(git diff --cached --name-only --diff-filter ACM | grep "\.go" | grep -v -e "^third_party" -e "^vendor"))
files=($(git diff --cached --name-only --diff-filter ACM | grep "\.go$" | grep -v -e "^third_party" -e "^vendor"))
fi

echo -e "${green}1. Formatting code style"
Expand All @@ -19,13 +19,12 @@ if [[ "${#files[@]}" -ne 0 ]]; then
fi

echo -e "${green}2. Linting"
for file in "${files[@]}"; do
out=$(golint ${file})
if [[ -n "${out}" ]]; then
echo "${red}${out}"
exit_code=1
fi
done
out=$(golangci-lint run)
if [[ -n "${out}" ]]; then
echo "${red}${out}"
exit_code=1
fi


if [[ ${exit_code} -ne 0 ]]; then
echo "${red}Please fix the errors above :)"
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type versionInfo struct {
goVersion string
}

func GetVersionInfo() versionInfo {
func GetVersionInfo() versionInfo { //revive:disable:unexported-return
if len(airVersion) != 0 && len(goVersion) != 0 {
return versionInfo{
airVersion: airVersion,
Expand Down
1 change: 1 addition & 0 deletions runner/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func Test_killCmd_SendInterrupt_false(t *testing.T) {
func TestGetStructureFieldTagMap(t *testing.T) {
c := Config{}
tagMap := flatConfig(c)
assert.NotEmpty(t, tagMap)
for _, i2 := range tagMap {
fmt.Printf("%v\n", i2.fieldPath)
}
Expand Down

0 comments on commit bd33bda

Please sign in to comment.