Skip to content

Commit e51f46f

Browse files
committed
fix: improve historic version detector
1 parent a9f4926 commit e51f46f

4 files changed

Lines changed: 95 additions & 45 deletions

File tree

_script/go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module syncthing.net/docs
22

3-
go 1.23.0
3+
go 1.24.0
44

55
require (
66
github.com/google/go-github/v49 v49.1.0
7-
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
8-
golang.org/x/tools v0.12.0
7+
golang.org/x/exp v0.0.0-20260209203927-2842357ff358
8+
golang.org/x/tools v0.42.0
99
)
1010

1111
require (
1212
github.com/google/go-querystring v1.1.0 // indirect
1313
golang.org/x/crypto v0.35.0 // indirect
14-
golang.org/x/mod v0.12.0 // indirect
15-
golang.org/x/sys v0.30.0 // indirect
14+
golang.org/x/mod v0.33.0 // indirect
15+
golang.org/x/sync v0.19.0 // indirect
1616
)

_script/go.sum

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
2-
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
3-
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
3+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
44
github.com/google/go-github/v49 v49.1.0 h1:LFkMgawGQ8dfzWLH/rNE0b3u1D3n6/dw7ZmrN3b+YFY=
55
github.com/google/go-github/v49 v49.1.0/go.mod h1:MUUzHPrhGniB6vUKa27y37likpipzG+BXXJbG04J334=
66
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
77
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
88
golang.org/x/crypto v0.35.0 h1:b15kiHdrGCHrP6LvwaQ3c03kgNhhiMgvlhxHQhmg2Xs=
99
golang.org/x/crypto v0.35.0/go.mod h1:dy7dXNW32cAb/6/PRuTNsix8T+vJAqvuIy5Bli/x0YQ=
10-
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb h1:mIKbk8weKhSeLH2GmUTrvx8CjkyJmnU1wFmg59CUjFA=
11-
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
12-
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
13-
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
14-
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
15-
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
16-
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
17-
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
18-
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
19-
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
10+
golang.org/x/exp v0.0.0-20260209203927-2842357ff358 h1:kpfSV7uLwKJbFSEgNhWzGSL47NDSF/5pYYQw1V0ub6c=
11+
golang.org/x/exp v0.0.0-20260209203927-2842357ff358/go.mod h1:R3t0oliuryB5eenPWl3rrQxwnNM3WTwnsRZZiXLAAW8=
12+
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
13+
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
14+
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
15+
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
16+
golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k=
17+
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=
2018
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

_script/histver/synver.go

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"bytes"
77
"compress/gzip"
88
"context"
9+
"errors"
910
"flag"
1011
"fmt"
1112
"io"
@@ -34,7 +35,7 @@ func main() {
3435
}
3536

3637
// Load current versions table
37-
var table []*tableRow
38+
var table []tableRow
3839
fd, err := os.Open(*versionsFile)
3940
if os.IsNotExist(err) {
4041
// File doesn't exist yet. That's alright.
@@ -109,39 +110,53 @@ func getReleases(ctx context.Context) ([]*github.RepositoryRelease, error) {
109110
return releases, nil
110111
}
111112

112-
func getReleaseVersion(rel *github.RepositoryRelease) (*tableRow, error) {
113+
func getReleaseVersion(rel *github.RepositoryRelease) (tableRow, error) {
113114
goos := runtime.GOOS
114115
if goos == "darwin" {
115116
goos = "macos"
116117
}
118+
119+
row := tableRow{
120+
Version: rel.GetTagName(),
121+
Date: rel.GetCreatedAt().Format("2006-01-01"),
122+
}
123+
117124
find := fmt.Sprintf("syncthing-%s-%s", goos, runtime.GOARCH)
118125
for _, asset := range rel.Assets {
119126
if strings.HasPrefix(*asset.Name, find) {
120127
log.Println("Downloading", *asset.Name)
121128
resp, err := http.Get(*asset.BrowserDownloadURL)
122129
if err != nil {
123-
return nil, err
130+
return tableRow{}, err
124131
}
125132
bs, err := io.ReadAll(resp.Body)
126133
resp.Body.Close()
127134
if err != nil {
128-
return nil, err
135+
return tableRow{}, err
129136
}
130137
switch filepath.Ext(*asset.Name) {
131138
case ".zip":
132-
return getReleaseVersionZip(bs)
139+
r, err := getReleaseVersionZip(bs)
140+
if err != nil {
141+
return tableRow{}, err
142+
}
143+
return row.merge(r), nil
133144
default:
134-
return getReleaseVersionTarGz(bs)
145+
r, err := getReleaseVersionTarGz(bs)
146+
if err != nil {
147+
return tableRow{}, err
148+
}
149+
return row.merge(r), nil
135150
}
136151
}
137152
}
138-
return nil, fmt.Errorf("no asset found")
153+
return tableRow{}, fmt.Errorf("no asset found")
139154
}
140155

141-
func getReleaseVersionZip(bs []byte) (*tableRow, error) {
156+
func getReleaseVersionZip(bs []byte) (tableRow, error) {
142157
zr, err := zip.NewReader(bytes.NewReader(bs), int64(len(bs)))
143158
if err != nil {
144-
return nil, err
159+
return tableRow{}, err
145160
}
146161
for _, f := range zr.File {
147162
if strings.Contains(path.Dir(f.Name), "/") {
@@ -153,18 +168,18 @@ func getReleaseVersionZip(bs []byte) (*tableRow, error) {
153168
}
154169
rd, err := f.Open()
155170
if err != nil {
156-
return nil, err
171+
return tableRow{}, err
157172
}
158173

159174
return getVersionFromReader(rd)
160175
}
161-
return nil, fmt.Errorf("no syncthing binary found")
176+
return tableRow{}, fmt.Errorf("no syncthing binary found")
162177
}
163178

164-
func getReleaseVersionTarGz(bs []byte) (*tableRow, error) {
179+
func getReleaseVersionTarGz(bs []byte) (tableRow, error) {
165180
gr, err := gzip.NewReader(bytes.NewReader(bs))
166181
if err != nil {
167-
return nil, err
182+
return tableRow{}, err
168183
}
169184
tr := tar.NewReader(gr)
170185
for {
@@ -178,36 +193,64 @@ func getReleaseVersionTarGz(bs []byte) (*tableRow, error) {
178193

179194
return getVersionFromReader(tr)
180195
}
181-
return nil, fmt.Errorf("no syncthing binary found")
196+
return tableRow{}, fmt.Errorf("no syncthing binary found")
182197
}
183198

184-
func getVersionFromReader(r io.Reader) (*tableRow, error) {
199+
func getVersionFromReader(r io.Reader) (tableRow, error) {
185200
fd, err := os.CreateTemp("", "syncthing")
186201
if err != nil {
187-
return nil, err
202+
return tableRow{}, err
188203
}
189204
if _, err := io.Copy(fd, r); err != nil {
190-
return nil, err
205+
return tableRow{}, err
191206
}
192207
fd.Close()
193208
defer os.Remove(fd.Name())
194209
if err := os.Chmod(fd.Name(), 0o755); err != nil {
195-
return nil, err
210+
return tableRow{}, err
196211
}
197212

198-
return getVersionFromCommand(fd.Name())
213+
if row, err := getVersionFromCommand(fd.Name()); err == nil {
214+
return row, nil
215+
}
216+
return getVersionFromGo(fd.Name())
217+
}
218+
219+
func getVersionFromGo(name string) (tableRow, error) {
220+
cmd := exec.Command("go", "version", "-m", name)
221+
out, err := cmd.Output()
222+
if err != nil {
223+
return tableRow{}, err
224+
}
225+
226+
// % go version -m ~/bin/syncthing
227+
// /Users/jb/bin/syncthing: go1.25.7
228+
// path github.com/syncthing/syncthing/cmd/syncthing
229+
// ...
230+
231+
if idx := bytes.Index(out, []byte{'\n'}); idx < 0 {
232+
return tableRow{}, errors.New("no version")
233+
} else {
234+
out = out[:idx]
235+
}
236+
237+
if idx := bytes.LastIndex(out, []byte{' '}); idx < 0 {
238+
return tableRow{}, errors.New("no version")
239+
} else {
240+
return tableRow{Runtime: string(out[idx+1:])}, nil
241+
}
199242
}
200243

201-
func getVersionFromCommand(name string) (*tableRow, error) {
244+
func getVersionFromCommand(name string) (tableRow, error) {
202245
cmd := exec.Command(name, "--version")
203246
out, err := cmd.Output()
204247
if err != nil {
205-
return nil, err
248+
return tableRow{}, err
206249
}
207250

208251
var r tableRow
209252
if err := r.fromVersion(string(out)); err != nil {
210-
return nil, err
253+
return tableRow{}, err
211254
}
212-
return &r, nil
255+
return r, nil
213256
}

_script/histver/table.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"cmp"
45
"encoding/csv"
56
"fmt"
67
"io"
@@ -14,6 +15,14 @@ type tableRow struct {
1415
Date string
1516
}
1617

18+
func (t tableRow) merge(other tableRow) tableRow {
19+
return tableRow{
20+
Version: cmp.Or(other.Version, t.Version),
21+
Runtime: cmp.Or(other.Runtime, t.Runtime),
22+
Date: cmp.Or(other.Date, t.Date),
23+
}
24+
}
25+
1726
func (r *tableRow) fromStrings(ss []string) error {
1827
if len(ss) < 3 {
1928
return fmt.Errorf("not enough fields")
@@ -37,13 +46,13 @@ func (r *tableRow) fromVersion(ver string) error {
3746
return nil
3847
}
3948

40-
func (r *tableRow) toStrings() []string {
49+
func (r tableRow) toStrings() []string {
4150
return []string{r.Version, r.Runtime, r.Date}
4251
}
4352

4453
var tableHeader = []string{"Version", "Runtime", "Date"}
4554

46-
func writeTable(w io.Writer, rows []*tableRow) error {
55+
func writeTable(w io.Writer, rows []tableRow) error {
4756
sort.Slice(rows, func(a, b int) bool {
4857
if rows[a].Date == rows[b].Date {
4958
return rows[a].Version > rows[b].Version
@@ -63,9 +72,9 @@ func writeTable(w io.Writer, rows []*tableRow) error {
6372
return cw.Error()
6473
}
6574

66-
func readTable(r io.Reader) ([]*tableRow, error) {
75+
func readTable(r io.Reader) ([]tableRow, error) {
6776
cr := csv.NewReader(r)
68-
var rows []*tableRow
77+
var rows []tableRow
6978
for {
7079
ss, err := cr.Read()
7180
if err == io.EOF {
@@ -84,7 +93,7 @@ func readTable(r io.Reader) ([]*tableRow, error) {
8493
if err := row.fromStrings(ss); err != nil {
8594
return nil, err
8695
}
87-
rows = append(rows, &row)
96+
rows = append(rows, row)
8897
}
8998
return rows, nil
9099
}

0 commit comments

Comments
 (0)