-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
67 lines (56 loc) · 1.82 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cleanlinter
import (
"flag"
"fmt"
"golang.org/x/tools/go/analysis"
)
var flagSet flag.FlagSet
var (
pathToDomain string
pathToUseCase string
pathToAdapter string
pathToInfra string
verboseMode bool
)
func init() {
flagSet.StringVar(&pathToDomain, "cleanlinter_path_to_domain", "", "path to Domain layer; usually contains domain layer business rules")
flagSet.StringVar(&pathToUseCase, "cleanlinter_path_to_usecase", "", "path to UseCase layer; usually contains services, reusable handlers, the app core logic goes here")
flagSet.StringVar(&pathToAdapter, "cleanlinter_path_to_adapter", "", "path to Adapter layer; usually contains adapters to other APIs")
flagSet.StringVar(&pathToInfra, "cleanlinter_path_to_infra", "", "path to Infrastructure layer; usually contains presentation logic (http-, grpc-handlers), DB integration, and so on")
flagSet.BoolVar(&verboseMode, "cleanlinter_verbose", false, "verbose mode")
}
func NewAnalyzer() *analysis.Analyzer {
return &analysis.Analyzer{
Name: "cleanlinter",
Doc: "Clean Architecture linter. Checks your project's dependencies according to Clean Architecture pattern",
Flags: flagSet,
Run: run,
}
}
func run(pass *analysis.Pass) (any, error) {
countNonEmptyPaths := 0
if pathToDomain != "" {
countNonEmptyPaths++
}
if pathToUseCase != "" {
countNonEmptyPaths++
}
if pathToAdapter != "" {
countNonEmptyPaths++
}
if pathToInfra != "" {
countNonEmptyPaths++
}
if countNonEmptyPaths < 2 {
return nil, fmt.Errorf("please specify at least 2 layers. Otherwise there is nothing to check (got %d)", countNonEmptyPaths)
}
linter := Linter{
PathToDomain: pathToDomain,
PathToUseCase: pathToUseCase,
PathToAdapter: pathToAdapter,
PathToInfra: pathToInfra,
verboseMode: verboseMode,
}
err := linter.CheckImports(pass)
return nil, err
}