-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add check updates functionality to fix #35
- Loading branch information
Showing
5 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ui | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/NHAS/wag/internal/config" | ||
) | ||
|
||
type githubResponse struct { | ||
Body string | ||
Prerelease bool `json:"prerelease"` | ||
TagName string `json:"tag_name"` | ||
Published string `json:"published_at"` | ||
Url string `json:"html_url"` | ||
} | ||
|
||
type Update struct { | ||
New bool | ||
UpdateVersion string | ||
Message []string | ||
Url string | ||
Released string | ||
} | ||
|
||
var ( | ||
mostRecentUpdate *Update | ||
lastChecked time.Time | ||
) | ||
|
||
func getUpdate() Update { | ||
|
||
if !config.Values().CheckUpdates { | ||
return Update{} | ||
} | ||
|
||
if time.Now().After(lastChecked.Add(15*time.Minute)) || mostRecentUpdate == nil { | ||
resp, err := http.Get("https://api.github.com/repos/NHAS/wag/releases/latest") | ||
if err != nil { | ||
return Update{} | ||
} | ||
defer resp.Body.Close() | ||
|
||
var gr githubResponse | ||
err = json.NewDecoder(resp.Body).Decode(&gr) | ||
if err != nil { | ||
return Update{} | ||
} | ||
|
||
mostRecentUpdate = &Update{ | ||
UpdateVersion: gr.TagName, | ||
Message: strings.Split(gr.Body, "\r\n"), | ||
Url: gr.Url, | ||
Released: gr.Published, | ||
} | ||
} | ||
|
||
mostRecentUpdate.New = strings.Split(config.Version, "-")[0] != mostRecentUpdate.UpdateVersion | ||
|
||
return *mostRecentUpdate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package ui | ||
|
||
type Page struct { | ||
Update | ||
Description string | ||
Title string | ||
User string | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters