forked from google/godepq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
godepq.go
154 lines (131 loc) · 3.31 KB
/
godepq.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
153
154
/*
Copyright (c) 2013-2016 the Godepq Authors
Use of this source code is governed by a MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
package main
import (
"errors"
"flag"
"fmt"
"go/build"
"os"
"regexp"
. "github.com/google/godepq/pkg"
)
var (
// TODO: add support for multiple from / to packages
from = flag.String("from", "", "root package")
to = flag.String("to", "", "target package for querying dependency paths")
ignore = flag.String("ignore", "", "regular expression for packages to ignore")
include = flag.String("include", "", "regular expression for packages to include (excluding packages matching -ignore)")
includeTests = flag.Bool("include-tests", false, "whether to include test imports")
includeStdlib = flag.Bool("include-stdlib", false, "whether to include go standard library imports")
allPaths = flag.Bool("all-paths", false, "whether to include all paths in the result")
output = flag.String("o", "list", "{list: print path(s), dot: export dot graph}")
)
func main() {
flag.Parse()
err := run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func run() error {
err := validateFlags()
if err != nil {
return err
}
wd, err := os.Getwd()
if err != nil {
return err
}
fromPkg, err := Resolve(*from, wd, build.Default)
if err != nil {
return err
}
var toPkg string
if *to != "" {
toPkg, err = Resolve(*to, wd, build.Default)
if err != nil {
return err
}
}
builder := Builder{
Roots: []Package{Package(fromPkg)},
IncludeTests: *includeTests,
IncludeStdlib: *includeStdlib,
BuildContext: build.Default,
BaseDir: wd,
}
if *ignore != "" {
ignoreRegexp, err := regexp.Compile(*ignore)
if err != nil {
return err
}
builder.Ignored = []*regexp.Regexp{ignoreRegexp}
}
if *include != "" {
includeRegexp, err := regexp.Compile(*include)
if err != nil {
return err
}
builder.Included = []*regexp.Regexp{includeRegexp}
}
deps, err := builder.Build()
if err != nil {
return err
}
var result Graph
if toPkg != "" {
if *allPaths {
result = deps.Forward.AllPaths(Package(fromPkg), Package(toPkg))
} else {
path := deps.Forward.SomePath(Package(fromPkg), Package(toPkg))
result = NewGraph()
result.AddPath(path)
}
} else {
result = deps.Forward
}
if result == nil || len(result) == 0 {
fmt.Printf("No path found from %q to %q\n", fromPkg, toPkg)
os.Exit(1)
}
switch *output {
case "list":
printList(Package(fromPkg), result)
return nil
case "dot":
printDot(Package(fromPkg), result)
return nil
default:
return fmt.Errorf("Unknown output format %q", *output)
}
}
func validateFlags() error {
if *from == "" {
return errors.New("-from must be set")
}
if *allPaths && *to == "" {
return errors.New("-all-paths requires a -to package")
}
if len(flag.Args()) != 0 {
return fmt.Errorf("Unexpected positional arguments: %v", flag.Args())
}
if *ignore != "" && *ignore == *include {
return errors.New("-include can not be the same as -ignore")
}
return nil
}
func printList(root Package, paths Graph) {
fmt.Println("Packages:")
for _, pkg := range paths.List(root) {
fmt.Printf(" %s\n", pkg)
}
}
func printDot(root Package, paths Graph) {
fmt.Println(paths.Dot(root))
}