-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
407 lines (365 loc) · 12 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package main
import (
"bufio"
"debug/buildinfo"
"errors"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
"sync"
)
// Example: <meta name="go-import" content="fyne.io/fyne/v2 git https://github.com/fyne-io/fyne">
var vanityRegex = regexp.MustCompile(`< *meta name="go-import" content=".+ \w+ (https?://.+)" *\\?>`)
var defaultGetOwnerRepoPair = func(modulePath string) (string, string, error) {
subs := strings.Split(modulePath, "/")
if len(subs) < 3 {
return "", "", fmt.Errorf("couldn't determine owner and repo name in module path '%s'", modulePath)
}
return subs[1], subs[2], nil
}
// For known Git providers, we don't need to check vanity URL redirects.
var knownGitProviders = map[string]Funcs{
"github.com": {
GetOwnerRepoPair: defaultGetOwnerRepoPair,
GetRepoURL: func(owner, repo string) string {
return fmt.Sprintf("https://github.com/%s/%s", owner, repo)
}},
"gitlab.com": {
GetOwnerRepoPair: defaultGetOwnerRepoPair,
GetRepoURL: func(owner, repo string) string {
return fmt.Sprintf("https://gitlab.com/%s/%s", owner, repo)
}},
"bitbucket.org": {
GetOwnerRepoPair: defaultGetOwnerRepoPair,
GetRepoURL: func(owner, repo string) string {
return fmt.Sprintf("https://bitbucket.com/%s/%s", owner, repo)
}},
"sr.ht": {
GetOwnerRepoPair: defaultGetOwnerRepoPair,
GetRepoURL: func(owner, repo string) string {
return fmt.Sprintf("https://sr.ht/%s/%s", owner, repo)
}},
"cs.opensource.google": {
GetOwnerRepoPair: defaultGetOwnerRepoPair,
GetRepoURL: func(owner, repo string) string {
return fmt.Sprintf("https://cs.opensource.google/%s/%s", owner, repo)
}},
"go.googlesource.com": {
// There's no owner/repo separation. E.g. go.googlesource.com/tools
GetOwnerRepoPair: func(modulePath string) (string, string, error) {
subs := strings.Split(modulePath, "/")
if len(subs) < 2 {
return "", "", fmt.Errorf("couldn't determine owner name in module path '%s'", modulePath)
}
return "", subs[1], nil
},
GetRepoURL: func(_, repo string) string {
return fmt.Sprintf("https://go.googlesource.com/%s", repo)
}},
"gitee.com": {
GetOwnerRepoPair: defaultGetOwnerRepoPair,
GetRepoURL: func(owner, repo string) string {
return fmt.Sprintf("https://gitee.com/%s/%s", owner, repo)
}},
"codeberg.org": {
GetOwnerRepoPair: defaultGetOwnerRepoPair,
GetRepoURL: func(owner, repo string) string {
return fmt.Sprintf("https://codeberg.org/%s/%s", owner, repo)
}},
}
type Funcs struct {
// Takes module path, returns owner and repo name
GetOwnerRepoPair func(string) (string, string, error)
// Takes Git owner and repo name, returns repo URL
GetRepoURL func(string, string) string
}
type BinInfo struct {
filename string // Without path, e.g. `arc`/`arc.exe`
// 4 values from `go version -m`
packagePath string // e.g. `github.com/mholt/archiver/v3/cmd/arc`
modulePath string // e.g. `github.com/mholt/archiver/v3`
moduleVersion string // Just the Git tag, not the full version as reported by `go version -m`. E.g. `v3.5.1`
vcsRevision string // e.g. `cc194d2e4af2dc09a812aa0ff61adc4813ea6c69`
repoURL string // URL that can be visited in a browser, after vanity URL resolving. E.g. for binary `arc` installed from `github.com/mholt/archiver/v3/cmd/arc@latest` with v3.5.1 being latest, it's "https://github.com/mholt/archiver". We could make this version-specific, to "https://github.com/mholt/archiver/tree/v3.5.1".
}
var (
wd = flag.Bool("wd", false, "Scan current working directory")
gobin = flag.Bool("gobin", false, `Scan "$GOBIN" directory`)
gopath = flag.Bool("gopath", false, `Scan "$GOPATH/bin" directory`)
)
func main() {
// Disable timestamps in STDERR log output
log.SetFlags(0)
// Precondition: CLI must be called with one argument.
// os.Args always holds the name of the program as the first argument.
if len(os.Args) != 2 {
log.Fatalln("gobin-info requires exactly one argument - either a path to a file/directory, or a flag.")
}
flag.Parse()
// Get path
path, err := getPath()
if err != nil {
log.Fatalln("Couldn't get path:", err)
}
// Scan path (file or dir)
info, err := os.Stat(path)
if err != nil {
log.Fatalln("Couldn't get file/dir info:", err)
}
log.Println("Scanning", path)
var binInfos []BinInfo
if info.IsDir() {
binInfos, err = scanDir(path)
if err != nil {
log.Fatalln("Couldn't scan dir:", err)
}
} else {
binInfo, err := scanFile(path, info)
if err != nil {
log.Fatalln("Couldn't scan file:", err)
}
// scanFile returns (nil, nil) if the file is not an executable
if binInfo != nil {
binInfos = append(binInfos, *binInfo)
}
}
// Sort
sort.Slice(binInfos, func(i, j int) bool {
return binInfos[i].filename < binInfos[j].filename
})
// Print result
printResult(binInfos)
}
func getPath() (string, error) {
var path string
var err error
if *wd {
path, err = os.Getwd()
if err != nil {
return "", fmt.Errorf("couldn't get current working directory: %w", err)
}
} else if *gobin {
path = os.Getenv("GOBIN")
if path == "" {
return "", errors.New("GOBIN environment variable is empty or not set")
}
} else if *gopath {
env := os.Getenv("GOPATH")
if env == "" {
// When the env var is not set, Go's own behavior is to use $HOME/go.
// See https://pkg.go.dev/cmd/go#hdr-GOPATH_environment_variable
log.Println("GOPATH is not set, falling back to $HOME/go like Go does")
env, err = os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("couldn't get user home directory: %w", err)
}
env = filepath.Join(env, "go")
}
// GOPATH can actually be multiple directories, separated by colon on Unix, semicolon on Windows.
// The $GOPATH/bin is always in the *first* of those directories.
// See https://pkg.go.dev/cmd/go#hdr-GOPATH_environment_variable and https://go.dev/doc/code#Command
env = strings.Split(env, string(os.PathListSeparator))[0]
path = filepath.Join(env, "bin")
} else {
// Can be path to a file or directory
path = os.Args[1]
}
return path, nil
}
// scanDir scans a directory for executables to run scanFile on.
func scanDir(path string) ([]BinInfo, error) {
var binInfos []BinInfo
entries, err := os.ReadDir(path)
if err != nil {
return nil, err
}
wg := sync.WaitGroup{}
resChan := make(chan BinInfo, len(entries))
errChan := make(chan error, len(entries))
for _, entry := range entries {
wg.Add(1)
go func(entry fs.DirEntry) {
defer wg.Done()
// Skip entries that are neither regular files (e.g. directories) nor symlinks
if !(entry.Type().IsRegular() || entry.Type()&fs.ModeSymlink != 0) {
return
}
info, err := entry.Info()
if err != nil {
errChan <- err
return
}
binInfo, err := scanFile(filepath.Join(path, info.Name()), info)
if err != nil {
errChan <- err
return
}
// scanFile returns (nil, nil) if the file is not an executable
if binInfo == nil {
return
}
resChan <- *binInfo
}(entry)
}
wg.Wait()
// Only returns if there's a value in the channel, otherwise continues.
select {
case err := <-errChan:
return nil, err
default:
}
// Usually the sender should close, but with the WaitGroup
// we know that there will be no more writes to the channel,
// and it allows us to conveniently range over it.
close(resChan)
for binInfo := range resChan {
binInfos = append(binInfos, binInfo)
}
return binInfos, nil
}
// scanFile scans file to try to return the Go binary info.
// If the file is not a Go binary, scanFile returns (nil, nil).
func scanFile(path string, info fs.FileInfo) (*BinInfo, error) {
if info.Mode()&fs.ModeSymlink != 0 {
// Accept file symlinks only.
i, err := os.Stat(path)
if err != nil || !i.Mode().IsRegular() {
return nil, err
}
info = i
}
if !isExe(path, info) {
return nil, nil
}
bi, err := buildinfo.ReadFile(path)
if err != nil {
return nil, err
}
binInfo := BinInfo{
filename: filepath.Base(path),
packagePath: bi.Path,
modulePath: bi.Main.Path,
moduleVersion: bi.Main.Version, // Most binaries have the proper mod info, but github.com/magefile/mage for example doesn't. It's installed via their bootstrap tool, and the mod info is just "(devel)"
vcsRevision: "?",
repoURL: "?",
}
// Add revision and derived URL
// Look for the revision and set if found
for _, biSetting := range bi.Settings {
if biSetting.Key == "vcs.revision" {
binInfo.vcsRevision = biSetting.Value
break
}
}
// Derive URL, potentially from vanity URL
gitProvider := strings.Split(binInfo.modulePath, "/")[0]
if funcs, ok := knownGitProviders[gitProvider]; !ok {
// Provider not known; assume it's a vanity URL
resolvedURL := resolveVanityURL(binInfo.packagePath, gitProvider)
if resolvedURL == "" {
// It wasn't a vanity URL. Probably unknown Git provider.
binInfo.repoURL = fallbackURL(binInfo.modulePath)
} else {
// We assume that the resolved URL is prefixed with a protocol
if strings.HasPrefix(resolvedURL, "http") {
_, resolvedURL, ok = strings.Cut(resolvedURL, "//") // trim protocol
if !ok {
return nil, fmt.Errorf("couldn't trim protocol from resolved URL %q", resolvedURL)
}
}
// Resolved URL might be known or unknown Git provider.
gitProvider = strings.Split(resolvedURL, "/")[0]
if funcs, ok := knownGitProviders[gitProvider]; !ok {
// Unknown Git provider
binInfo.repoURL = fallbackURL(resolvedURL)
} else {
owner, repo, err := funcs.GetOwnerRepoPair(resolvedURL)
if err != nil {
return nil, err
}
binInfo.repoURL = funcs.GetRepoURL(owner, repo)
}
}
} else {
// Known Git provider
owner, repo, err := funcs.GetOwnerRepoPair(binInfo.modulePath)
if err != nil {
return nil, err
}
binInfo.repoURL = funcs.GetRepoURL(owner, repo)
}
return &binInfo, nil
}
// isExe reports whether the file should be considered executable.
func isExe(file string, info fs.FileInfo) bool {
if runtime.GOOS == "windows" {
return strings.HasSuffix(strings.ToLower(file), ".exe")
}
return info.Mode().IsRegular() && info.Mode()&0111 != 0
}
// TODO: Check if we can reuse the code Go uses for this:
// https://github.com/golang/go/blob/1102616/src/cmd/go/internal/get/discovery.go#L31
func resolveVanityURL(packagePath, gitProvider string) string {
// We check the package path instead of module path because it's what's registered at the vanity URL service (the go install command calls the package URL)
res, err := http.Get("https://" + packagePath)
if err != nil {
return ""
}
defer res.Body.Close()
scanner := bufio.NewScanner(res.Body)
for ok := scanner.Scan(); ok; ok = scanner.Scan() {
line := scanner.Text()
match := vanityRegex.FindStringSubmatch(line)
if match == nil {
// Relevant meta tags are defined inside the head, so if we're already at the end of head we can stop.
if strings.Contains(line, "</head>") {
return ""
}
// Otherwise continue to next line
continue
}
// We found a match. The redirect is defined as capture group in the regex.
redir := match[1]
_, err := url.Parse(redir)
if err != nil {
return ""
}
return redir
}
return ""
}
func fallbackURL(modulePath string) string {
// In our known examples the URLs are always The host and then owner and repo as separate path elements.
// Let's apply this, but warn that it might be wrong
subs := strings.Split(modulePath, "/")
if len(subs) < 3 {
return "https://" + modulePath + " (❓)"
}
return "https://" + subs[0] + "/" + subs[1] + "/" + subs[2] + " (❓)"
}
func printResult(binInfos []BinInfo) {
var maxFileNameLen int
var maxVersionLen int
for _, binInfo := range binInfos {
if len(binInfo.filename) > maxFileNameLen {
maxFileNameLen = len(binInfo.filename)
}
if len(binInfo.moduleVersion) > maxVersionLen {
maxVersionLen = len(binInfo.moduleVersion)
}
}
for _, binInfo := range binInfos {
filenameWithPadding := binInfo.filename + strings.Repeat(" ", maxFileNameLen-len(binInfo.filename))
versionWithPadding := binInfo.moduleVersion + strings.Repeat(" ", maxVersionLen-len(binInfo.moduleVersion))
fmt.Printf("%s %s %s\n", filenameWithPadding, versionWithPadding, binInfo.repoURL)
}
}