-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
291 lines (248 loc) · 6.67 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"regexp"
"sort"
"strings"
"github.com/google/go-github/v32/github"
"github.com/pmatseykanets/gh-tools/auth"
gh "github.com/pmatseykanets/gh-tools/github"
"github.com/pmatseykanets/gh-tools/terminal"
"github.com/pmatseykanets/gh-tools/version"
"golang.org/x/mod/modfile"
"golang.org/x/oauth2"
)
func usage() {
usage := `Find reverse Go dependencies across GitHub repositories
Usage: gh-go-rdeps [flags] <owner> <path>
owner Repository owner (user or organization)
path Module/package path
Flags:
-help Print this information and exit
-no-repo= The pattern to reject repository names
-repo= The pattern to match repository names
-token Prompt for an Access Token
-version Print the version and exit
`
fmt.Println(usage)
}
func main() {
if err := run(context.Background()); err != nil {
fmt.Printf("error: %s\n", err)
os.Exit(1)
}
}
type config struct {
owner string
modpath string
repoRegexp *regexp.Regexp
token bool // Propmt for an access token.
noRepoRegexp *regexp.Regexp // The pattern to reject repository names.
}
type finder struct {
gh *github.Client
config config
stdout io.WriteCloser
stderr io.WriteCloser
}
func readConfig() (config, error) {
if len(os.Args) == 0 {
usage()
os.Exit(1)
}
config := config{}
var (
showVersion, showHelp bool
repo, noRepo string
err error
)
flag.BoolVar(&showHelp, "help", showHelp, "Print this information and exit")
flag.StringVar(&noRepo, "no-repo", "", "The pattern to reject repository names")
flag.StringVar(&repo, "repo", "", "The pattern to match repository names")
flag.BoolVar(&config.token, "token", config.token, "Prompt for Access Token")
flag.BoolVar(&showVersion, "version", showVersion, "Print version and exit")
flag.Usage = usage
flag.Parse()
if showHelp {
usage()
os.Exit(0)
}
if showVersion {
fmt.Printf("gh-go-rdeps version %s\n", version.Version)
os.Exit(0)
}
if flag.NArg() < 1 {
return config, fmt.Errorf("owner is required")
}
config.owner = strings.TrimSpace(flag.Arg(0))
if config.owner == "" {
return config, fmt.Errorf("owner can't be empty")
}
if flag.NArg() < 2 {
return config, fmt.Errorf("mod path is required")
}
config.modpath = strings.TrimSpace(flag.Arg(1))
if config.modpath == "" {
return config, fmt.Errorf("mod path can't be empty")
}
if repo != "" {
config.repoRegexp, err = regexp.Compile(repo)
if err != nil {
return config, fmt.Errorf("invalid repo pattern: %s: %s", repo, err)
}
}
if noRepo != "" {
if config.noRepoRegexp, err = regexp.Compile(noRepo); err != nil {
return config, fmt.Errorf("invalid no-repo pattern: %s", err)
}
}
return config, nil
}
func run(ctx context.Context) error {
var err error
finder := &finder{
stdout: os.Stdout,
stderr: os.Stderr,
}
finder.config, err = readConfig()
if err != nil {
return err
}
var token string
if finder.config.token {
token, _ = terminal.PasswordPrompt("Access Token: ")
} else {
token = auth.GetToken()
}
if token == "" {
return fmt.Errorf("access token is required")
}
finder.gh = github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)))
return finder.find(ctx)
}
func (f *finder) find(ctx context.Context) error {
repos, err := gh.NewRepoFinder(f.gh).Find(ctx, gh.RepoFilter{
Owner: f.config.owner,
RepoRegexp: f.config.repoRegexp,
NoRepoRegexp: f.config.noRepoRegexp,
})
if err != nil {
return err
}
var (
repo *github.Repository
goRepo bool
contents []byte
mod *modfile.File
require *modfile.Require
replace *modfile.Replace
gopkg *Gopkg
gopkgProject GopkgProject
dependencies []string
)
nextRepo:
for _, repo = range repos {
goRepo, err = f.goRepo(ctx, repo)
if err != nil {
return err
}
if !goRepo {
continue
}
// go modules take precedence.
contents, err = f.getFileContents(ctx, repo, "go.mod")
if err != nil {
return err
}
if len(contents) > 0 {
mod, err = modfile.Parse("go.mod", contents, nil)
if err != nil {
return err
}
for _, require = range mod.Require {
if strings.HasPrefix(require.Mod.Path, f.config.modpath) {
dependencies = append(dependencies, mod.Module.Mod.Path)
continue nextRepo
}
}
for _, replace = range mod.Replace {
if strings.HasPrefix(replace.Old.Path, f.config.modpath) ||
strings.HasPrefix(replace.New.Path, f.config.modpath) {
dependencies = append(dependencies, mod.Module.Mod.Path)
continue nextRepo
}
}
continue nextRepo
}
// Gopkg.toml.
contents, err = f.getFileContents(ctx, repo, "Gopkg.toml")
if err != nil {
return err
}
if len(contents) == 0 {
continue nextRepo
}
gopkg, err = parseGopkg(bytes.NewReader(contents))
if err != nil {
return err
}
for _, gopkgProject = range gopkg.Constraints {
if strings.HasPrefix(gopkgProject.Name, f.config.modpath) ||
strings.HasPrefix(gopkgProject.Source, f.config.modpath) {
dependencies = append(dependencies, fmt.Sprintf("github.com/%s/%s", f.config.owner, repo.GetName()))
continue nextRepo
}
}
for _, gopkgProject = range gopkg.Overrides {
if strings.HasPrefix(gopkgProject.Name, f.config.modpath) ||
strings.HasPrefix(gopkgProject.Source, f.config.modpath) {
dependencies = append(dependencies, fmt.Sprintf("github.com/%s/%s", f.config.owner, repo.GetName()))
continue nextRepo
}
}
}
sort.Strings(dependencies)
for _, dependency := range dependencies {
fmt.Fprintln(f.stdout, dependency)
}
return nil
}
func (f *finder) getFileContents(ctx context.Context, repo *github.Repository, filename string) ([]byte, error) {
fileContents, _, resp, err := f.gh.Repositories.GetContents(ctx, f.config.owner, repo.GetName(), filename, nil)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
return nil, nil
}
return nil, err
}
contents, err := fileContents.GetContent()
if err != nil {
return nil, err
}
return []byte(contents), nil
}
func (f *finder) goRepo(ctx context.Context, repo *github.Repository) (bool, error) {
tree, resp, err := f.gh.Git.GetTree(ctx, f.config.owner, *repo.Name, "master", true)
if err != nil {
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusConflict {
return false, nil
}
return false, err
}
for _, entry := range tree.Entries {
if strings.HasSuffix(entry.GetPath(), ".go") ||
strings.HasSuffix(entry.GetPath(), "Gopkg.toml") ||
strings.HasSuffix(entry.GetPath(), "go.mod") {
return true, nil
}
}
return false, nil
}