-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathgoviz.go
109 lines (98 loc) · 2.88 KB
/
goviz.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
106
107
108
109
package main
import (
"fmt"
"github.com/hirokidaichi/goviz/dotwriter"
"github.com/hirokidaichi/goviz/goimport"
"github.com/hirokidaichi/goviz/metrics"
"github.com/jessevdk/go-flags"
"os"
)
type options struct {
InputDir string `short:"i" long:"input" required:"true" description:"intput ploject name"`
OutputFile string `short:"o" long:"output" default:"STDOUT" description:"output file"`
Depth int `short:"d" long:"depth" default:"128" description:"max plot depth of the dependency tree"`
Reversed string `short:"f" long:"focus" description:"focus on the specific module"`
SeekPath string `short:"s" long:"search" default:"" description:"top directory of searching"`
PlotLeaf bool `short:"l" long:"leaf" default:"false" description:"whether leaf nodes are plotted"`
UseMetrics bool `short:"m" long:"metrics" default:"false" description:"display module metrics"`
}
func getOptions() (*options, error) {
options := new(options)
_, err := flags.Parse(options)
if err != nil {
return nil, err
}
return options, nil
}
func main() {
res := process()
os.Exit(res)
}
func errorf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format, args...)
}
func process() int {
options, err := getOptions()
if err != nil {
return 1
}
factory := goimport.ParseRelation(
options.InputDir,
options.SeekPath,
options.PlotLeaf,
)
if factory == nil {
errorf("inputdir does not exist.\n go get %s", options.InputDir)
return 1
}
root := factory.GetRoot()
if !root.HasFiles() {
errorf("%s has no .go files\n", root.ImportPath)
return 1
}
if 0 > options.Depth {
errorf("-d or --depth should have positive int\n")
return 1
}
output := getOutputWriter(options.OutputFile)
if options.UseMetrics {
metrics_writer := metrics.New(output)
metrics_writer.Plot(pathToNode(factory.GetAll()))
return 0
}
writer := dotwriter.New(output)
writer.MaxDepth = options.Depth
if options.Reversed == "" {
writer.PlotGraph(root)
return 0
}
writer.Reversed = true
rroot := factory.Get(options.Reversed)
if rroot == nil {
errorf("-r %s does not exist.\n ", options.Reversed)
return 1
}
if !rroot.HasFiles() {
errorf("-r %s has no go files.\n ", options.Reversed)
return 1
}
writer.PlotGraph(rroot)
return 0
}
func pathToNode(pathes []*goimport.ImportPath) []dotwriter.IDotNode {
r := make([]dotwriter.IDotNode, len(pathes))
for i, _ := range pathes {
r[i] = pathes[i]
}
return r
}
func getOutputWriter(name string) *os.File {
if name == "STDOUT" {
return os.Stdout
}
if name == "STDERR" {
return os.Stderr
}
f, _ := os.Create(name)
return f
}