Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Refactor version checking slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
Bios-Marcel committed Oct 25, 2020
1 parent 84e549d commit c2de287
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
14 changes: 2 additions & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ func RunWithAccount(account string) {
configuration.Token = configuration.GetAccountToken(account)
}

updateAvailableChannel := make(chan bool, 1)
if configuration.ShowUpdateNotifications {
go func() {
updateAvailableChannel <- version.IsLocalOutdated(configuration.DontShowUpdateNotificationFor)
}()
} else {
updateAvailableChannel <- false
}

updateAvailableChannel := version.CheckForUpdate(configuration.DontShowUpdateNotificationFor)
app.MouseEnabled = configuration.MouseEnabled

go func() {
Expand All @@ -78,9 +70,7 @@ func RunWithAccount(account string) {

readstate.Load(discord.State)

isUpdateAvailable := <-updateAvailableChannel
close(updateAvailableChannel)
if isUpdateAvailable {
if isUpdateAvailable := <-updateAvailableChannel; isUpdateAvailable {
waitForUpdateDialogChannel := make(chan bool, 1)

dialog := tview.NewModal()
Expand Down
66 changes: 38 additions & 28 deletions version/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,54 @@ import (
"github.com/google/go-github/v29/github"
)

var latestRemoteVersion *string

// IsLocalOutdated checks the latest release on github and compares the version
// numbers. Version numbers representing a later date than the local version
// will cause the call to return true. If an error occurs, we will return
// false. If dontRemindFor is passed, false will be returned if it equals the
// current remote version. This is used in order to avoid spamming the user
// with an update notification on every startup.
func IsLocalOutdated(dontRemindFor string) bool {
if GetLatestRemoteVersion() == "" || GetLatestRemoteVersion() == dontRemindFor {
return false
}

return isCurrentOlder(Version, GetLatestRemoteVersion())
var latestRemoteVersion string

// CheckForUpdate checks whether the Version-string saved at version.Version
// is different to the name of the latest tagged GitHub release. If it is, and
// the release name isn't equal to the `donRemindFor`-parameter, we return
// true, which stands for "update available". This allows the user to manually
// say "Don't remind me again for this version". The actual return value is
// supplied via a channel that's closed after one value has been read.
func CheckForUpdate(dontRemindFor string) chan bool {
//Note, this isn't buffered, so that we can safely close the channel
//when it has been read by the caller.
updateAvailableChannel := make(chan bool)
go func() {
remoteVersion := GetLatestRemoteVersion()
if remoteVersion == "" || remoteVersion == dontRemindFor {
//Error retrieving version or user wishes to ignore update.
updateAvailableChannel <- false
} else {
updateAvailableChannel <- isLocalOlderThanRemote(Version, remoteVersion)
}
close(updateAvailableChannel)
}()
return updateAvailableChannel
}

func isCurrentOlder(current, other string) bool {
var yearRemote, monthRemote, dayRemote int
fmt.Sscanf(other, "%04d-%02d-%02d", &yearRemote, &monthRemote, &dayRemote)
var yearLocal, monthLocal, dayLocal int
fmt.Sscanf(current, "%04d-%02d-%02d", &yearLocal, &monthLocal, &dayLocal)
func isLocalOlderThanRemote(local, remote string) bool {
yearRemote, monthRemote, dayRemote := parseTag(remote)
yearLocal, monthLocal, dayLocal := parseTag(local)
return !(yearLocal >= yearRemote && monthLocal >= monthRemote && dayLocal >= dayRemote)
}

func parseTag(tag string) (year int, month int, day int) {
fmt.Sscanf(tag, "%04d-%02d-%02d", &year, &month, &day)
return
}

// GetLatestRemoteVersion queries GitHub for the latest Release-Tag and caches
// it. This value will never be updated during runtime of cordless.
// it. This value will never be updated during runtime.
func GetLatestRemoteVersion() string {
if latestRemoteVersion != nil {
return *latestRemoteVersion
if latestRemoteVersion != "" {
return latestRemoteVersion
}

repositoryRelease, _, lookupError := github.NewClient(nil).Repositories.GetLatestRelease(context.Background(), "Bios-Marcel", "cordless")
if lookupError == nil {
latestRemoteVersion = repositoryRelease.TagName
} else {
emptyVersion := ""
latestRemoteVersion = &emptyVersion
if lookupError != nil || repositoryRelease == nil {
return ""
}

return *latestRemoteVersion
latestRemoteVersion = *repositoryRelease.TagName
return latestRemoteVersion
}
2 changes: 1 addition & 1 deletion version/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Test_isCurrentOlder(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isCurrentOlder(tt.args.current, tt.args.other); got != tt.want {
if got := isLocalOlderThanRemote(tt.args.current, tt.args.other); got != tt.want {
t.Errorf("isCurrentOlder() = %v, want %v", got, tt.want)
}
})
Expand Down
4 changes: 4 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package version

// Version reflects the latest release date. For local builds, this number
// isn't really correct. Therefore new commits also won't lead to an update
// reminder, unless the latest tag is different to this number. This should
// not be touched manually.
var Version = "2020-10-24"

0 comments on commit c2de287

Please sign in to comment.