Skip to content

Commit

Permalink
Add support for new release check
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen Premaratne committed Mar 7, 2020
1 parent 7285e03 commit a6a0a89
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (app *App) validate() {
if len(os.Args[1:]) < 1 {
fmt.Printf("no arguments are given\n\n")
app.usage()
os.Exit(1)
}

if app.Started != "" {
Expand All @@ -59,6 +60,7 @@ func (app *App) validate() {

if app.Help {
app.usage()
os.Exit(0)
}

if app.Version {
Expand Down Expand Up @@ -94,5 +96,4 @@ func (app *App) usage() {
"\ttimesheet -remaining -d 2020-03-05\n" +
"\ttimesheet -remaining -history\n" +
"\ttimesheet -remaining -history -d 2020-03-05\n")
os.Exit(1)
}
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"os"
)

Expand Down Expand Up @@ -52,6 +54,31 @@ type Application interface {

var VERSION string

func (app *App) upgrade() {
var client = &http.Client{}
req, rErr := http.NewRequest("GET", "https://api.github.com/repos/praveenprem/timesheet/releases/latest", nil)
if rErr != nil {
panic(rErr)
}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var response struct {
Name string `json:"name"`
URL string `json:"html_url"`
}
decodeErr := json.NewDecoder(resp.Body).Decode(&response)
if decodeErr != nil {
panic(decodeErr)
}

if fmt.Sprintf("v%s", VERSION) != response.Name {
fmt.Println("New version available! Please download the latest release from", response.URL)
}
}

func main() {
var app App

Expand All @@ -64,6 +91,7 @@ func main() {

app.Parser()
app.loadConf()
app.upgrade()

if app.TimeRemaining {
app.GetTimeRemaining(app.Configuration.Domain, app.Configuration.Auth)
Expand Down

0 comments on commit a6a0a89

Please sign in to comment.