From eecead4fca2bbbe71126bc396a200868101af3a8 Mon Sep 17 00:00:00 2001 From: Ivo Gosemann Date: Wed, 20 Sep 2023 10:42:06 +0200 Subject: [PATCH] add additional linter & dotimportwhitelist New config field additionalLinters to add linters for specific needs New config field dotImportWhitelist to exclude specific libraries from dot import linter --- README.md | 8 ++++++++ internal/core/config.go | 8 +++++--- internal/golangcilint/golangci_lint.go | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 622bc88..5833caa 100644 --- a/README.md +++ b/README.md @@ -223,6 +223,10 @@ golangciLint: - (*net/http.Client).Do skipDirs: - easypg/migrate/* + additionalLinters: + - ginkgolinter + dotImportWhitelist: + - github.com/onsi/ginkgo/v2 ``` The `make check` and `make static-check` targets use [`golangci-lint`](https://golangci-lint.run) to lint your code. @@ -237,6 +241,10 @@ and a list of functions to be excluded from `errcheck` linter in `errcheckExclud Refer to [`errcheck`'s README](https://github.com/kisielk/errcheck#excluding-functions) for info on the format for function signatures that `errcheck` accepts. +With `additionalLinters` you can add a list of extra linters to be enabled in the generated config file. The list of available linters can be found [here](https://golangci-lint.run/usage/linters/). + +With `dotImportWhitelist` it is possible to exclude libraries that are usually dot imported such as Ginkgo and Gomega from the `stylecheck` linter. The list expects the full import path of the library. + Take a look at `go-makefile-maker`'s own [`golangci-lint` config file](./.golangci.yaml) for an up-to-date example of what the generated config would look like. ### `goreleaser` diff --git a/internal/core/config.go b/internal/core/config.go index 9558ba6..f2440be 100644 --- a/internal/core/config.go +++ b/internal/core/config.go @@ -95,9 +95,11 @@ type GolangConfiguration struct { // GolangciLintConfiguration appears in type Configuration. type GolangciLintConfiguration struct { - CreateConfig bool `yaml:"createConfig"` - ErrcheckExcludes []string `yaml:"errcheckExcludes"` - SkipDirs []string `yaml:"skipDirs"` + CreateConfig bool `yaml:"createConfig"` + ErrcheckExcludes []string `yaml:"errcheckExcludes"` + SkipDirs []string `yaml:"skipDirs"` + AdditionalLinters []string `yaml:"additionalLinters"` + DotImportWhitelist []string `yaml:"dotImportWhitelist"` } type GoreleaserConfiguration struct { diff --git a/internal/golangcilint/golangci_lint.go b/internal/golangcilint/golangci_lint.go index ed2eb0d..c4be3dd 100644 --- a/internal/golangcilint/golangci_lint.go +++ b/internal/golangcilint/golangci_lint.go @@ -101,6 +101,15 @@ linters-settings: - unnecessaryDefer - weakCond - yodaStyleExpr +{{- if .DotImportWhitelist }} + stylecheck: + {{- if .DotImportWhitelist }} + dot-import-whitelist: + {{- range .DotImportWhitelist }} + - {{ . }} + {{- end }} + {{- end }} +{{- end }} goimports: # Put local imports after 3rd-party packages. local-prefixes: {{ .ModulePath }} @@ -175,6 +184,11 @@ linters: - unused - usestdlibvars - whitespace +{{- if .AdditionalLinters }} + {{- range .AdditionalLinters }} + - {{ . }} + {{- end }} +{{- end }} `, "\t", " ")))) type configTmplData struct { @@ -183,6 +197,8 @@ type configTmplData struct { MisspellIgnoreWords []string ErrcheckExcludes []string SkipDirs []string + DotImportWhitelist []string + AdditionalLinters []string } func RenderConfig(cfg core.GolangciLintConfiguration, vendoring bool, modulePath string, misspellIgnoreWords []string) { @@ -199,6 +215,8 @@ func RenderConfig(cfg core.GolangciLintConfiguration, vendoring bool, modulePath MisspellIgnoreWords: misspellIgnoreWords, ErrcheckExcludes: cfg.ErrcheckExcludes, SkipDirs: cfg.SkipDirs, + DotImportWhitelist: cfg.DotImportWhitelist, + AdditionalLinters: cfg.AdditionalLinters, })) fmt.Fprintln(f) // empty line at end