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

Commit

Permalink
Improve update check
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Sep 27, 2018
1 parent ad1e625 commit 451ed1c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions bookbrowser.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -119,6 +121,7 @@ func main() {
if len(s.Indexer.BookList()) == 0 {
log.Fatalln("Fatal error: no books found")
}
checkUpdate()
}()

sigusr.Handle(func() {
Expand All @@ -131,3 +134,34 @@ func main() {
log.Fatalf("Error starting server: %s\n", err)
}
}

func checkUpdate() {
resp, err := http.Get("https://api.github.com/repos/geek1011/BookBrowser/releases/latest")
if err != nil {
return
}
defer resp.Body.Close()

buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}

if resp.StatusCode != 200 {
return
}

var obj struct {
URL string `json:"html_url"`
Tag string `json:"tag_name"`
}
if json.Unmarshal(buf, &obj) != nil {
return
}

if curversion != "dev" {
if !strings.HasPrefix(curversion, obj.Tag) {
log.Printf("Running version %s. Latest version is %s: %s\n", curversion, obj.Tag, obj.URL)
}
}
}

0 comments on commit 451ed1c

Please sign in to comment.