Skip to content
This repository was archived by the owner on Jun 27, 2020. It is now read-only.

Commit 451ed1c

Browse files
committed
Improve update check
1 parent ad1e625 commit 451ed1c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

bookbrowser.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io/ioutil"
67
"log"
8+
"net/http"
79
"os"
810
"os/signal"
911
"path/filepath"
@@ -119,6 +121,7 @@ func main() {
119121
if len(s.Indexer.BookList()) == 0 {
120122
log.Fatalln("Fatal error: no books found")
121123
}
124+
checkUpdate()
122125
}()
123126

124127
sigusr.Handle(func() {
@@ -131,3 +134,34 @@ func main() {
131134
log.Fatalf("Error starting server: %s\n", err)
132135
}
133136
}
137+
138+
func checkUpdate() {
139+
resp, err := http.Get("https://api.github.com/repos/geek1011/BookBrowser/releases/latest")
140+
if err != nil {
141+
return
142+
}
143+
defer resp.Body.Close()
144+
145+
buf, err := ioutil.ReadAll(resp.Body)
146+
if err != nil {
147+
return
148+
}
149+
150+
if resp.StatusCode != 200 {
151+
return
152+
}
153+
154+
var obj struct {
155+
URL string `json:"html_url"`
156+
Tag string `json:"tag_name"`
157+
}
158+
if json.Unmarshal(buf, &obj) != nil {
159+
return
160+
}
161+
162+
if curversion != "dev" {
163+
if !strings.HasPrefix(curversion, obj.Tag) {
164+
log.Printf("Running version %s. Latest version is %s: %s\n", curversion, obj.Tag, obj.URL)
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)