From 1461d87272fe4bdd40044bb38ee90e9e89669c07 Mon Sep 17 00:00:00 2001 From: Wim Spaargaren Date: Wed, 10 Apr 2024 00:18:59 +0200 Subject: [PATCH] initial project setup --- .gitignore | 1 + .golangci.yml | 221 ++++++++++++++++++++++++++++++++++ .prolayout.yml | 6 + LICENSE | 21 ++++ Makefile | 57 +++++++++ README.md | 24 ++++ go.mod | 16 +++ go.sum | 16 +++ internal/analyzer/analyzer.go | 134 +++++++++++++++++++++ internal/errors/errors.go | 22 ++++ internal/model/config.go | 21 ++++ main.go | 26 ++++ tests/analysis_test.go | 138 +++++++++++++++++++++ tests/testdata/bar/bar.go | 1 + tests/testdata/bar/baz/baz.go | 1 + tests/testdata/cmd/main.go | 1 + 16 files changed, 706 insertions(+) create mode 100644 .gitignore create mode 100644 .golangci.yml create mode 100644 .prolayout.yml create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/analyzer/analyzer.go create mode 100644 internal/errors/errors.go create mode 100644 internal/model/config.go create mode 100644 main.go create mode 100644 tests/analysis_test.go create mode 100644 tests/testdata/bar/bar.go create mode 100644 tests/testdata/bar/baz/baz.go create mode 100644 tests/testdata/cmd/main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..27a3afb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +reports diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..dd902be --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,221 @@ +run: + concurrency: 3 + timeout: 15m + issues-exit-code: 1 + tests: true + + skip-dirs: + - data + + # list of build tags, all linters use it. Default is empty list + +# output configuration options +output: + # colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number" + format: code-climate + + # print lines of code with issue, default is true + print-issued-lines: true + + # print linter name in the end of issue text, default is true + print-linter-name: true + + uniq-by-line: false + +# all available settings of specific linters +linters-settings: + depguard: + rules: + main: + deny: + - pkg: "github.com/tj/assert" + desc: not allowed, did you mean testify assert? + gci: + local-prefixes: github.com/wimspaargaren/prolayout + errcheck: + check-type-assertions: false + check-foonk: true + + revive: + ignore-generated-header: true + enable-all-rules: true + rules: + - name: add-constant + disabled: true + - name: var-naming + severity: warning + disabled: false + arguments: + - ["ID"] # AllowList + - [] # DenyList + - name: argument-limit + severity: warning + disabled: true + - name: banned-characters + disabled: true + - name: max-public-structs + disabled: true + - name: file-header + disabled: true + - name: cognitive-complexity + disabled: false + arguments: [15] + - name: unused-receiver + disabled: true + - name: function-length + disabled: false + arguments: [30, 0] + - name: line-length-limit + disabled: true + - name: cyclomatic + disabled: false + arguments: [7] + - name: function-result-limit + severity: warning + disabled: false + arguments: [3] + - name: import-alias-naming + severity: warning + disabled: false + arguments: + - "^[a-z][a-z0-9A-Z]{0,}$" + govet: + check-shadowing: false + + golint: + min-confidence: 0.8 + + gofmt: + simplify: true + gocyclo: + min-complexity: 18 + maligned: + suggest-new: true + dupl: + threshold: 175 + goconst: + min-len: 3 + min-occurrences: 3 + funlen: + lines: 80 + statements: 50 + godox: + keywords: + - HACK + - OPTIMIZE + - TODO + - BUG + misspell: + locale: UK + ignore-words: + - color + - center + - centers + lll: + line-length: 200 + tab-width: 2 + unused: + check-exported: false + + unparam: + check-exported: false + + nakedret: + max-func-lines: 0 + prealloc: + simple: true + range-loops: true # Report preallocation suggestions on range loops, true by default + for-loops: false # Report preallocation suggestions on for loops, false by default + gocritic: + disabled-checks: + - regexpMust + + enabled-tags: + - performance + + settings: # settings passed to gocritic + captLocal: # must be valid enabled check name + paramsOnly: true + rangeValCopy: + sizeThreshold: 32 + +linters: + disable-all: true + enable: + - asciicheck + - bodyclose + - dupl + - errcheck + - errorlint + - exhaustive + - exportloopref + - funlen + - gci + - gochecknoglobals + - gochecknoinits + - gocognit + - goconst + - gocritic + - gocyclo + - godox + - gofmt + - gofumpt + - goheader + - revive + - gomodguard + - goprintffuncname + - gosimple + - govet + - ineffassign + - misspell + - nakedret + - nestif + - noctx + - nolintlint + - rowserrcheck + - sqlclosecheck + - staticcheck + - tparallel + - typecheck + - unconvert + - unparam + - whitespace + - dogsled + - godot + - goerr113 + - goimports + - lll + - prealloc + - testpackage + - depguard + + fast: false + +severity: + default-severity: error + rules: + - linters: + - gomodguard + severity: warning + +issues: + exclude-rules: + - path: _test\.go + linters: + - funlen + + - linters: + - staticcheck + text: "SA9003:" + + - linters: + - lll + text: "^//go:generate " + + exclude-use-default: false + + max-issues-per-linter: 0 + + max-same-issues: 0 + + new: false diff --git a/.prolayout.yml b/.prolayout.yml new file mode 100644 index 0000000..af31249 --- /dev/null +++ b/.prolayout.yml @@ -0,0 +1,6 @@ +module: "github.com/wimspaargaren/prolayout" +root: + - name: "bar" + + + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..48c669a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Wim Spaargaren + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..47d0257 --- /dev/null +++ b/Makefile @@ -0,0 +1,57 @@ +.PHONY: lint test format + +golangciLintVersion = "v1.55.2" +gofumptVersion = "v0.5.0" +gciVersion = "v0.11.0" +govulncheckVersion = "v1.0.1" + +# Lint Go Code +$(GOBIN)/golangci-lint: + @go install github.com/golangci/golangci-lint/cmd/golangci-lint@${golangciLintVersion} + +lint: | $(GOBIN)/golangci-lint + @echo Linting... + @golangci-lint -v --concurrency=3 --config=.golangci.yml --issues-exit-code=1 run \ + --out-format=colored-line-number + +# Format Go Code +$(GOBIN)/gofumpt: + @go install mvdan.cc/gofumpt@${gofumptVersion} + @go mod tidy + +gofumpt: | $(GOBIN)/gofumpt + @gofumpt -w $(shell ls -d $(PWD)/*/) + +# Format imports +$(GOBIN)/gci: + @go install github.com/daixiang0/gci@${gciVersion} + @go mod tidy + +gci: | $(GOBIN)/gci + @gci write --section Standard --section Default --section "Prefix(github.com/wimspaargaren/prolayout)" $(shell ls -d $(PWD)/*) + +# Run unit tests and generate coverage report +test: + @mkdir -p reports + @go test -coverprofile=reports/codecoverage_all.cov ./... -cover -race -p=4 + @go tool cover -func=reports/codecoverage_all.cov > reports/functioncoverage.out + @go tool cover -html=reports/codecoverage_all.cov -o reports/coverage.html + @echo "View report at $(PWD)/reports/coverage.html" + @tail -n 1 reports/functioncoverage.out + +# Opens coverage report in browser +coverage-report: + @open ./reports/coverage.html + +# Formats code +format: + @make gofumpt + @make gci + @go mod tidy + +# Install and run govulncheck tool +$(GOBIN)/govulncheck: + @go install golang.org/x/vuln/cmd/govulncheck@${govulncheckVersion} + +govulncheck: | $(GOBIN)/govulncheck + @govulncheck -test ./... diff --git a/README.md b/README.md new file mode 100644 index 0000000..8a0ae9d --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# ProLayout + +Pro(ject) layout is a static analysis tool that allow you to lint the project structure of your go project. + +# Why + +Since Go does not enforce any real project structure, we wanted to have a static analysis tool, to help us to ensure projects are structured in a similar fashion. + +# Example configuration file + +```YAML +module: "github.com/wimspaargaren/prolayout" +root: + - name: "cmd" + dirs: + - name: ".*" + files: + - "main.go" + - name: "internal" + - name: "pkg" + - name: "tests" + files: + - name: ".*_test.go" +``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..22ea90d --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module github.com/wimspaargaren/prolayout + +go 1.22.0 + +require ( + github.com/stretchr/testify v1.9.0 + golang.org/x/tools v0.20.0 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + golang.org/x/mod v0.17.0 // indirect + golang.org/x/sync v0.7.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9b02d14 --- /dev/null +++ b/go.sum @@ -0,0 +1,16 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY= +golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/analyzer/analyzer.go b/internal/analyzer/analyzer.go new file mode 100644 index 0000000..38be6a0 --- /dev/null +++ b/internal/analyzer/analyzer.go @@ -0,0 +1,134 @@ +// Package analyzer contains the logic to validate the project structure. +package analyzer + +import ( + "regexp" + "strings" + + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/go/analysis/passes/inspect" + + "github.com/wimspaargaren/prolayout/internal/errors" + "github.com/wimspaargaren/prolayout/internal/model" +) + +// New creates a new analyzer for a given root directory. +func New(root model.Root) *analysis.Analyzer { + runner := newRunner(root) + return &analysis.Analyzer{ + Name: "prolayout", + Doc: "Validates if a project's folder structure adheres to the given folder config.", + Run: runner.run, + Requires: []*analysis.Analyzer{inspect.Analyzer}, + } +} + +type runner struct { + Root model.Root +} + +func newRunner(root model.Root) *runner { + return &runner{Root: root} +} + +func (r *runner) run(pass *analysis.Pass) (any, error) { + err := r.assess(pass) + if err != nil { + return nil, err + } + + return nil, nil +} + +func (r *runner) assess(pass *analysis.Pass) error { + dir, err := r.assessDir(pass) + if err != nil { + return err + } + + return r.assessFiles(pass, dir) +} + +func (r *runner) assessDir(pass *analysis.Pass) (*model.Dir, error) { + module := r.Root.Module + packagePathWithoutModule := strings.ReplaceAll(pass.Pkg.Path(), module, "") + packagePathWithoutModule = strings.TrimPrefix(packagePathWithoutModule, "/") + packageSplittedPerFolder := splitPath(packagePathWithoutModule) + dirs := r.Root.Root + dir := &model.Dir{} + + for _, folder := range packageSplittedPerFolder { + if len(dirs) == 0 { + return nil, nil + } + if strings.HasSuffix(folder, ".test") { + return nil, nil + } + + res, ok, err := matchDir(dirs, folder) + if err != nil { + return nil, err + } + if !ok { + pass.ReportRangef(pass.Files[0], "package not allowed: %s, %s not found in allowed names: [%s]", packagePathWithoutModule, folder, strings.Join(dirsNames(dirs), ",")) + break + } + dir = res + dirs = res.Dirs + } + return dir, nil +} + +func dirsNames(dirs []*model.Dir) []string { + names := make([]string, len(dirs)) + for i, d := range dirs { + names[i] = d.Name + } + return names +} + +func (r *runner) assessFiles(pass *analysis.Pass, dir *model.Dir) error { + if dir == nil || len(dir.Files) == 0 { + return nil + } + for _, file := range pass.Files { + matchedFile, err := r.matchFiles(dir.Files, file.Name.Name) + if err != nil { + return err + } + if !matchedFile { + pass.ReportRangef(file, "file not allowed in this folder: %s", file.Name.Name) + } + } + return nil +} + +func (r *runner) matchFiles(files []*model.File, name string) (bool, error) { + for _, f := range files { + match, err := regexp.MatchString(f.Name, name+".go") + if err != nil { + return false, errors.ErrInvalidFileNameRegex{FileName: f.Name} + } + if match { + return true, nil + } + } + return false, nil +} + +func matchDir(dir []*model.Dir, name string) (*model.Dir, bool, error) { + for _, d := range dir { + match, err := regexp.MatchString(d.Name, name) + if err != nil { + return nil, false, errors.ErrInvalidDirNameRegex{DirName: d.Name} + } + if match { + return d, true, nil + } + } + return nil, false, nil +} + +func splitPath(path string) []string { + return strings.Split(path, "/") +} diff --git a/internal/errors/errors.go b/internal/errors/errors.go new file mode 100644 index 0000000..583de6d --- /dev/null +++ b/internal/errors/errors.go @@ -0,0 +1,22 @@ +// Package errors contains the error types that can be returned by the analyzer. +package errors + +import "fmt" + +// ErrInvalidFileNameRegex is returned when a file name is not a valid regular expression. +type ErrInvalidFileNameRegex struct { + FileName string +} + +func (e ErrInvalidFileNameRegex) Error() string { + return fmt.Sprintf("file name \"%s\" is not a valid regular expression", e.FileName) +} + +// ErrInvalidDirNameRegex is returned when a directory name is not a valid regular expression. +type ErrInvalidDirNameRegex struct { + DirName string +} + +func (e ErrInvalidDirNameRegex) Error() string { + return fmt.Sprintf("directory name \"%s\" is not a valid regular expression", e.DirName) +} diff --git a/internal/model/config.go b/internal/model/config.go new file mode 100644 index 0000000..d10976c --- /dev/null +++ b/internal/model/config.go @@ -0,0 +1,21 @@ +// Package model contains the project structure definitions. +package model + +// Root represents the root of the project structure. +type Root struct { + Module string `yaml:"module"` + Root []*Dir `yaml:"root"` +} + +// Dir represents a directory in the project structure. +type Dir struct { + Name string `yaml:"name"` + + Files []*File `yaml:"files"` + Dirs []*Dir `yaml:"dirs"` +} + +// File represents a file in the project structure. +type File struct { + Name string `yaml:"name"` +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..5011e0f --- /dev/null +++ b/main.go @@ -0,0 +1,26 @@ +// Package main bootstrap the analyzer to validate the project structure. +package main + +import ( + "log" + "os" + + "golang.org/x/tools/go/analysis/singlechecker" + "gopkg.in/yaml.v3" + + "github.com/wimspaargaren/prolayout/internal/analyzer" + "github.com/wimspaargaren/prolayout/internal/model" +) + +func main() { + data, err := os.ReadFile(".prolayout.yml") + if err != nil { + log.Fatalf("error: %v", err) + } + t := model.Root{} + err = yaml.Unmarshal(data, &t) + if err != nil { + log.Fatalf("error: %v", err) + } + singlechecker.Main(analyzer.New(t)) +} diff --git a/tests/analysis_test.go b/tests/analysis_test.go new file mode 100644 index 0000000..9123b71 --- /dev/null +++ b/tests/analysis_test.go @@ -0,0 +1,138 @@ +package main + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "golang.org/x/tools/go/analysis/analysistest" + + "github.com/wimspaargaren/prolayout/internal/analyzer" + "github.com/wimspaargaren/prolayout/internal/model" +) + +func TestPassingAnalysis(t *testing.T) { + wd, err := os.Getwd() + require.NoError(t, err) + testdataPath := filepath.Join(wd, "testdata") + + tests := []struct { + name string + input model.Root + }{ + { + name: "empty", + input: model.Root{ + Module: "_" + testdataPath, + }, + }, + { + name: "allow all", + input: model.Root{ + Module: "_" + testdataPath, + Root: []*model.Dir{ + { + Name: ".*", + }, + }, + }, + }, + { + name: "ensure matchin top to bottom", + input: model.Root{ + Module: "_" + testdataPath, + Root: []*model.Dir{ + { + Name: "bar", + Dirs: []*model.Dir{ + { + Name: "baz", + }, + }, + }, + { + Name: ".*", + Dirs: []*model.Dir{ + { + Name: "other", + }, + }, + }, + }, + }, + }, + { + name: "extensive structure", + input: model.Root{ + Module: "_" + testdataPath, + Root: []*model.Dir{ + { + Name: "cmd", + Files: []*model.File{ + { + Name: "main.go", + }, + }, + }, + { + Name: "bar", + Files: []*model.File{ + { + Name: "b.*r.go", + }, + }, + Dirs: []*model.Dir{ + { + Name: "^baz$", + Files: []*model.File{ + { + Name: "baz", + }, + }, + }, + }, + }, + }, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + analysistest.Run(t, testdataPath, analyzer.New(tt.input), "./...") + }) + } + + analysistest.Run(t, testdataPath, analyzer.New(model.Root{ + Module: "_" + testdataPath, + Root: []*model.Dir{ + { + Name: "cmd", + Files: []*model.File{ + { + Name: "main.go", + }, + }, + }, + { + Name: "bar", + Files: []*model.File{ + { + Name: "b.*r.go", + }, + }, + Dirs: []*model.Dir{ + { + Name: "^baz$", + Files: []*model.File{ + { + Name: "baz", + }, + }, + }, + }, + }, + }, + }), "./...") +} diff --git a/tests/testdata/bar/bar.go b/tests/testdata/bar/bar.go new file mode 100644 index 0000000..ddac0fa --- /dev/null +++ b/tests/testdata/bar/bar.go @@ -0,0 +1 @@ +package bar diff --git a/tests/testdata/bar/baz/baz.go b/tests/testdata/bar/baz/baz.go new file mode 100644 index 0000000..d776876 --- /dev/null +++ b/tests/testdata/bar/baz/baz.go @@ -0,0 +1 @@ +package baz diff --git a/tests/testdata/cmd/main.go b/tests/testdata/cmd/main.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/tests/testdata/cmd/main.go @@ -0,0 +1 @@ +package main