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

chore: add more linters, address findings #670

Merged
merged 10 commits into from
Mar 13, 2024
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54.1
version: v1.56.2
20 changes: 19 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
linters-settings:
gocritic:
disabled-checks:
- hugeParam
enabled-tags:
- performance
govet:
check-shadowing: true
misspell:
Expand All @@ -10,6 +15,12 @@ linters-settings:
- name: unused-parameter
disabled: true

issues:
exclude:
# https://github.com/catenacyber/perfsprint/issues/21
# https://github.com/catenacyber/perfsprint/issues/23
- "^fmt\\.Sprintf can be replaced with string (addi|concatena)tion$"

linters:
disable-all: true
enable:
Expand Down Expand Up @@ -38,7 +49,6 @@ linters:
- gocyclo
- godot
- godox
- gofmt
- gofumpt
- goheader
- goimports
Expand All @@ -47,14 +57,22 @@ linters:
- gosimple
- govet
- ineffassign
- makezero
- mirror
- misspell
- nestif
- noctx
- nolintlint
- perfsprint
- predeclared
- reassign
- revive
- staticcheck
- tagalign
- tenv
- typecheck
- unconvert
- unparam
- unused
- usestdlibvars
- whitespace
3 changes: 3 additions & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[default.extend-words]
# .golangci.yml
decorder = "decorder"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bench:

bin/golangci-lint:
@test -x $$(go env GOPATH)/bin/golangci-lint || \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.54.1
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.56.2

lint: bin/golangci-lint
$$(go env GOPATH)/bin/golangci-lint run --fix
Expand Down
34 changes: 17 additions & 17 deletions internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ import (
var errFilesIncompatible = errors.New("One of your runners contains incompatible file types")

type Command struct {
Run string `mapstructure:"run" yaml:"run" json:"run" toml:"run"`

Skip interface{} `mapstructure:"skip" yaml:",omitempty" json:"skip,omitempty" toml:"skip,omitempty,inline"`
Only interface{} `mapstructure:"only" yaml:",omitempty" json:"only,omitempty" toml:"only,omitempty,inline"`
Tags []string `mapstructure:"tags" yaml:",omitempty" json:"tags,omitempty" toml:"tags,omitempty"`
Glob string `mapstructure:"glob" yaml:",omitempty" json:"glob,omitempty" toml:"glob,omitempty"`
Files string `mapstructure:"files" yaml:",omitempty" json:"files,omitempty" toml:"files,omitempty"`
Env map[string]string `mapstructure:"env" yaml:",omitempty" json:"env,omitempty" toml:"env,omitempty"`

Root string `mapstructure:"root" yaml:",omitempty" json:"root,omitempty" toml:"root,omitempty"`
Exclude string `mapstructure:"exclude" yaml:",omitempty" json:"exclude,omitempty" toml:"exclude,omitempty"`
Priority int `mapstructure:"priority" yaml:",omitempty" json:"priority,omitempty" toml:"priority,omitempty"`

FailText string `mapstructure:"fail_text" yaml:"fail_text,omitempty" json:"fail_text,omitempty" toml:"fail_text,omitempty"`
Interactive bool `mapstructure:"interactive" yaml:",omitempty" json:"interactive,omitempty" toml:"interactive,omitempty"`
UseStdin bool `mapstructure:"use_stdin" yaml:",omitempty" json:"use_stdin,omitempty" toml:"use_stdin,omitempty"`
StageFixed bool `mapstructure:"stage_fixed" yaml:"stage_fixed,omitempty" json:"stage_fixed,omitempty" toml:"stage_fixed,omitempty"`
Run string `json:"run" mapstructure:"run" toml:"run" yaml:"run"`

Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"`
Only interface{} `json:"only,omitempty" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"`
Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"`
Glob string `json:"glob,omitempty" mapstructure:"glob" toml:"glob,omitempty" yaml:",omitempty"`
Files string `json:"files,omitempty" mapstructure:"files" toml:"files,omitempty" yaml:",omitempty"`
Env map[string]string `json:"env,omitempty" mapstructure:"env" toml:"env,omitempty" yaml:",omitempty"`

Root string `json:"root,omitempty" mapstructure:"root" toml:"root,omitempty" yaml:",omitempty"`
Exclude string `json:"exclude,omitempty" mapstructure:"exclude" toml:"exclude,omitempty" yaml:",omitempty"`
Priority int `json:"priority,omitempty" mapstructure:"priority" toml:"priority,omitempty" yaml:",omitempty"`

FailText string `json:"fail_text,omitempty" mapstructure:"fail_text" toml:"fail_text,omitempty" yaml:"fail_text,omitempty"`
Interactive bool `json:"interactive,omitempty" mapstructure:"interactive" toml:"interactive,omitempty" yaml:",omitempty"`
UseStdin bool `json:"use_stdin,omitempty" mapstructure:"use_stdin" toml:"use_stdin,omitempty" yaml:",omitempty"`
StageFixed bool `json:"stage_fixed,omitempty" mapstructure:"stage_fixed" toml:"stage_fixed,omitempty" yaml:"stage_fixed,omitempty"`
}

func (c Command) Validate() error {
Expand Down
20 changes: 10 additions & 10 deletions internal/config/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ type Hook struct {
// Should be unmarshalled with `mapstructure:"commands"`
// But replacing '{cmd}' is still an issue
// Unmarshalling it manually, so omit auto unmarshalling
Commands map[string]*Command `mapstructure:"-" yaml:",omitempty" json:"commands,omitempty" toml:"commands,omitempty"`
Commands map[string]*Command `json:"commands,omitempty" mapstructure:"-" toml:"commands,omitempty" yaml:",omitempty"`

// Should be unmarshalled with `mapstructure:"scripts"`
// But parsing keys with dots in it is still an issue: https://github.com/spf13/viper/issues/324
// Unmarshalling it manually, so omit auto unmarshalling
Scripts map[string]*Script `mapstructure:"-" yaml:",omitempty" json:"scripts,omitempty" toml:"scripts,omitempty"`

Files string `mapstructure:"files" yaml:",omitempty" json:"files,omitempty" toml:"files,omitempty"`
Parallel bool `mapstructure:"parallel" yaml:",omitempty" json:"parallel,omitempty" toml:"parallel,omitempty"`
Piped bool `mapstructure:"piped" yaml:",omitempty" json:"piped,omitempty" toml:"piped,omitempty"`
Follow bool `mapstructure:"follow" yaml:",omitempty" json:"follow,omitempty" toml:"follow,omitempty"`
ExcludeTags []string `mapstructure:"exclude_tags" yaml:"exclude_tags,omitempty" json:"exclude_tags,omitempty" toml:"exclude_tags,omitempty"`
Skip interface{} `mapstructure:"skip" yaml:",omitempty" json:"skip,omitempty" toml:"skip,omitempty,inline"`
Only interface{} `mapstructure:"only" yaml:",omitempty" json:"only,omitempty" toml:"only,omitempty,inline"`
Scripts map[string]*Script `json:"scripts,omitempty" mapstructure:"-" toml:"scripts,omitempty" yaml:",omitempty"`

Files string `json:"files,omitempty" mapstructure:"files" toml:"files,omitempty" yaml:",omitempty"`
Parallel bool `json:"parallel,omitempty" mapstructure:"parallel" toml:"parallel,omitempty" yaml:",omitempty"`
Piped bool `json:"piped,omitempty" mapstructure:"piped" toml:"piped,omitempty" yaml:",omitempty"`
Follow bool `json:"follow,omitempty" mapstructure:"follow" toml:"follow,omitempty" yaml:",omitempty"`
ExcludeTags []string `json:"exclude_tags,omitempty" mapstructure:"exclude_tags" toml:"exclude_tags,omitempty" yaml:"exclude_tags,omitempty"`
Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"`
Only interface{} `json:"only,omitempty" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"`
}

func (h *Hook) Validate() error {
Expand Down
8 changes: 4 additions & 4 deletions internal/config/remote.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package config

type Remote struct {
GitURL string `mapstructure:"git_url" yaml:"git_url" json:"git_url,omitempty" toml:"git_url"`
Ref string `mapstructure:"ref,omitempty" yaml:",omitempty" json:"ref,omitempty" toml:"ref,omitempty"`
GitURL string `json:"git_url,omitempty" mapstructure:"git_url" toml:"git_url" yaml:"git_url"`
Ref string `json:"ref,omitempty" mapstructure:"ref,omitempty" toml:"ref,omitempty" yaml:",omitempty"`
// Deprecated
Config string `mapstructure:"config,omitempty" yaml:",omitempty" json:"config,omitempty" toml:"config,omitempty"`
Configs []string `mapstructure:"configs,omitempty" yaml:",omitempty" json:"configs,omitempty" toml:"configs,omitempty"`
Config string `json:"config,omitempty" mapstructure:"config,omitempty" toml:"config,omitempty" yaml:",omitempty"`
Configs []string `json:"configs,omitempty" mapstructure:"configs,omitempty" toml:"configs,omitempty" yaml:",omitempty"`
}

func (r *Remote) Configured() bool {
Expand Down
18 changes: 9 additions & 9 deletions internal/config/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import (
)

type Script struct {
Runner string `mapstructure:"runner" yaml:"runner" json:"runner" toml:"runner"`
Runner string `json:"runner" mapstructure:"runner" toml:"runner" yaml:"runner"`

Skip interface{} `mapstructure:"skip" yaml:",omitempty" json:"skip,omitempty" toml:"skip,omitempty,inline"`
Only interface{} `mapstructure:"only" yaml:",omitempty" json:"only,omitempty" toml:"only,omitempty,inline"`
Tags []string `mapstructure:"tags" yaml:",omitempty" json:"tags,omitempty" toml:"tags,omitempty"`
Env map[string]string `mapstructure:"env" yaml:",omitempty" json:"env,omitempty" toml:"env,omitempty"`
Skip interface{} `json:"skip,omitempty" mapstructure:"skip" toml:"skip,omitempty,inline" yaml:",omitempty"`
Only interface{} `json:"only,omitempty" mapstructure:"only" toml:"only,omitempty,inline" yaml:",omitempty"`
Tags []string `json:"tags,omitempty" mapstructure:"tags" toml:"tags,omitempty" yaml:",omitempty"`
Env map[string]string `json:"env,omitempty" mapstructure:"env" toml:"env,omitempty" yaml:",omitempty"`

FailText string `mapstructure:"fail_text" yaml:"fail_text,omitempty" json:"fail_text,omitempty" toml:"fail_text,omitempty"`
Interactive bool `mapstructure:"interactive" yaml:",omitempty" json:"interactive,omitempty" toml:"interactive,omitempty"`
UseStdin bool `mapstructure:"use_stdin" yaml:",omitempty" json:"use_stdin,omitempty" toml:"use_stdin,omitempty"`
StageFixed bool `mapstructure:"stage_fixed" yaml:"stage_fixed,omitempty" json:"stage_fixed,omitempty" toml:"stage_fixed,omitempty"`
FailText string `json:"fail_text,omitempty" mapstructure:"fail_text" toml:"fail_text,omitempty" yaml:"fail_text,omitempty"`
Interactive bool `json:"interactive,omitempty" mapstructure:"interactive" toml:"interactive,omitempty" yaml:",omitempty"`
UseStdin bool `json:"use_stdin,omitempty" mapstructure:"use_stdin" toml:"use_stdin,omitempty" yaml:",omitempty"`
StageFixed bool `json:"stage_fixed,omitempty" mapstructure:"stage_fixed" toml:"stage_fixed,omitempty" yaml:"stage_fixed,omitempty"`
}

func (s Script) DoSkip(gitState git.State) bool {
Expand Down
4 changes: 2 additions & 2 deletions internal/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"crypto/md5"
"encoding/hex"
"fmt"
"errors"
"io"
"os"
"path/filepath"
Expand All @@ -31,7 +31,7 @@ const (
var (
lefthookChecksumRegexp = regexp.MustCompile(`(\w+)\s+(\d+)`)
configGlob = glob.MustCompile("{.,}lefthook.{yml,yaml,json,toml}")
errNoConfig = fmt.Errorf("no lefthook config found")
errNoConfig = errors.New("no lefthook config found")
)

// Install installs the hooks from config file to the .git/hooks.
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/run/exec/execute_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (e CommandExecutor) Execute(ctx context.Context, opts Options, out io.Write
}

root, _ := filepath.Abs(opts.Root)
envs := make([]string, len(opts.Env))
envs := make([]string, 0, len(opts.Env))
for name, value := range opts.Env {
envs = append(
envs,
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (r *Runner) RunAll(ctx context.Context, sourceDirs []string) {
defer log.StopSpinner()
}

scriptDirs := make([]string, len(sourceDirs))
scriptDirs := make([]string, 0, len(sourceDirs))
for _, sourceDir := range sourceDirs {
scriptDirs = append(scriptDirs, filepath.Join(
sourceDir, r.HookName,
Expand Down
4 changes: 2 additions & 2 deletions internal/log/skip_settings_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package log

import (
"fmt"
"strconv"
"testing"
)

Expand Down Expand Up @@ -85,7 +85,7 @@ func TestSkipSetting(t *testing.T) {
},
},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var settings SkipSettings

(&settings).ApplySettings(tt.tags, tt.settings)
Expand Down
Loading