-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdeplist.go
92 lines (81 loc) · 1.7 KB
/
deplist.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
package main
import (
"flag"
"fmt"
"log"
"os"
"sort"
"golang.org/x/tools/go/packages"
)
func usage() {
fmt.Printf(`Usage:
%s [flags] [pkg]
where pkg is the name of a Go package (e.g., github.com/cespare/deplist).
If no package name is given, the current directory is used.
Flags:
`, os.Args[0])
flag.PrintDefaults()
}
func main() {
log.SetFlags(0)
testDeps := flag.Bool("t", false, "Include test dependencies")
std := flag.Bool("std", false, "Include standard library dependencies")
flag.Usage = usage
flag.Parse()
pkg := "."
switch flag.NArg() {
case 1:
pkg = flag.Arg(0)
case 0:
default:
usage()
os.Exit(1)
}
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedImports | packages.NeedDeps,
Tests: *testDeps,
}
pkgs, err := packages.Load(cfg, pkg)
if err != nil {
log.Fatalln("Error loading packages:", err)
}
if packages.PrintErrors(pkgs) > 0 {
os.Exit(1)
}
depSet := make(map[string]struct{})
pre := func(pkg *packages.Package) bool {
depSet[pkg.PkgPath] = struct{}{}
return true
}
packages.Visit(pkgs, pre, nil)
if !*std {
cfg.Tests = false
stdPkgs, err := packages.Load(cfg, "std")
if err != nil {
log.Fatalln("Error discovering packages:", err)
}
if packages.PrintErrors(pkgs) > 0 {
os.Exit(1)
}
stdSet := make(map[string]struct{})
pre := func(pkg *packages.Package) bool {
stdSet[pkg.PkgPath] = struct{}{}
return true
}
packages.Visit(stdPkgs, pre, nil)
for pkg := range stdSet {
delete(depSet, pkg)
}
}
for _, pkg := range pkgs {
delete(depSet, pkg.PkgPath)
}
var deps []string
for dep := range depSet {
deps = append(deps, dep)
}
sort.Strings(deps)
for _, dep := range deps {
fmt.Println(dep)
}
}