-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
105 lines (93 loc) · 2.64 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"flag"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/flowdev/spaghetti-cutter/config"
"github.com/flowdev/spaghetti-cutter/deps"
"github.com/flowdev/spaghetti-cutter/parse"
"github.com/flowdev/spaghetti-cutter/size"
"github.com/flowdev/spaghetti-cutter/x/dirs"
"github.com/flowdev/spaghetti-cutter/x/pkgs"
)
func main() {
rc := cut(os.Args[1:])
if rc != 0 {
os.Exit(rc)
}
}
func cut(args []string) int {
const (
usageShort = " (shorthand)"
defaultRoot = "."
usageRoot = "root directory of the project"
defaultNoErr = false
usageNoErr = "don't report errors and don't exit with an error"
)
var startDir string
var noErr bool
fs := flag.NewFlagSet("spaghetti-cutter", flag.ExitOnError)
fs.StringVar(&startDir, "root", defaultRoot, usageRoot)
fs.StringVar(&startDir, "r", defaultRoot, usageRoot+usageShort)
fs.BoolVar(&noErr, "noerror", defaultNoErr, usageNoErr)
fs.BoolVar(&noErr, "e", defaultNoErr, usageNoErr+usageShort)
err := fs.Parse(args)
if err != nil {
log.Printf("FATAL - %v", err)
return 2
}
root, err := dirs.FindRoot(startDir, config.File)
if err != nil {
log.Printf("FATAL - %v", err)
return 3
}
cfgFile := filepath.Join(root, config.File)
cfgBytes, err := ioutil.ReadFile(cfgFile)
if err != nil {
log.Printf("FATAL - unable to read configuration file %q: %v", cfgFile, err)
return 4
}
cfg, err := config.Parse(cfgBytes, cfgFile)
if err != nil {
log.Printf("FATAL - %v", err)
return 5
}
log.Printf("INFO - configuration 'allowOnlyIn': %s", cfg.AllowOnlyIn)
log.Printf("INFO - configuration 'allowAdditionally': %s", cfg.AllowAdditionally)
log.Printf("INFO - configuration 'god': %s", cfg.God)
log.Printf("INFO - configuration 'tool': %s", cfg.Tool)
log.Printf("INFO - configuration 'db': %s", cfg.DB)
log.Printf("INFO - configuration 'size': %d", cfg.Size)
log.Printf("INFO - configuration 'noGod': %t", cfg.NoGod)
log.Printf("INFO - no errors are reported: %t", noErr)
packs, err := parse.DirTree(root)
if err != nil {
log.Printf("FATAL - %v", err)
return 6
}
rootPkg := parse.RootPkg(packs)
log.Printf("INFO - root package: %s", rootPkg)
pkgInfos := pkgs.UniquePackages(packs)
var errs []error
for _, pkgInfo := range pkgInfos {
errs = addErrors(errs, deps.Check(pkgInfo.Pkg, rootPkg, cfg))
errs = addErrors(errs, size.Check(pkgInfo.Pkg, rootPkg, cfg.Size))
}
retCode := 0
if len(errs) > 0 {
for _, err = range errs {
log.Printf("ERROR - %v", err)
}
if !noErr {
retCode = 1
}
} else {
log.Print("INFO - No errors found.")
}
return retCode
}
func addErrors(errs []error, newErrs []error) []error {
return append(errs, newErrs...)
}