forked from ondrajz/go-callvis
/
main.go
152 lines (128 loc) · 3.89 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// go-callvis: a tool to help visualize the call graph of a Go program.
//
package main
import (
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"time"
"github.com/pkg/browser"
"golang.org/x/tools/go/buildutil"
)
var (
Version = "v0.4-dev"
)
var (
focusFlag = flag.String("focus", "main", "Focus specific package using name or import path.")
groupFlag = flag.String("group", "", "Grouping functions by packages and/or types [pkg, type] (separated by comma)")
limitFlag = flag.String("limit", "", "Limit package paths to given prefixes (separated by comma)")
ignoreFlag = flag.String("ignore", "", "Ignore package paths containing given prefixes (separated by comma)")
ignoreNameFlag = flag.String("ignore_name", "", "Ignore func or paths containing given words (separated by comma)")
includeFlag = flag.String("include", "", "Include package paths with given prefixes (separated by comma)")
nostdFlag = flag.Bool("nostd", false, "Omit calls to/from packages in standard library.")
nointerFlag = flag.Bool("nointer", false, "Omit calls to unexported functions.")
testFlag = flag.Bool("tests", false, "Include test code.")
debugFlag = flag.Bool("debug", false, "Enable verbose log.")
versionFlag = flag.Bool("version", false, "Show version and exit.")
httpFlag = flag.String("http", ":7878", "HTTP service address.")
skipBrowser = flag.Bool("skipbrowser", false, "Skip opening browser.")
outputFile = flag.String("file", "", "output filename - omit to use server mode")
outputFormat = flag.String("format", "svg", "output file format [svg | png | jpg | ...]")
)
func init() {
flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
// Graphviz options
flag.UintVar(&minlen, "minlen", 2, "Minimum edge length (for wider output).")
flag.Float64Var(&nodesep, "nodesep", 0.35, "Minimum space between two adjacent nodes in the same rank (for taller output).")
}
const Usage = `go-callvis: visualize call graph of a Go program.
Usage:
go-callvis [flags] package
Package should be main package, otherwise -tests flag must be used.
Flags:
`
func outputDot(fname string, outputFormat string) {
// get cmdline default for analysis
opts := analysisSetup()
if e := processListArgs(&opts); e != nil {
log.Fatalf("%v\n", e)
}
output, err := Analysis.render(opts)
if err != nil {
log.Fatalf("%v\n", err)
}
log.Println("writing dot output..")
writeErr := ioutil.WriteFile(fmt.Sprintf("%s.gv", fname), output, 0755)
if writeErr != nil {
log.Fatalf("%v\n", writeErr)
}
log.Printf("converting dot to %s..\n", outputFormat)
_, err = dotToImage(fname, outputFormat, output)
if err != nil {
log.Fatalf("%v\n", err)
}
}
func main() {
flag.Parse()
if *versionFlag {
fmt.Fprintf(os.Stderr, "go-callvis %s\n", Version)
os.Exit(0)
}
if *debugFlag {
log.SetFlags(log.Lmicroseconds)
}
args := flag.Args()
if flag.NArg() != 1 {
fmt.Fprintf(os.Stderr, Usage)
flag.PrintDefaults()
os.Exit(2)
}
tests := *testFlag
httpAddr := *httpFlag
urlAddr := parseHTTPAddr(httpAddr)
doAnalysis(&build.Default, tests, args)
http.HandleFunc("/", handler)
if *outputFile == "" {
*outputFile = "output"
if !*skipBrowser {
go openBrowser(urlAddr)
}
log.Printf("http serving at %s", urlAddr)
if err := http.ListenAndServe(httpAddr, nil); err != nil {
log.Fatal(err)
}
} else {
outputDot(*outputFile, *outputFormat)
}
}
func parseHTTPAddr(addr string) string {
host, port, _ := net.SplitHostPort(addr)
if host == "" {
host = "localhost"
}
if port == "" {
port = "80"
}
u := url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%s", host, port),
}
return u.String()
}
func openBrowser(url string) {
time.Sleep(time.Millisecond * 100)
if err := browser.OpenURL(url); err != nil {
log.Printf("OpenURL error: %v", err)
}
}
func logf(f string, a ...interface{}) {
if *debugFlag {
log.Printf(f, a...)
}
}