Skip to content

Commit aa7cdd3

Browse files
committed
update upgrade script
1 parent a66908a commit aa7cdd3

File tree

4 files changed

+35
-43
lines changed

4 files changed

+35
-43
lines changed

upgrade/go.mod

Lines changed: 0 additions & 5 deletions
This file was deleted.

upgrade/go.sum

Lines changed: 0 additions & 12 deletions
This file was deleted.

upgrade/package.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

upgrade/upgrade.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"archive/zip"
88
"bufio"
99
"bytes"
10+
"errors"
1011
"fmt"
1112
"io"
1213
"io/ioutil"
@@ -15,36 +16,49 @@ import (
1516
"os"
1617
"path"
1718
"path/filepath"
19+
"runtime"
1820
"strings"
19-
"time"
20-
21-
"github.com/PuerkitoBio/goquery"
2221
)
2322

2423
func download(prefix string) (url string, content []byte, err error) {
25-
year := time.Now().Year()
26-
27-
site := "https://www.sqlite.org/download.html"
28-
//fmt.Printf("scraping %v\n", site)
29-
doc, err := goquery.NewDocument(site)
24+
resp, err := http.Get("https://www.sqlite.org/download.html")
3025
if err != nil {
31-
log.Fatal(err)
26+
return "", nil, err
3227
}
28+
defer resp.Body.Close()
3329

34-
doc.Find("a").Each(func(_ int, s *goquery.Selection) {
35-
if strings.HasPrefix(s.Text(), prefix) {
36-
url = fmt.Sprintf("https://www.sqlite.org/%d/", year) + s.Text()
30+
b, err := ioutil.ReadAll(resp.Body)
31+
if err != nil {
32+
return "", nil, err
33+
}
34+
html := string(b)
35+
start := strings.Index(html, `<!-- Download product data for scripts to read`)
36+
if start == -1 {
37+
return "", nil, errors.New("Unable to find download section on sqlite.org")
38+
}
39+
html = html[start:]
40+
end := strings.Index(html, `-->`)
41+
if end == -1 {
42+
return "", nil, errors.New("Unable to find download section on sqlite.org")
43+
}
44+
html = html[:end]
45+
for _, line := range strings.Split(html, "\n") {
46+
if strings.Contains(line, prefix) {
47+
if tok := strings.Split(line, ","); len(tok) >= 5 {
48+
url = fmt.Sprintf("https://www.sqlite.org/%s", tok[2])
49+
break
50+
}
3751
}
38-
})
52+
}
3953

4054
if url == "" {
4155
return "", nil, fmt.Errorf("Unable to find prefix '%s' on sqlite.org", prefix)
4256
}
4357

4458
fmt.Printf("Downloading %v\n", url)
45-
resp, err := http.Get(url)
59+
resp, err = http.Get(url)
4660
if err != nil {
47-
log.Fatal(err)
61+
return "", nil, err
4862
}
4963

5064
// Ready Body Content
@@ -98,13 +112,13 @@ func mergeFile(src string, dst string) error {
98112
func main() {
99113
fmt.Println("Go-SQLite3 Upgrade Tool")
100114

101-
wd, err := os.Getwd()
102-
if err != nil {
103-
log.Fatal(err)
115+
_, file, _, ok := runtime.Caller(0)
116+
if !ok {
117+
log.Fatal("could not get current file info")
104118
}
105-
if filepath.Base(wd) != "upgrade" {
106-
log.Printf("Current directory is %q but should run in upgrade directory", wd)
107-
os.Exit(1)
119+
err := os.Chdir(filepath.Dir(filepath.Dir(file)))
120+
if err != nil {
121+
log.Fatalf("could not change directory: %s", err)
108122
}
109123

110124
// Download Amalgamation

0 commit comments

Comments
 (0)