-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
239 lines (211 loc) · 5.37 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// go-latest tries to upgrade programs go install-d to GOBIN.
//
// For reference, go itself:
// ./src/cmd/go/internal/version/version.go
// ./src/cmd/go/internal/work/build.go
package main
import (
"bytes"
"context"
"debug/buildinfo"
"encoding/json"
"errors"
"flag"
"fmt"
"io/fs"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"runtime/debug"
"golang.org/x/mod/semver"
"golang.org/x/sync/errgroup"
)
// NOTES:
// golang.org/x/mod/semver
// golang.org/x/mod/module
//
// Use:
// go list -m -json golang.org/x/tools/gopls@latest
// either on each pkg or on the module.
func gobin() string {
gobin := os.Getenv("GOBIN")
if gobin != "" {
return gobin
}
home := os.Getenv("HOME")
if home != "" {
return filepath.Join(home, "go", "bin")
}
return ""
}
func goversion(ctx context.Context) (string, error) {
cmd := exec.CommandContext(ctx, "go", "env", "GOVERSION")
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("go env (%w):\n%s", err, out)
}
return string(bytes.TrimSpace(out)), nil
}
func listPrograms(dir string) ([]string, error) {
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
var programs []string
for _, de := range files {
fi, err := de.Info()
if err != nil {
return nil, fmt.Errorf("info %q: %w", de.Name(), err)
}
if isExecutable(fi) {
programs = append(programs, filepath.Join(dir, fi.Name()))
}
}
return programs, nil
}
func isExecutable(fi fs.FileInfo) bool {
return fi.Mode().Perm()&0111 != 0
}
// isSpecific revision installed from local repo or a specific SHA.
// In other words not some generally available package installed with @latest.
func isSpecific(v string) bool {
// Local
if v == "(devel)" {
return true
}
// Specific SHA or otherwise not a "clean" version.
if semver.IsValid(v) && semver.Prerelease(v) != "" {
return true
}
return false
}
// latest version of package, or error.
func latest(ctx context.Context, pkg string) (string, error) {
cmd := exec.CommandContext(ctx, "go", "list", "-m", "-json", pkg+"@latest")
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("go list (%w):\n%s", err, out)
}
listing := struct {
Version string
}{}
err = json.Unmarshal(out, &listing)
if err != nil {
return "", fmt.Errorf("json unmarshal: %v", err)
}
return listing.Version, nil
}
func installer(ctx context.Context, nProcs int, latestGo, force bool) error {
dir := gobin()
if dir == "" {
return errors.New("GOBIN not found")
}
progs, err := listPrograms(dir)
if err != nil {
return err
}
var goVersion string
// Check against the local toolchain version of Go since that is
// what we're going to use to install programs.
if latestGo {
goVersion, err = goversion(ctx)
if err != nil {
return fmt.Errorf("go version: %w", err)
}
}
var eg errgroup.Group
eg.SetLimit(nProcs)
for _, f := range progs {
ff := f
eg.Go(func() error {
info, err := buildinfo.ReadFile(ff)
if err != nil {
return err
}
if isSpecific(info.Main.Version) {
fmt.Printf("%s %s skip\n", info.Path, info.Main.Version)
return nil
}
// Latest available is checked per module.
// TODO: Cache this lookup.
target, err := latest(ctx, info.Main.Path)
if err != nil {
if errors.Is(err, context.Canceled) {
return nil
}
fmt.Printf("%s\n", err)
// TODO: Doesn't work for golang.org/x/tools/cmd/auth/authtest
target = "?"
}
goUpgrade := latestGo && goVersion != info.GoVersion
modUpgrade := target != info.Main.Version
if !(force || goUpgrade || modUpgrade) {
fmt.Printf("%s %s already latest\n", info.Path, info.Main.Version)
return nil
}
fmt.Printf("%s %s -> %s\n", info.Path, info.Main.Version, target)
// TODO: Is it faster to combine packages from the same module into a single exec?
cmd := exec.CommandContext(ctx, "go", "install", info.Path+"@latest")
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("go install (%s):\n%s", err, out)
}
// TODO: If no longer present in module or deprecated, ask if remove?
return nil
})
}
return eg.Wait()
}
const help = `Usage: go-latest [options]
Install the latest version of go install'd programs in GOBIN.
Options:
`
func runMain(ctx context.Context) error {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), help)
flag.PrintDefaults()
}
showVersion := flag.Bool("v", false, "Print version and exit")
nProcs := flag.Int("j", 0, "Number of parallel workers, defaults to number of CPUs")
latestGo := flag.Bool("go", false, "Re-install programs not built with the current version of Go")
force := flag.Bool("force", false, "Re-install everything")
flag.Parse()
if *showVersion {
bi, ok := debug.ReadBuildInfo()
if !ok {
return errors.New("could not read buildinfo")
}
fmt.Println(bi.Main.Version)
return nil
}
if *nProcs == 0 {
*nProcs = runtime.NumCPU()
}
dir, err := os.MkdirTemp("", "")
if err != nil {
return fmt.Errorf("make temp dir: %w", err)
}
defer os.Remove(dir)
err = os.Chdir(dir)
if err != nil {
return fmt.Errorf("chdir: %w", err)
}
err = installer(ctx, *nProcs, *latestGo, *force)
if err != nil {
return err
}
return nil
}
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
err := runMain(ctx)
if err != nil {
if !errors.Is(err, context.Canceled) {
fmt.Printf("%s", err.Error())
}
os.Exit(1)
}
}