update upgrade script

This commit is contained in:
Yasuhiro Matsumoto
2026-01-01 22:32:40 +09:00
parent a66908a543
commit aa7cdd300f
4 changed files with 35 additions and 43 deletions

View File

@@ -1,5 +0,0 @@
module github.com/mattn/go-sqlite3/upgrade
go 1.19
require github.com/PuerkitoBio/goquery v1.7.1 // indirect

View File

@@ -1,12 +0,0 @@
github.com/PuerkitoBio/goquery v1.7.1 h1:oE+T06D+1T7LNrn91B4aERsRIeCLJ/oPSa6xB9FPnz4=
github.com/PuerkitoBio/goquery v1.7.1/go.mod h1:XY0pP4kfraEmmV1O7Uf6XyjoslwsneBbgeDjLYuN8xY=
github.com/andybalholm/cascadia v1.2.0 h1:vuRCkM5Ozh/BfmsaTm26kbjm0mIOM3yS5Ek/F5h18aE=
github.com/andybalholm/cascadia v1.2.0/go.mod h1:YCyR8vOZT9aZ1CHEd8ap0gMVm2aFgxBp0T0eFw1RUQY=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e h1:XpT3nA5TvE525Ne3hInMh6+GETgn27Zfm9dxsThnX2Q=
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View File

@@ -1,5 +0,0 @@
// Package upgrade is a dummy package to ensure package can be loaded
//
// This file is to avoid the following error:
// can't load package: package go-sqlite3/upgrade: build constraints exclude all Go files in go-sqlite3\upgrade
package upgrade

View File

@@ -7,6 +7,7 @@ import (
"archive/zip" "archive/zip"
"bufio" "bufio"
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@@ -15,36 +16,49 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"time"
"github.com/PuerkitoBio/goquery"
) )
func download(prefix string) (url string, content []byte, err error) { func download(prefix string) (url string, content []byte, err error) {
year := time.Now().Year() resp, err := http.Get("https://www.sqlite.org/download.html")
site := "https://www.sqlite.org/download.html"
//fmt.Printf("scraping %v\n", site)
doc, err := goquery.NewDocument(site)
if err != nil { if err != nil {
log.Fatal(err) return "", nil, err
} }
defer resp.Body.Close()
doc.Find("a").Each(func(_ int, s *goquery.Selection) { b, err := ioutil.ReadAll(resp.Body)
if strings.HasPrefix(s.Text(), prefix) { if err != nil {
url = fmt.Sprintf("https://www.sqlite.org/%d/", year) + s.Text() return "", nil, err
}
html := string(b)
start := strings.Index(html, `<!-- Download product data for scripts to read`)
if start == -1 {
return "", nil, errors.New("Unable to find download section on sqlite.org")
}
html = html[start:]
end := strings.Index(html, `-->`)
if end == -1 {
return "", nil, errors.New("Unable to find download section on sqlite.org")
}
html = html[:end]
for _, line := range strings.Split(html, "\n") {
if strings.Contains(line, prefix) {
if tok := strings.Split(line, ","); len(tok) >= 5 {
url = fmt.Sprintf("https://www.sqlite.org/%s", tok[2])
break
}
}
} }
})
if url == "" { if url == "" {
return "", nil, fmt.Errorf("Unable to find prefix '%s' on sqlite.org", prefix) return "", nil, fmt.Errorf("Unable to find prefix '%s' on sqlite.org", prefix)
} }
fmt.Printf("Downloading %v\n", url) fmt.Printf("Downloading %v\n", url)
resp, err := http.Get(url) resp, err = http.Get(url)
if err != nil { if err != nil {
log.Fatal(err) return "", nil, err
} }
// Ready Body Content // Ready Body Content
@@ -98,13 +112,13 @@ func mergeFile(src string, dst string) error {
func main() { func main() {
fmt.Println("Go-SQLite3 Upgrade Tool") fmt.Println("Go-SQLite3 Upgrade Tool")
wd, err := os.Getwd() _, file, _, ok := runtime.Caller(0)
if err != nil { if !ok {
log.Fatal(err) log.Fatal("could not get current file info")
} }
if filepath.Base(wd) != "upgrade" { err := os.Chdir(filepath.Dir(filepath.Dir(file)))
log.Printf("Current directory is %q but should run in upgrade directory", wd) if err != nil {
os.Exit(1) log.Fatalf("could not change directory: %s", err)
} }
// Download Amalgamation // Download Amalgamation