-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathsearch.go
More file actions
110 lines (94 loc) · 2.41 KB
/
search.go
File metadata and controls
110 lines (94 loc) · 2.41 KB
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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"regexp"
"strings"
)
const (
golangBinariesURL = "https://api.ftp-master.debian.org/binary/by_metadata/Go-Import-Path"
)
type debianPackage struct {
binary string
source string
}
type ftpMasterApiResult struct {
Binary string `json:"binary"`
MetadataValue string `json:"metadata_value"`
Source string `json:"source"`
}
type getGolangBinariesConfig struct {
url string
}
type getGolangBinariesOption func(cfg *getGolangBinariesConfig)
func getGolangBinariesUrl(url string) getGolangBinariesOption {
return func(cfg *getGolangBinariesConfig) {
cfg.url = url
}
}
func getGolangBinaries(opts ...getGolangBinariesOption) (map[string]debianPackage, error) {
cfg := &getGolangBinariesConfig{url: golangBinariesURL}
for _, opt := range opts {
opt(cfg)
}
golangBinaries := make(map[string]debianPackage)
resp, err := http.Get(cfg.url)
if err != nil {
return nil, fmt.Errorf("getting %q: %w", cfg.url, err)
}
defer resp.Body.Close()
if got, want := resp.StatusCode, http.StatusOK; got != want {
return nil, fmt.Errorf("unexpected HTTP status code: got %d, want %d", got, want)
}
var pkgs []ftpMasterApiResult
if err := json.NewDecoder(resp.Body).Decode(&pkgs); err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
for _, pkg := range pkgs {
if !strings.HasSuffix(pkg.Binary, "-dev") {
continue // skip -dbgsym packages etc.
}
for importPath := range strings.SplitSeq(pkg.MetadataValue, ",") {
// XS-Go-Import-Path can be comma-separated and contain spaces.
golangBinaries[strings.TrimSpace(importPath)] = debianPackage{
binary: pkg.Binary,
source: pkg.Source,
}
}
}
return golangBinaries, nil
}
func execSearch(args []string) {
fs := flag.NewFlagSet("search", flag.ExitOnError)
fs.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage: %s search <pattern>
Uses Go's default regexp syntax (https://golang.org/pkg/regexp/syntax/)
Example: %s search 'debi.*'
`, os.Args[0], os.Args[0])
}
err := fs.Parse(args)
if err != nil {
log.Fatal(err)
}
if fs.NArg() != 1 {
fs.Usage()
os.Exit(1)
}
pattern, err := regexp.Compile(fs.Arg(0))
if err != nil {
log.Fatal(err)
}
golangBinaries, err := getGolangBinaries()
if err != nil {
log.Fatal(err)
}
for importPath, pkg := range golangBinaries {
if pattern.MatchString(importPath) {
fmt.Printf("%s: %s\n", pkg.binary, importPath)
}
}
}