mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-21 02:55:37 -08:00
fix(upgrade): avoid duplicate HTTP requests
This commit is contained in:
parent
6c7cffc69a
commit
6d6efc2981
|
@ -2,8 +2,6 @@ package segments
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||||
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
"github.com/jandedobbeleer/oh-my-posh/src/properties"
|
||||||
|
@ -34,53 +32,59 @@ func (u *Upgrade) Init(props properties.Properties, env platform.Environment) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Upgrade) Enabled() bool {
|
func (u *Upgrade) Enabled() bool {
|
||||||
version := fmt.Sprintf("v%s", u.env.Flags().Version)
|
current := u.env.Flags().Version
|
||||||
|
latest := u.cachedLatest(current)
|
||||||
if shouldDisplay, err := u.ShouldDisplay(version); err == nil {
|
if len(latest) == 0 {
|
||||||
return shouldDisplay
|
latest = u.checkUpdate(current)
|
||||||
}
|
}
|
||||||
|
|
||||||
latest, err := upgrade.Latest(u.env)
|
if len(latest) == 0 || current == latest {
|
||||||
if err != nil {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if latest == version {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheData := &UpgradeCache{
|
|
||||||
Latest: latest,
|
|
||||||
Current: version,
|
|
||||||
}
|
|
||||||
cacheJSON, err := json.Marshal(cacheData)
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
oneWeek := 10080
|
|
||||||
cacheTimeout := u.props.GetInt(properties.CacheTimeout, oneWeek)
|
|
||||||
u.env.Cache().Set(UPGRADECACHEKEY, string(cacheJSON), cacheTimeout)
|
|
||||||
|
|
||||||
u.Version = latest
|
u.Version = latest
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Upgrade) ShouldDisplay(version string) (bool, error) {
|
func (u *Upgrade) cachedLatest(current string) string {
|
||||||
data, OK := u.env.Cache().Get(UPGRADECACHEKEY)
|
data, ok := u.env.Cache().Get(UPGRADECACHEKEY)
|
||||||
if !OK {
|
if !ok {
|
||||||
return false, errors.New("no cache")
|
return "" // no cache
|
||||||
}
|
}
|
||||||
|
|
||||||
var cacheJSON UpgradeCache
|
var cacheJSON UpgradeCache
|
||||||
err := json.Unmarshal([]byte(data), &cacheJSON)
|
err := json.Unmarshal([]byte(data), &cacheJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.New("invalid cache data")
|
return "" // invalid cache data
|
||||||
}
|
}
|
||||||
|
|
||||||
if version == cacheJSON.Current {
|
if current != cacheJSON.Current {
|
||||||
return cacheJSON.Current != cacheJSON.Latest, nil
|
return "" // version changed, run the check again
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, errors.New("version changed, run the check again")
|
return cacheJSON.Latest
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Upgrade) checkUpdate(current string) string {
|
||||||
|
tag, err := upgrade.Latest(u.env)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
latest := tag[1:]
|
||||||
|
cacheData := &UpgradeCache{
|
||||||
|
Latest: latest,
|
||||||
|
Current: current,
|
||||||
|
}
|
||||||
|
cacheJSON, err := json.Marshal(cacheData)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
oneWeek := 10080
|
||||||
|
cacheTimeout := u.props.GetInt(properties.CacheTimeout, oneWeek)
|
||||||
|
// update cache
|
||||||
|
u.env.Cache().Set(UPGRADECACHEKEY, string(cacheJSON), cacheTimeout)
|
||||||
|
|
||||||
|
return latest
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ func TestUpgrade(t *testing.T) {
|
||||||
HasCache bool
|
HasCache bool
|
||||||
CurrentVersion string
|
CurrentVersion string
|
||||||
LatestVersion string
|
LatestVersion string
|
||||||
CacheVersion string
|
CachedVersion string
|
||||||
Error error
|
Error error
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
@ -36,7 +36,7 @@ func TestUpgrade(t *testing.T) {
|
||||||
LatestVersion: "1.0.1",
|
LatestVersion: "1.0.1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Case: "Version error",
|
Case: "Error on update check",
|
||||||
Error: errors.New("error"),
|
Error: errors.New("error"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -44,7 +44,7 @@ func TestUpgrade(t *testing.T) {
|
||||||
HasCache: true,
|
HasCache: true,
|
||||||
CurrentVersion: "1.0.2",
|
CurrentVersion: "1.0.2",
|
||||||
LatestVersion: "1.0.3",
|
LatestVersion: "1.0.3",
|
||||||
CacheVersion: "1.0.2",
|
CachedVersion: "1.0.2",
|
||||||
ExpectedEnabled: true,
|
ExpectedEnabled: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -52,14 +52,14 @@ func TestUpgrade(t *testing.T) {
|
||||||
HasCache: true,
|
HasCache: true,
|
||||||
CurrentVersion: "1.0.2",
|
CurrentVersion: "1.0.2",
|
||||||
LatestVersion: "1.0.2",
|
LatestVersion: "1.0.2",
|
||||||
CacheVersion: "1.0.1",
|
CachedVersion: "1.0.1",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Case: "On previous, version changed",
|
Case: "On previous, version changed",
|
||||||
HasCache: true,
|
HasCache: true,
|
||||||
CurrentVersion: "1.0.2",
|
CurrentVersion: "1.0.2",
|
||||||
LatestVersion: "1.0.3",
|
LatestVersion: "1.0.3",
|
||||||
CacheVersion: "1.0.1",
|
CachedVersion: "1.0.1",
|
||||||
ExpectedEnabled: true,
|
ExpectedEnabled: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -69,10 +69,10 @@ func TestUpgrade(t *testing.T) {
|
||||||
cache := &mock.MockedCache{}
|
cache := &mock.MockedCache{}
|
||||||
|
|
||||||
env.On("Cache").Return(cache)
|
env.On("Cache").Return(cache)
|
||||||
if len(tc.CacheVersion) == 0 {
|
if len(tc.CachedVersion) == 0 {
|
||||||
tc.CacheVersion = tc.CurrentVersion
|
tc.CachedVersion = tc.CurrentVersion
|
||||||
}
|
}
|
||||||
cacheData := fmt.Sprintf(`{"latest":"v%s", "current": "v%s"}`, tc.LatestVersion, tc.CacheVersion)
|
cacheData := fmt.Sprintf(`{"latest":"%s", "current": "%s"}`, tc.LatestVersion, tc.CachedVersion)
|
||||||
cache.On("Get", UPGRADECACHEKEY).Return(cacheData, tc.HasCache)
|
cache.On("Get", UPGRADECACHEKEY).Return(cacheData, tc.HasCache)
|
||||||
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue