/
gopakgen.go
194 lines (166 loc) · 4.75 KB
/
gopakgen.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
186
187
188
189
190
191
192
193
194
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"golang.org/x/exp/slices"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"golang.org/x/tools/go/vcs"
)
func proxyURL(path, endpoint string) string {
// TODO: Respect $GOPROXY?
return fmt.Sprintf("https://proxy.golang.org/%v/%v", path, endpoint)
}
func fetch(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("create request: %w", err)
}
rsp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("perform request: %w", err)
}
defer rsp.Body.Close()
data, err := io.ReadAll(rsp.Body)
if err != nil {
return data, fmt.Errorf("read response: %w", err)
}
return data, nil
}
func latest(ctx context.Context, module string) (string, error) {
raw, err := fetch(ctx, proxyURL(module, "@latest"))
if err != nil {
return "", fmt.Errorf("fetch: %w", err)
}
var data struct{ Version string }
err = json.Unmarshal(raw, &data)
if err != nil {
return "", fmt.Errorf("parse response: %w", err)
}
return data.Version, nil
}
func mod(ctx context.Context, path, version string) (*modfile.File, error) {
data, err := fetch(ctx, proxyURL(path, "@v/"+version+".mod"))
if err != nil {
return nil, fmt.Errorf("fetch: %w", err)
}
file, err := modfile.Parse(path+"@"+version+".mod", data, nil)
if err != nil {
return nil, fmt.Errorf("parse: %w", err)
}
return file, nil
}
type Source struct {
Type string `json:"type"`
URL string `json:"url"`
Tag string `json:"tag,omitempty"`
Commit string `json:"commit,omitempty"`
Dest string `json:"dest"`
DisableFsckObjects bool `json:"disable-fsckobjects,omitempty"`
DisableShallowClone bool `json:"disable-shallow-clone,omitempty"`
DisableSubmodules bool `json:"disable-submodules,omitempty"`
}
func source(path, version string) (Source, error) {
rr, err := vcs.RepoRootForImportPath(path, false)
if err != nil {
return Source{}, fmt.Errorf("get repo root: %w", err)
}
tag, commit := version, ""
if module.IsPseudoVersion(version) {
rev, err := module.PseudoVersionRev(version)
if err != nil {
return Source{}, fmt.Errorf("get pseudo-version revision for %q: %w", path+"@"+version, err)
}
tag, commit = "", rev
}
tag, incompatible := strings.CutSuffix(tag, "+incompatible")
major := "/" + semver.Major(version)
if incompatible || (major == "/v0") || (major == "/v1") {
major = ""
}
if tag != "" {
subdir := strings.TrimPrefix(path, rr.Root)
subdir = strings.TrimSuffix(subdir, major)
subdir = strings.TrimPrefix(subdir, "/")
if subdir != "" {
tag = subdir + "/" + tag
}
}
return Source{
Type: rr.VCS.Cmd,
URL: rr.Repo,
Tag: tag,
Commit: commit,
Dest: filepath.Join("vendor", rr.Root, major),
}, nil
}
func run(ctx context.Context) error {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %v [options] <module path>\n", os.Args[0])
fmt.Fprintln(os.Stderr, "Options:")
flag.PrintDefaults()
}
disableFsckObjects := flag.Bool("disable-fsckobjects", false, "disable cloning of fsck objects from dependencies")
disableShallowClone := flag.Bool("disable-shallow-clone", false, "disable shallow clones of dependencies")
disableSubmodules := flag.Bool("disable-submodules", false, "disable submodules of dependencies")
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(2)
}
base := flag.Arg(0)
path, version, ok := strings.Cut(base, "@")
if !ok || (version == "latest") {
var err error
version, err = latest(ctx, path)
if err != nil {
return fmt.Errorf("get latest version of module: %w", err)
}
}
mod, err := mod(ctx, path, version)
if err != nil {
return fmt.Errorf("get modfile: %w", err)
}
out, err := asyncMap(ctx, mod.Require, func(req *modfile.Require) (Source, error) {
s, err := source(req.Mod.Path, req.Mod.Version)
if err != nil {
return s, fmt.Errorf("generate source for %q: %w", req.Mod.String(), err)
}
switch s.Type {
case "git":
s.DisableFsckObjects = *disableFsckObjects
s.DisableShallowClone = *disableShallowClone
s.DisableSubmodules = *disableSubmodules
}
return s, nil
})
if err != nil {
return err
}
slices.SortFunc(out, func(s1, s2 Source) bool { return s1.Dest < s2.Dest })
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
err = enc.Encode(out)
if err != nil {
return fmt.Errorf("encode output: %w", err)
}
return nil
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
err := run(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}