-
Notifications
You must be signed in to change notification settings - Fork 17
/
goimportdot.go
73 lines (61 loc) · 1.94 KB
/
goimportdot.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
package main
import (
"flag"
"fmt"
"os"
"github.com/yqylovy/goimportdot/core"
)
func main() {
var ignoreGit = true
var ignoreTest = true
var onlySelfPkg = true
var packageName = ""
var root = ""
var filters = ""
var level = -1
flag.BoolVar(&ignoreGit, "ignoregit", ignoreGit, "ignore files in git")
flag.BoolVar(&ignoreTest, "ignoretest", ignoreTest, "ignore test files")
flag.BoolVar(&onlySelfPkg, "only", onlySelfPkg, "only to draw the input package")
flag.StringVar(&filters, "filter", "", "filter to (ignore/only include) package match wildcard,example: -filter=w:a*,*b;b:c means only include package start with a and ends with b, ignore package named c")
flag.StringVar(&root, "root", root, "only draw package with the graph start from root")
flag.IntVar(&level, "level", level, "show how many level , -1 for all")
flag.StringVar(&packageName, "pkg", packageName, "the package to draw")
flag.Parse()
if packageName == "" {
fmt.Println("You must specify the packge name with -pkg ")
return
}
fileFilter := []core.FileFilter{
core.HasSuffix(false, ".go"),
}
if ignoreGit {
fileFilter = append(fileFilter, core.NameContains(true, ".git"))
}
if ignoreTest {
fileFilter = append(fileFilter, core.NameContains(true, "_test.go"))
}
pkgAndImports, err := core.GetImports(packageName, fileFilter...)
if err != nil {
panic(err)
}
pkgFilters := []core.PkgFilter{}
if onlySelfPkg {
pkgFilters = append(pkgFilters, core.PkgWildcardFilter(false, packageName+"*"))
}
if root != "" {
pkgFilters = append(pkgFilters, core.RootFilter(root))
}
moreFilters, err := core.ParsePkgWildcardStr(filters)
if err != nil {
fmt.Printf("No right filter [%s], please check!", filters)
return
}
pkgFilters = append(pkgFilters, moreFilters...)
if level >= 0 {
pkgFilters = append(pkgFilters, core.PkgLevelFilter(level))
}
for _, f := range pkgFilters {
pkgAndImports = f(pkgAndImports)
}
core.WriteDot(pkgAndImports, os.Stdout)
}