internal/module/module.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
package module import ( "context" "encoding/json" "fmt" "io" "net/http" "os" "path" "strings" "golang.org/x/mod/module" "golang.org/x/sync/errgroup" ) type latestResp struct { Version string `json:"Version"` } type resolvedModule struct { module string version string goMod []byte } func proxyRequest(importPath, suffix string) (*http.Request, error) { proxyBase := "https://proxy.golang.org" if gp := os.Getenv("GOPROXY"); gp != "" { if strings.ContainsRune(gp, ',') || strings.ContainsRune(gp, '|') || gp == "off" || gp == "direct" { return nil, fmt.Errorf("only GOPROXY=<url> is supported by gore, not %q", gp) } proxyBase = strings.TrimSuffix(gp, "/") } escapedSuffix, err := module.EscapeVersion(suffix) if err != nil { return nil, err } if escapedSuffix != "@latest" { escapedSuffix = "@v/" + escapedSuffix } escapedImportPath, err := module.EscapePath(importPath) if err != nil { return nil, err } req, err := http.NewRequest("GET", proxyBase+"/"+escapedImportPath+"/"+escapedSuffix, nil) if err != nil { return nil, err } // TODO: dynamic version here pls req.Header.Set("User-Agent", "gore runs everything alpha") return req, nil } func moduleInfo(ctx context.Context, importPath, version string) (*latestResp, error) { suffix := version + ".info" if version == "latest" { suffix = "@latest" } req, err := proxyRequest(importPath, suffix) if err != nil { return nil, err } req.Header.Set("Accept", "application/json") resp, err := http.DefaultClient.Do(req.WithContext(ctx)) if err != nil { return nil, err } defer func() { io.ReadAll(resp.Body) resp.Body.Close() }() if resp.StatusCode == http.StatusNotFound { return nil, nil } if got, want := resp.StatusCode, http.StatusOK; got != want { return nil, fmt.Errorf("unexpected HTTP status: got %v, want %v", resp.Status, want) } var latest latestResp b, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("reading HTTP response: %v", err) } if err := json.Unmarshal(b, &latest); err != nil { return nil, fmt.Errorf("decoding /@latest response: %v", err) } return &latest, nil } func resolveGoMod(ctx context.Context, importPath string, latest *latestResp) (*resolvedModule, error) { req, err := proxyRequest(importPath, latest.Version+".mod") if err != nil { return nil, err } resp, err := http.DefaultClient.Do(req.WithContext(ctx)) if err != nil { return nil, err } defer func() { io.ReadAll(resp.Body) resp.Body.Close() }() if got, want := resp.StatusCode, http.StatusOK; got != want { return nil, fmt.Errorf("unexpected HTTP status: got %v, want %v", resp.Status, want) } b, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("reading HTTP response: %v", err) } return &resolvedModule{ module: importPath, version: latest.Version, goMod: b, }, nil } func Resolve(ctx context.Context, importPath, version string) (*resolvedModule, error) { eg, latestctx := errgroup.WithContext(ctx) parts := strings.Split(path.Clean(importPath), "/") resps := make([]*latestResp, len(parts)) for idx := len(parts); idx > 0; idx-- { idx := idx // copy importPath := strings.Join(parts[:idx], "/") eg.Go(func() error { if importPath == "github.com" { // Short-circuit: github.com is not a Go module :) return nil } if strings.HasPrefix(importPath, "github.com/") && !strings.ContainsRune(strings.TrimPrefix(importPath, "github.com/"), '/') { // Short-circuit: github.com/<something> references an // organisation or user, not a repository. return nil } resp, err := moduleInfo(latestctx, importPath, version) if err != nil { return err } resps[idx-1] = resp return nil }) } if err := eg.Wait(); err != nil { return nil, err } for idx := len(parts); idx > 0; idx-- { importPath := strings.Join(parts[:idx], "/") resp := resps[idx-1] if resp == nil { continue } return resolveGoMod(ctx, importPath, resp) } return nil, fmt.Errorf("could not resolve import path %q to any Go module", importPath) }