|
1
|
|
|
package action |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/json" |
|
5
|
|
|
"path/filepath" |
|
6
|
|
|
"sort" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/Masterminds/glide/dependency" |
|
9
|
|
|
"github.com/Masterminds/glide/msg" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
// List lists all of the dependencies of the current project. |
|
13
|
|
|
// |
|
14
|
|
|
// Params: |
|
15
|
|
|
// - dir (string): basedir |
|
16
|
|
|
// - deep (bool): whether to do a deep scan or a shallow scan |
|
17
|
|
|
// - format (string): The format to output (text, json, json-pretty) |
|
18
|
|
|
func List(basedir string, deep bool, format string) { |
|
19
|
|
|
basedir, err := filepath.Abs(basedir) |
|
20
|
|
|
if err != nil { |
|
21
|
|
|
msg.Die("Could not read directory: %s", err) |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
r, err := dependency.NewResolver(basedir) |
|
25
|
|
|
if err != nil { |
|
26
|
|
|
msg.Die("Could not create a resolver: %s", err) |
|
27
|
|
|
} |
|
28
|
|
|
h := &dependency.DefaultMissingPackageHandler{Missing: []string{}, Gopath: []string{}, Prefix: "vendor"} |
|
29
|
|
|
r.Handler = h |
|
30
|
|
|
|
|
31
|
|
|
localPkgs, _, err := r.ResolveLocal(deep) |
|
32
|
|
|
if err != nil { |
|
33
|
|
|
msg.Die("Error listing dependencies: %s", err) |
|
34
|
|
|
} |
|
35
|
|
|
sort.Strings(localPkgs) |
|
36
|
|
|
installed := make([]string, len(localPkgs)) |
|
37
|
|
|
for i, pkg := range localPkgs { |
|
38
|
|
|
relPkg, err := filepath.Rel(basedir, pkg) |
|
39
|
|
|
if err != nil { |
|
40
|
|
|
// msg.Warn("Failed to Rel path: %s", err) |
|
41
|
|
|
relPkg = pkg |
|
42
|
|
|
} |
|
43
|
|
|
installed[i] = relPkg |
|
44
|
|
|
} |
|
45
|
|
|
l := PackageList{ |
|
46
|
|
|
Installed: installed, |
|
47
|
|
|
Missing: h.Missing, |
|
48
|
|
|
Gopath: h.Gopath, |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
outputList(l, format) |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// PackageList contains the packages being used by their location |
|
55
|
|
|
type PackageList struct { |
|
56
|
|
|
Installed []string `json:"installed"` |
|
57
|
|
|
Missing []string `json:"missing"` |
|
58
|
|
|
Gopath []string `json:"gopath"` |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
const ( |
|
62
|
|
|
textFormat = "text" |
|
63
|
|
|
jsonFormat = "json" |
|
64
|
|
|
jsonPrettyFormat = "json-pretty" |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
|
|
func outputList(l PackageList, format string) { |
|
68
|
|
|
switch format { |
|
69
|
|
|
case textFormat: |
|
70
|
|
|
msg.Puts("INSTALLED packages:") |
|
71
|
|
|
for _, pkg := range l.Installed { |
|
72
|
|
|
msg.Puts("\t%s", pkg) |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if len(l.Missing) > 0 { |
|
76
|
|
|
msg.Puts("\nMISSING packages:") |
|
77
|
|
|
for _, pkg := range l.Missing { |
|
78
|
|
|
msg.Puts("\t%s", pkg) |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
if len(l.Gopath) > 0 { |
|
82
|
|
|
msg.Puts("\nGOPATH packages:") |
|
83
|
|
|
for _, pkg := range l.Gopath { |
|
84
|
|
|
msg.Puts("\t%s", pkg) |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
case jsonFormat: |
|
88
|
|
|
json.NewEncoder(msg.Default.Stdout).Encode(l) |
|
89
|
|
|
case jsonPrettyFormat: |
|
90
|
|
|
b, err := json.MarshalIndent(l, "", " ") |
|
91
|
|
|
if err != nil { |
|
92
|
|
|
msg.Die("could not unmarshal package list: %s", err) |
|
93
|
|
|
} |
|
94
|
|
|
msg.Puts(string(b)) |
|
95
|
|
|
default: |
|
96
|
|
|
msg.Die("invalid output format: must be one of: json|json-pretty|text") |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|