-
Notifications
You must be signed in to change notification settings - Fork 24
/
goproxy.go
87 lines (77 loc) · 1.78 KB
/
goproxy.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
package goproxy
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/ossf/package-feeds/feeds"
)
const (
FeedName = "goproxy"
)
var (
baseURL = "https://index.golang.org/index"
httpClient = &http.Client{Timeout: 10 * time.Second}
)
type PackageJSON struct {
Path string `json:"Path"`
Version string `json:"Version"`
Timestamp string `json:"Timestamp"`
}
type Package struct {
Title string
ModifiedDate time.Time
Version string
}
func fetchPackages(since time.Time) ([]Package, error) {
var packages []Package
params := url.Values{}
params.Add("since", since.Format(time.RFC3339))
requestURL := fmt.Sprintf("%s?%s", baseURL, params.Encode())
resp, err := httpClient.Get(requestURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
var packageJSON PackageJSON
err = json.Unmarshal([]byte(scanner.Text()), &packageJSON)
if err != nil {
return nil, err
}
modifiedDate, err := time.Parse(time.RFC3339, packageJSON.Timestamp)
if err != nil {
return nil, err
}
pkg := Package{
Title: packageJSON.Path,
ModifiedDate: modifiedDate,
Version: packageJSON.Version,
}
packages = append(packages, pkg)
}
err = scanner.Err()
if err != nil {
return nil, err
}
return packages, nil
}
type Feed struct{}
func (feed Feed) Latest(cutoff time.Time) ([]*feeds.Package, error) {
pkgs := []*feeds.Package{}
packages, err := fetchPackages(cutoff)
if err != nil {
return pkgs, fmt.Errorf("error fetching packages: %w", err)
}
for _, pkg := range packages {
pkg, err := feeds.NewPackage(pkg.ModifiedDate, cutoff, pkg.Title, pkg.Version, FeedName)
if err != nil {
continue
}
pkgs = append(pkgs, pkg)
}
return pkgs, nil
}