-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.go
86 lines (70 loc) · 2.31 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/roblaszczak/go-cleanarch/cleanarch"
)
var domainAliases = []string{"domain", "entities"}
var applicationAliases = []string{"app", "application", "usecases", "usecase", "use_cases"}
var interfacesAliases = []string{"interfaces", "interface", "adapters", "adapter"}
var infrastructureAliases = []string{"infrastructure", "infra"}
func main() {
ignoredPackages := sliceFlag{}
ignoreTests := flag.Bool("ignore-tests", false, "if flag is passed *_test.go files will be not checked")
debug := flag.Bool("debug", false, "debug mode")
flag.Var(
&ignoredPackages,
"ignore-package",
"provided packages can be imported to any layer, "+
"for example you can use`-ignore-package github.com/roblaszczak/go-cleanarch/infrastructure` to import "+
"this package to the domain",
)
domain := flag.String("domain", "", "name of the domain layer")
application := flag.String("application", "", "name of the application layer")
interfaces := flag.String("interfaces", "", "name of the interfaces layer")
infrastructure := flag.String("infrastructure", "", "name of the infrastructure layer")
flag.Parse()
var path string
aliases := make(map[string]cleanarch.Layer)
addAliases(aliases, *domain, domainAliases, cleanarch.LayerDomain)
addAliases(aliases, *application, applicationAliases, cleanarch.LayerApplication)
addAliases(aliases, *interfaces, interfacesAliases, cleanarch.LayerInterfaces)
addAliases(aliases, *infrastructure, infrastructureAliases, cleanarch.LayerInfrastructure)
if *debug {
cleanarch.Log.SetOutput(os.Stderr)
}
if len(flag.Args()) > 1 {
path = flag.Args()[1]
} else {
var err error
path, err = filepath.Abs(flag.Arg(0))
if err != nil {
panic(err)
}
}
fmt.Printf("[cleanarch] checking %s\n", path)
validator := cleanarch.NewValidator(aliases)
isValid, errors, err := validator.Validate(path, *ignoreTests, ignoredPackages)
if err != nil {
panic(err)
}
if !isValid {
for _, err := range errors {
fmt.Println(err.Error())
}
fmt.Println("Uncle Bob is not happy.")
os.Exit(1)
}
os.Exit(0)
}
func addAliases(aliases map[string]cleanarch.Layer, name string, names []string, layer cleanarch.Layer) {
if len(name) > 0 {
aliases[name] = layer
} else {
for _, n := range names {
aliases[n] = layer
}
}
}