-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
83 lines (71 loc) · 1.69 KB
/
index.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
package gomodmrcli
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
)
type IndexClient struct {
hc *http.Client
}
func NewIndexClient(hc *http.Client) *IndexClient {
return &IndexClient{
hc: hc,
}
}
type Index struct {
Path string `json:"Path"`
Version string `json:"Version"`
Timestamp time.Time `json:"Timestamp"`
}
func (c *IndexClient) Index(since time.Time, limit int, disableModuleFetch bool) ([]Index, error) {
const layout="2006-01-02T15:04:05Z"
if limit < 0 || limit > 2000 {
return nil, errors.New("limit must be between 0 and 2000")
}
// --- build URL
url := "https://index.golang.org/index?"
var queries []string
if !since.IsZero() {
queries = append(queries, fmt.Sprintf("since=%s", since.Format(layout)))
}
if limit != 0 {
queries = append(queries, fmt.Sprintf("limit=%d", limit))
}
url += strings.Join(queries, "&")
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, fmt.Errorf("create a http request to %s: %w", url, err)
}
// --- set Header
if disableModuleFetch {
req.Header.Set("Disable-Module-Fetch", "true")
}
// --- execute http Get
resp, err := c.hc.Do(req)
if err != nil {
return nil, fmt.Errorf("http request")
}
defer resp.Body.Close()
// parse http response
var ret []Index
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
idx, err := parseIndexLine(scanner.Bytes())
if err != nil {
return nil, err
}
ret = append(ret, *idx)
}
return ret, nil
}
func parseIndexLine(line []byte) (*Index, error) {
var idx Index
if err := json.Unmarshal(line, &idx); err != nil {
return nil, fmt.Errorf("parse line as json. line=%s: %w", line, err)
}
return &idx, nil
}