Skip to content

Commit

Permalink
fix(upgrade): restore caching mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Nov 6, 2024
1 parent 905dd20 commit 63d79fe
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 18 deletions.
27 changes: 22 additions & 5 deletions src/config/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ func (segment *Segment) migrate(version int) {

// Cache settings
delete(segment.Properties, "cache_version")
segment.Cache = segment.migrateCache()

segment.IncludeFolders = segment.migrateFolders(includeFolders)
segment.ExcludeFolders = segment.migrateFolders(excludeFolders)

switch segment.Type { //nolint:exhaustive
case UPGRADE:
segment.timeoutToDuration()
default:
segment.timeoutToCache()
}
}

func (segment *Segment) hasProperty(property properties.Property) bool {
Expand All @@ -45,24 +51,35 @@ func (segment *Segment) hasProperty(property properties.Property) bool {
return false
}

func (segment *Segment) migrateCache() *cache.Config {
func (segment *Segment) timeoutToCache() {
if !segment.hasProperty(cacheTimeout) {
return nil
return
}

timeout := segment.Properties.GetInt(cacheTimeout, 0)
delete(segment.Properties, cacheTimeout)

if timeout == 0 {
return nil
return
}

return &cache.Config{
segment.Cache = &cache.Config{
Duration: cache.ToDuration(timeout * 60),
Strategy: cache.Folder,
}
}

func (segment *Segment) timeoutToDuration() {
timeout := segment.Properties.GetInt(cacheTimeout, 0)
delete(segment.Properties, cacheTimeout)

if timeout == 0 {
return
}

segment.Properties[properties.CacheDuration] = cache.ToDuration(timeout * 60)
}

func (segment *Segment) migrateFolders(property properties.Property) []string {
if !segment.hasProperty(property) {
return []string{}
Expand Down
6 changes: 3 additions & 3 deletions src/config/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestMigrateCache(t *testing.T) {
func TestTimeoutToCache(t *testing.T) {
cases := []struct {
Expected *cache.Config
Case string
Expand All @@ -27,8 +27,8 @@ func TestMigrateCache(t *testing.T) {
},
}

got := segment.migrateCache()
assert.Equal(t, tc.Expected, got, tc.Case)
segment.timeoutToCache()
assert.Equal(t, tc.Expected, segment.Cache, tc.Case)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/properties/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ const (
DefaultHTTPTimeout = 20
// Files to trigger the segment on
Files Property = "files"
// Duration of the cache
CacheDuration Property = "cache_duration"
)

type Map map[Property]any
Expand Down
60 changes: 50 additions & 10 deletions src/segments/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package segments

import (
"encoding/json"
"errors"

"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"
)

type upgradeData struct {
type UpgradeCache struct {
Latest string `json:"latest"`
Current string `json:"current"`
}
Expand All @@ -16,36 +21,71 @@ type Upgrade struct {
// deprecated
Version string

upgradeData
UpgradeCache
}

const (
UPGRADECACHEKEY = "upgrade_segment"
)

func (u *Upgrade) Template() string {
return " \uf019 "
}

func (u *Upgrade) Enabled() bool {
u.Current = build.Version

latest, err := u.checkUpdate(u.Current)
latest, err := u.cachedLatest(u.Current)
if err != nil {
latest, err = u.checkUpdate(u.Current)
}

if err != nil || u.Current == latest.Latest {
return false
}

u.upgradeData = *latest
u.UpgradeCache = *latest
u.Version = u.Latest
return true
}

func (u *Upgrade) checkUpdate(current string) (*upgradeData, error) {
func (u *Upgrade) cachedLatest(current string) (*UpgradeCache, error) {
data, ok := u.env.Cache().Get(UPGRADECACHEKEY)
if !ok {
return nil, errors.New("no cache data")
}

var cacheJSON UpgradeCache
err := json.Unmarshal([]byte(data), &cacheJSON)
if err != nil {
return nil, err // invalid cache data
}

if current != cacheJSON.Current {
return nil, errors.New("version changed, run the check again")
}

return &cacheJSON, nil
}

func (u *Upgrade) checkUpdate(current string) (*UpgradeCache, error) {
tag, err := upgrade.Latest(u.env)
if err != nil {
return nil, err
}

return &upgradeData{
// strip leading v
Latest: tag[1:],
latest := tag[1:]
cacheData := &UpgradeCache{
Latest: latest,
Current: current,
}, nil
}
cacheJSON, err := json.Marshal(cacheData)
if err != nil {
return nil, err
}

// update cache
duration := u.props.GetString(properties.CacheDuration, string(cache.ONEWEEK))
u.env.Cache().Set(UPGRADECACHEKEY, string(cacheJSON), cache.Duration(duration))

return cacheData, nil
}
23 changes: 23 additions & 0 deletions src/segments/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"testing"

"github.com/jandedobbeleer/oh-my-posh/src/build"
cache_ "github.com/jandedobbeleer/oh-my-posh/src/cache/mock"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"

"github.com/alecthomas/assert"
testify_ "github.com/stretchr/testify/mock"
)

func TestUpgrade(t *testing.T) {
Expand Down Expand Up @@ -38,21 +40,42 @@ func TestUpgrade(t *testing.T) {
Case: "Error on update check",
Error: errors.New("error"),
},
{
Case: "On previous, from cache",
HasCache: true,
CurrentVersion: "1.0.2",
LatestVersion: "1.0.3",
CachedVersion: "1.0.2",
ExpectedEnabled: true,
},
{
Case: "On latest, version changed",
HasCache: true,
CurrentVersion: "1.0.2",
LatestVersion: "1.0.2",
CachedVersion: "1.0.1",
},
{
Case: "On previous, version changed",
HasCache: true,
CurrentVersion: "1.0.2",
LatestVersion: "1.0.3",
CachedVersion: "1.0.1",
ExpectedEnabled: true,
},
}

for _, tc := range cases {
env := new(mock.Environment)
cache := &cache_.Cache{}

env.On("Cache").Return(cache)
if len(tc.CachedVersion) == 0 {
tc.CachedVersion = tc.CurrentVersion
}
cacheData := fmt.Sprintf(`{"latest":"%s", "current": "%s"}`, tc.LatestVersion, tc.CachedVersion)
cache.On("Get", UPGRADECACHEKEY).Return(cacheData, tc.HasCache)
cache.On("Set", testify_.Anything, testify_.Anything, testify_.Anything)

build.Version = tc.CurrentVersion

Expand Down
10 changes: 10 additions & 0 deletions website/docs/segments/system/upgrade.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ import Config from "@site/src/components/Config.js";
style: "plain",
foreground: "#111111",
background: "#FFD664",
properties: {
cache_duration: "168h",
},
}}
/>

## Properties

| Name | Type | Default | Description |
| ---------------- | :------: | :-----: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cache_duration` | `string` | `` | the duration for which the segment will be cached. The duration is a string in the format `1h2m3s`. The duration is parsed using the [time.ParseDuration] function from the Go standard library |

## Template ([info][templates])

:::note default template
Expand All @@ -39,3 +48,4 @@ import Config from "@site/src/components/Config.js";
| `.Latest` | `string` | the latest available version number |

[templates]: /docs/configuration/templates
[time.ParseDuration]: https://golang.org/pkg/time/#ParseDuration

0 comments on commit 63d79fe

Please sign in to comment.