From d623488da7648b753126a52290966410463668cc Mon Sep 17 00:00:00 2001 From: Sung Won Cho Date: Sat, 1 Apr 2017 12:11:33 +1100 Subject: [PATCH] Check update --- main.go | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 831ee647..7849167e 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,11 @@ package main import ( + "encoding/json" "fmt" "gopkg.in/yaml.v2" "io/ioutil" + "net/http" "os" "os/user" "sort" @@ -17,6 +19,7 @@ type Note map[string][]string const configFilename = ".dnoterc" const dnoteFilename = ".dnote" +const version = "v0.0.1" func getConfigPath() (string, error) { usr, err := user.Current() @@ -233,16 +236,50 @@ func checkFileExists(filepath string) bool { return !os.IsNotExist(err) } +func checkUpdates() error { + endpoint := "https://api.github.com/repos/dnote-io/cli/releases" + resp, err := http.Get(endpoint) + if err != nil { + return err + } + + defer resp.Body.Close() + + var x []map[string]interface{} + err = json.NewDecoder(resp.Body).Decode(&x) + if err != nil { + return err + } + + if len(x) == 0 { + return nil + } + + latestRelease := x[0] + latestVersion := latestRelease["tag_name"] + + if version != latestVersion { + fmt.Println("==") + fmt.Printf("Update available: %s\n", latestVersion) + fmt.Printf("See: %s\n", x[0]["html_url"]) + fmt.Println("==\n") + } + + return nil +} + func main() { err := initDnote() check(err) + err = checkUpdates() + check(err) if len(os.Args) < 2 { - fmt.Println("Dnote - A command line tool to spontaneously record new learnings\n") + fmt.Println("Dnote - Spontaneously capture new engineering lessons\n") fmt.Println("Commands:") fmt.Println(" use [u] - choose the book") fmt.Println(" new [n] - write a new note") - fmt.Println(" ls [l] - show books") + fmt.Println(" books [b] - show books") os.Exit(0) } @@ -258,7 +295,7 @@ func main() { fmt.Println(note) err := writeNote(note) check(err) - case "ls", "l", "books": + case "books", "b": currentBook, err := getCurrentBook() check(err) books, err := getBooks()