forked from ribice/glice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (233 loc) · 6.1 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"bufio"
"context"
"encoding/base64"
"flag"
"go/build"
"log"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/ribice/glice/api"
)
const (
nl = "\n"
fs = string(filepath.Separator)
)
type dep struct {
name string
count int
repo *api.Repository
}
type deps struct {
deps []dep
baseDir string
depth string
bdl int
incstdlib bool
verbose bool
count bool
fw bool
cl *api.GitClient
}
func main() {
var (
verbose = flag.Bool("v", false, "Include detailed imports (github.com/author/repo/net/http, github.com/author/repo/net/middleware ... instead of only github.com/author/repo")
incStdLib = flag.Bool("s", false, "Include standard library dependencies")
recursive = flag.Bool("r", false, "Gets single level recursive dependencies")
fileWrite = flag.Bool("f", false, "Writes all licenses to files")
ignoreDirs = flag.String("i", "", "Comma separated list of folders that should be ignored")
ghkey = flag.String("gh", "", "GitHub API key used for increasing the GitHub's API rate limit from 60req/h to 5000req/h")
path = flag.String("p", "", `Path of desired directory to be scanned with Glice (e.g. "github.com/ribice/glice/")`)
thx = flag.Bool("t", false, "Stars dependent repos.")
count = flag.Bool("c", false, "Include usage count in exported result")
depth = "Imports"
apiKeys = map[string]string{}
)
flag.Parse()
fullPath := getCurrentFolder(*path)
basedir := strings.Split(fullPath, "src"+fs)[1]
bdl := len(basedir) - 1
if *recursive {
depth = "Deps"
}
ds := deps{
baseDir: basedir,
bdl: bdl,
incstdlib: *incStdLib,
verbose: *verbose,
count: *count,
fw: *fileWrite,
depth: depth,
}
for _, v := range getFolders(fullPath, *ignoreDirs) {
ds.getDeps(v)
}
if *ghkey != "" {
apiKeys["github.com"] = *ghkey
}
c := context.Background()
ds.cl = api.NewGitClient(c, apiKeys, *thx)
ds.getLicenses(c)
tw := tablewriter.NewWriter(os.Stdout)
ds.writeStd(tw)
tw.Render()
checkErr(ds.writeLicensesToFile(fullPath))
}
func getCurrentFolder(path string) string {
if path != "" {
if !strings.HasSuffix(path, fs) {
path += fs
}
return build.Default.GOPATH + fs + "src" + fs + path
}
cf, err := os.Getwd()
checkErr(err)
return cf + fs
}
func getFolders(fullPath, ignore string) []string {
ign := strings.Split(ignore, ",")
var folders []string
checkErr(filepath.Walk(fullPath+".", func(path string, info os.FileInfo, err error) error {
// Return only folders
if info.IsDir() {
// Skip if folder name is vendor, is hidden (starting with dot, but ignore dot only)
if (info.Name() == "vendor" || skipHidden(info.Name())) && info.Name() != "." {
return filepath.SkipDir
}
for _, v := range ign {
if info.Name() == v {
return filepath.SkipDir
}
}
folders = append(folders, strings.Split(path, fullPath)[1])
}
return nil
}))
return folders
}
func skipHidden(name string) bool {
for _, v := range strings.Split(name, fs) {
if strings.HasPrefix(v, ".") {
return true
}
}
return false
}
func (ds *deps) getDeps(dirname string) {
// used for comparing dependency with current project minus the file separator
if dirname == "."+fs {
dirname = ""
}
args := "go list -f" + ` '{{ .` + ds.depth + ` }}' ` + ds.baseDir + dirname + ` | tr "[" " " | tr "]" " " | xargs go list -f '{{if not .Standard}}{{.ImportPath}}{{end}}' `
if ds.incstdlib {
args = "go list -f '{{ join ." + ds.depth + ` "\n"}}` + "' " + ds.baseDir + dirname
}
cmd := exec.Command("bash", "-c", args)
stdout, err := cmd.StdoutPipe()
checkErr(err)
checkErr(cmd.Start())
s := bufio.NewScanner(stdout)
for s.Scan() {
if d := ds.exists(s.Text(), ds.verbose); d != nil {
if len(d.name) >= ds.bdl && d.name[0:ds.bdl]+fs == ds.baseDir {
continue
}
ds.deps = append(ds.deps, *d)
}
}
}
func (ds *deps) exists(s string, verbose bool) *dep {
if strings.Contains(s, "vendor"+fs) {
s = strings.Split(s, "vendor"+fs)[1]
}
l := getRepoURL(&s, verbose)
for i, v := range ds.deps {
if v.name == s {
ds.deps[i].count++
return nil
}
if v.repo != nil && v.repo.URL == l.URL {
l.Exists = true
}
}
return &dep{name: s, repo: l}
}
func getRepoURL(s *string, verbose bool) *api.Repository {
spl := strings.Split(*s, fs)
switch spl[0] {
case "github.com", "gitlab.com", "bitbucket.org":
if len(spl) < 3 {
return &api.Repository{}
}
if !verbose {
*s = filepath.Join(spl[0], spl[1], spl[2])
}
return &api.Repository{URL: "https://" + spl[0] + "/" + spl[1] + "/" + spl[2], Host: spl[0], Author: spl[1], Project: spl[2]}
case "gopkg.in":
if len(spl) < 3 {
return &api.Repository{}
}
if !verbose {
*s = filepath.Join(spl[0], spl[1], spl[2])
}
return &api.Repository{URL: "https://github.com/" + spl[1] + "/" + strings.Split(spl[2], ".")[0], Host: "github.com", Author: spl[1], Project: strings.Split(spl[2], ".")[0]}
}
return getOtherRepo(s, verbose)
}
func (ds *deps) getLicenses(c context.Context) {
for _, v := range ds.deps {
v.repo.GetLicenses(c, ds.cl)
}
}
func (ds *deps) writeStd(tw *tablewriter.Table) {
keys := []string{"Dependency", "RepoURL", "License"}
if ds.count {
keys = append(keys, "Count")
}
tw.SetHeader(keys)
for _, v := range ds.deps {
vals := []string{v.name, color.BlueString(v.repo.URL), v.repo.Shortname}
if ds.count {
vals = append(vals, strconv.Itoa(v.count+1))
}
tw.Append(vals)
}
}
func (ds *deps) writeLicensesToFile(path string) error {
if !ds.fw {
return nil
}
os.Mkdir("licenses", 0777)
for _, v := range ds.deps {
if v.repo.Text == "" {
continue
}
dec, err := base64.StdEncoding.DecodeString(v.repo.Text)
if err != nil {
return err
}
f, err := os.Create(path + fs + v.repo.Author + "-" + v.repo.Project + "-license.MD")
if err != nil {
return err
}
defer f.Close()
if _, err := f.Write(dec); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
}
return nil
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}