/
main.go
185 lines (159 loc) · 3.95 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
// Copyright 2013 Frederik Zipp. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Gofind searches for Go packages via pkg.go.dev.
//
// Usage:
//
// gofind [-a] query ...
//
// Flags:
//
// -a load all search results if set, not just the first 10 results
//
// Examples:
//
// gofind logging
// gofind -a logging
// gofind go cloud # Search for multiple terms
// gofind "go cloud" # Search for an exact match
// gofind yaml OR json # Combine searches
package main
import (
"flag"
"fmt"
"go/doc"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
)
func usage() {
_, _ = fmt.Fprint(os.Stderr, usageDoc)
os.Exit(2)
}
const usageDoc = `Find Go modules via pkg.go.dev.
Usage:
gofind [-a] query ...
Flags:
-a load all search results if set, not just the first 10 results
Examples:
gofind logging
gofind -a logging
gofind go cloud # Search for multiple terms
gofind "go cloud" # Search for an exact match
gofind yaml OR json # Combine searches
`
func main() {
allFlag := flag.Bool("a", false, "load all search results, not just the first 10 results")
flag.Usage = usage
flag.Parse()
if flag.NArg() == 0 {
usage()
}
args := flag.Args()
for i, arg := range args {
if strings.Contains(arg, " ") {
args[i] = `"` + arg + `"`
}
}
query := strings.Join(args, " ")
run(query, *allFlag)
}
func run(query string, all bool) {
modules, err := search(query, all)
check(err)
for _, mod := range modules {
check(mod.writeTo(os.Stdout))
}
}
func search(query string, all bool) ([]searchResult, error) {
limit := 10
if all {
limit = 100
}
return searchLimited(query, limit)
}
func searchLimited(query string, limit int) ([]searchResult, error) {
v := url.Values{}
v.Set("q", query)
v.Set("limit", strconv.Itoa(limit))
req, err := http.NewRequest("GET", "https://pkg.go.dev/search?"+v.Encode(), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "text/html")
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("HTTP status code error: %d %sr", res.StatusCode, res.Status)
}
htmlDoc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
return scrapeSearchResults(htmlDoc)
}
func scrapeSearchResults(htmlDoc *goquery.Document) (sr []searchResult, err error) {
htmlDoc.Find(".SearchSnippet").Each(func(i int, s *goquery.Selection) {
header := s.Find(".SearchSnippet-headerContainer").Text()
moduleName := strings.TrimSpace(header[:strings.Index(header, "(")])
modulePath := trimParens(strings.TrimSpace(s.Find(".SearchSnippet-header-path").Text()))
synopsis := strings.TrimSpace(s.Find(".SearchSnippet-synopsis").Text())
info := formatInfo(s.Find(".SearchSnippet-infoLabel").Text())
sr = append(sr, searchResult{
moduleName: moduleName,
modulePath: modulePath,
synopsis: synopsis,
info: info,
})
})
return sr, err
}
func trimParens(s string) string {
return strings.TrimSuffix(strings.TrimPrefix(s, "("), ")")
}
func formatInfo(info string) string {
var parts []string
for _, p := range strings.Split(info, "|") {
parts = append(parts, strings.TrimSpace(p))
}
return strings.Join(parts, " | ")
}
type searchResult struct {
moduleName string
modulePath string
synopsis string
info string
}
const (
punchCardWidth = 80
indent = " "
)
func (s searchResult) writeTo(w io.Writer) error {
_, err := fmt.Fprintf(w, "%s (%s)\n", s.moduleName, s.modulePath)
if err != nil {
return err
}
if s.synopsis != "" {
doc.ToText(w, s.synopsis, indent, "", punchCardWidth-2*len(indent))
}
_, err = fmt.Fprintf(w, "\n%s%s\n\n", indent, s.info)
return err
}
func check(err error) {
if err != nil {
fail(err)
}
}
func fail(msg ...any) {
_, _ = fmt.Fprintln(os.Stderr, msg...)
os.Exit(1)
}