Skip to content

Commit 2dc5fd5

Browse files
committed
Add support for multiple limit path prefixes
1 parent d5d9c6d commit 2dc5fd5

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Purpose of this tool is to provide a visual overview of your program by using th
2020

2121
- focus specific package in a program
2222
- group functions by package and methods by type
23-
- limit packages to custom prefix path
24-
- ignore packages containing custom prefix
23+
- limit packages to custom path prefixes
24+
- ignore packages containing path prefixes
2525

2626
### Output preview
2727

@@ -100,11 +100,11 @@ Represents | Style
100100
-focus string
101101
Focus package with import path or name. (default: main)
102102
-limit string
103-
Limit package path to prefix.
103+
Limit package paths to prefix. (separate multiple by comma)
104104
-group string
105-
Grouping functions by [pkg, type] (separate multiple by comma).
105+
Grouping functions by [pkg, type]. (separate multiple by comma)
106106
-ignore string
107-
Ignore package paths with prefix (separate multiple by comma).
107+
Ignore package paths with prefix. (separate multiple by comma)
108108
-minlen uint
109109
Minimum edge length (for wider output). (default: 2)
110110
-nodesep float

main.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var Version = "0.0.0-src"
2121

2222
var (
2323
focusFlag = flag.String("focus", "main", "Focus package with name or import path.")
24-
limitFlag = flag.String("limit", "", "Limit package path to prefix.")
24+
limitFlag = flag.String("limit", "", "Limit package paths to prefix. (separate multiple by comma)")
2525
groupFlag = flag.String("group", "", "Grouping functions by [pkg, type] (separate multiple by comma).")
2626
ignoreFlag = flag.String("ignore", "", "Ignore package paths with prefix (separate multiple by comma).")
2727
testFlag = flag.Bool("tests", false, "Include test code.")
@@ -44,14 +44,6 @@ func main() {
4444
log.SetFlags(log.Lmicroseconds)
4545
}
4646

47-
ignorePaths := []string{}
48-
for _, p := range strings.Split(*ignoreFlag, ",") {
49-
p = strings.TrimSpace(p)
50-
if p != "" {
51-
ignorePaths = append(ignorePaths, p)
52-
}
53-
}
54-
5547
groupBy := make(map[string]bool)
5648
for _, g := range strings.Split(*groupFlag, ",") {
5749
g := strings.TrimSpace(g)
@@ -65,13 +57,29 @@ func main() {
6557
groupBy[g] = true
6658
}
6759

68-
if err := run(&build.Default, *focusFlag, *limitFlag, groupBy, ignorePaths, *testFlag, flag.Args()); err != nil {
60+
limitPaths := []string{}
61+
for _, p := range strings.Split(*limitFlag, ",") {
62+
p = strings.TrimSpace(p)
63+
if p != "" {
64+
limitPaths = append(limitPaths, p)
65+
}
66+
}
67+
68+
ignorePaths := []string{}
69+
for _, p := range strings.Split(*ignoreFlag, ",") {
70+
p = strings.TrimSpace(p)
71+
if p != "" {
72+
ignorePaths = append(ignorePaths, p)
73+
}
74+
}
75+
76+
if err := run(&build.Default, *focusFlag, groupBy, limitPaths, ignorePaths, *testFlag, flag.Args()); err != nil {
6977
fmt.Fprintf(os.Stderr, "go-callvis: %s\n", err)
7078
os.Exit(1)
7179
}
7280
}
7381

74-
func run(ctxt *build.Context, focus, limitPath string, groupBy map[string]bool, ignorePaths []string, tests bool, args []string) error {
82+
func run(ctxt *build.Context, focus string, groupBy map[string]bool, limitPaths, ignorePaths []string, tests bool, args []string) error {
7583
if len(args) == 0 {
7684
return fmt.Errorf("missing arguments")
7785
}
@@ -154,7 +162,7 @@ func run(ctxt *build.Context, focus, limitPath string, groupBy map[string]bool,
154162
logf("analysis took: %v", time.Since(t0))
155163

156164
return printOutput(mains[0].Pkg, result.CallGraph,
157-
focusPkg, limitPath, ignorePaths, groupBy)
165+
focusPkg, limitPaths, ignorePaths, groupBy)
158166
}
159167

160168
func logf(f string, a ...interface{}) {

output.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
var output io.Writer = os.Stdout
1616

17-
func printOutput(mainPkg *types.Package, cg *callgraph.Graph, focusPkg *build.Package, limitPath string, ignorePaths []string, groupBy map[string]bool) error {
17+
func printOutput(mainPkg *types.Package, cg *callgraph.Graph, focusPkg *build.Package, limitPaths, ignorePaths []string, groupBy map[string]bool) error {
1818
groupType := groupBy["type"]
1919
groupPkg := groupBy["pkg"]
2020

@@ -39,6 +39,32 @@ func printOutput(mainPkg *types.Package, cg *callgraph.Graph, focusPkg *build.Pa
3939

4040
cg.DeleteSyntheticNodes()
4141

42+
logf("%d limit prefixes: %v", len(limitPaths), limitPaths)
43+
logf("%d ignore prefixes: %v", len(ignorePaths), ignorePaths)
44+
45+
var inLimit = func(from, to *callgraph.Node) bool {
46+
if len(limitPaths) == 0 {
47+
return true
48+
}
49+
var fromOk, toOk bool
50+
fromPath := from.Func.Pkg.Pkg.Path()
51+
toPath := to.Func.Pkg.Pkg.Path()
52+
for _, p := range limitPaths {
53+
if strings.HasPrefix(fromPath, p) {
54+
fromOk = true
55+
}
56+
if strings.HasPrefix(toPath, p) {
57+
toOk = true
58+
}
59+
if fromOk && toOk {
60+
logf("in limit: %s -> %s", from, to)
61+
return true
62+
}
63+
}
64+
logf("NOT in limit: %s -> %s", from, to)
65+
return false
66+
}
67+
4268
err := callgraph.GraphVisitEdges(cg, func(edge *callgraph.Edge) error {
4369
caller := edge.Caller
4470
callee := edge.Callee
@@ -57,9 +83,8 @@ func printOutput(mainPkg *types.Package, cg *callgraph.Graph, focusPkg *build.Pa
5783
return nil
5884
}
5985

60-
// limit to path prefix
61-
if !(strings.HasPrefix(callerPkg.Path(), limitPath) &&
62-
strings.HasPrefix(calleePkg.Path(), limitPath)) {
86+
// limit path prefixes
87+
if !inLimit(caller, callee) {
6388
return nil
6489
}
6590

0 commit comments

Comments
 (0)