Skip to content

Commit ad6e9a2

Browse files
committed
Run tfproviderlint as a golangci-lint plugin
1 parent 8ab2e92 commit ad6e9a2

File tree

5 files changed

+52
-15
lines changed

5 files changed

+52
-15
lines changed

.github/workflows/pr-lint.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ jobs:
2828
matrix:
2929
target:
3030
- lint-golangci
31-
- lint-tfprovider
3231
- lint-examples-tf
3332
- lint-examples-sh
3433
- lint-generated

.github/workflows/push.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ jobs:
4040
matrix:
4141
target:
4242
- lint-golangci
43-
- lint-tfprovider
4443
- lint-examples-tf
4544
- lint-examples-sh
4645
- lint-generated

.golangci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ run:
44
timeout: 3m
55

66
linters-settings:
7+
custom:
8+
tfproviderlint:
9+
path: ./bin/tfproviderlint-plugin.so
10+
description: "Terraform Provider Lint Tool"
11+
original-url: https://github.com/bflad/tfproviderlint
712
errcheck:
813
exclude: errcheck_excludes.txt
914

1015
linters:
1116
enable:
1217
- gofmt
1318
- govet
19+
- tfproviderlint

GNUmakefile

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,15 @@ endif
2121
test: ## Run unit tests.
2222
go test $(TESTARGS) $(PROVIDER_SRC_DIR)
2323

24-
TFPROVIDERLINTX_CHECKS = -XAT001=false -XR003=false -XS002=false
25-
26-
fmt: tool-golangci-lint tool-tfproviderlintx tool-terraform tool-shfmt ## Format files and fix issues.
24+
fmt: tool-golangci-lint tool-terraform tool-shfmt tfproviderlint-plugin ## Format files and fix issues.
2725
gofmt -w -s .
2826
$(GOBIN)/golangci-lint run --fix
29-
$(GOBIN)/tfproviderlintx $(TFPROVIDERLINTX_CHECKS) --fix ./...
3027
$(GOBIN)/terraform fmt -recursive -list ./examples
3128
$(GOBIN)/shfmt -l -s -w ./examples
3229

33-
lint-golangci: tool-golangci-lint ## Run golangci-lint linter (same as fmt but without modifying files).
30+
lint-golangci: tool-golangci-lint tfproviderlint-plugin ## Run golangci-lint linter (same as fmt but without modifying files).
3431
$(GOBIN)/golangci-lint run
3532

36-
lint-tfprovider: tool-tfproviderlintx ## Run tfproviderlintx linter (same as fmt but without modifying files).
37-
$(GOBIN)/tfproviderlintx $(TFPROVIDERLINTX_CHECKS) ./...
38-
3933
lint-examples-tf: tool-terraform ## Run terraform linter on examples (same as fmt but without modifying files).
4034
$(GOBIN)/terraform fmt -recursive -check ./examples
4135

@@ -73,7 +67,7 @@ testacc-down: ## Teardown a GitLab instance.
7367
docker-compose down --volumes
7468

7569
testacc: ## Run acceptance tests against a GitLab instance.
76-
TF_ACC=1 GITLAB_TOKEN=$(GITLAB_TOKEN) GITLAB_BASE_URL=$(GITLAB_BASE_URL) go test -v $(PROVIDER_SRC_DIR) $(TESTARGS) -timeout 40m -tags acceptance
70+
TF_ACC=1 GITLAB_TOKEN=$(GITLAB_TOKEN) GITLAB_BASE_URL=$(GITLAB_BASE_URL) go test -v $(PROVIDER_SRC_DIR) $(TESTARGS) -timeout 40m
7771

7872
certs: ## Generate certs for the GitLab container registry
7973
mkdir -p certs
@@ -85,9 +79,6 @@ certs: ## Generate certs for the GitLab container registry
8579
tool-golangci-lint:
8680
@$(call install-tool, github.com/golangci/golangci-lint/cmd/golangci-lint)
8781

88-
tool-tfproviderlintx:
89-
@$(call install-tool, github.com/bflad/tfproviderlint/cmd/tfproviderlintx)
90-
9182
tool-tfplugindocs:
9283
@$(call install-tool, github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs)
9384

@@ -110,4 +101,7 @@ tool-terraform:
110101
@[ -f $(GOBIN)/terraform ] || { mkdir -p tmp; cd tmp; rm -rf terraform; git clone --branch $(TERRAFORM_VERSION) --depth 1 https://github.com/hashicorp/terraform.git; cd terraform; GOBIN=$(GOBIN) go install; cd ..; rm -rf terraform; }
111102

112103
clean: testacc-down
113-
@rm -rf certs/
104+
@rm -rf certs/
105+
106+
tfproviderlint-plugin:
107+
@cd tools && go build -buildmode=plugin -o $(GOBIN)/tfproviderlint-plugin.so ./cmd/tfproviderlint-plugin
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Program tfproviderlint-plugin is a custom linter plugin for golangci-lint which runs the
2+
// tfproviderlint analyzers.
3+
//
4+
// See: https://golangci-lint.run/contributing/new-linters/#create-a-plugin
5+
package main
6+
7+
import (
8+
"github.com/bflad/tfproviderlint/passes"
9+
"github.com/bflad/tfproviderlint/xpasses"
10+
"golang.org/x/tools/go/analysis"
11+
)
12+
13+
var excludes = []string{
14+
"XAT001",
15+
"XR003",
16+
"XS002",
17+
}
18+
19+
type analyzerPlugin struct{}
20+
21+
func (*analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
22+
excludesSet := make(map[string]struct{}, len(excludes))
23+
24+
for _, exclude := range excludes {
25+
excludesSet[exclude] = struct{}{}
26+
}
27+
28+
var analyzers []*analysis.Analyzer
29+
30+
for _, analyzer := range append(passes.AllChecks, xpasses.AllChecks...) {
31+
if _, isExcluded := excludesSet[analyzer.Name]; !isExcluded {
32+
analyzers = append(analyzers, analyzer)
33+
}
34+
}
35+
36+
return analyzers
37+
}
38+
39+
var AnalyzerPlugin analyzerPlugin

0 commit comments

Comments
 (0)