feat(upgrade): expose current version number in segment

This commit is contained in:
Jan De Dobbeleer 2023-11-29 09:30:01 +01:00 committed by Jan De Dobbeleer
parent 5313c1c36c
commit 230f8f4973
2 changed files with 24 additions and 18 deletions

View file

@ -2,6 +2,7 @@ package segments
import ( import (
"encoding/json" "encoding/json"
"errors"
"github.com/jandedobbeleer/oh-my-posh/src/build" "github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/platform" "github.com/jandedobbeleer/oh-my-posh/src/platform"
@ -18,7 +19,10 @@ type Upgrade struct {
props properties.Properties props properties.Properties
env platform.Environment env platform.Environment
// deprecated
Version string Version string
UpgradeCache
} }
const UPGRADECACHEKEY = "upgrade_segment" const UPGRADECACHEKEY = "upgrade_segment"
@ -33,43 +37,44 @@ func (u *Upgrade) Init(props properties.Properties, env platform.Environment) {
} }
func (u *Upgrade) Enabled() bool { func (u *Upgrade) Enabled() bool {
current := build.Version u.Current = build.Version
latest := u.cachedLatest(current) latest, err := u.cachedLatest(u.Current)
if len(latest) == 0 { if err != nil {
latest = u.checkUpdate(current) latest, err = u.checkUpdate(u.Current)
} }
if len(latest) == 0 || current == latest { if err != nil || u.Current == latest.Latest {
return false return false
} }
u.Version = latest u.UpgradeCache = *latest
u.Version = u.Latest
return true return true
} }
func (u *Upgrade) cachedLatest(current string) string { func (u *Upgrade) cachedLatest(current string) (*UpgradeCache, error) {
data, ok := u.env.Cache().Get(UPGRADECACHEKEY) data, ok := u.env.Cache().Get(UPGRADECACHEKEY)
if !ok { if !ok {
return "" // no cache return nil, errors.New("no cache data")
} }
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 "" // invalid cache data return nil, err // invalid cache data
} }
if current != cacheJSON.Current { if current != cacheJSON.Current {
return "" // version changed, run the check again return nil, errors.New("version changed, run the check again")
} }
return cacheJSON.Latest return &cacheJSON, nil
} }
func (u *Upgrade) checkUpdate(current string) string { func (u *Upgrade) checkUpdate(current string) (*UpgradeCache, error) {
tag, err := upgrade.Latest(u.env) tag, err := upgrade.Latest(u.env)
if err != nil { if err != nil {
return "" return nil, err
} }
latest := tag[1:] latest := tag[1:]
@ -79,7 +84,7 @@ func (u *Upgrade) checkUpdate(current string) string {
} }
cacheJSON, err := json.Marshal(cacheData) cacheJSON, err := json.Marshal(cacheData)
if err != nil { if err != nil {
return "" return nil, err
} }
oneWeek := 10080 oneWeek := 10080
@ -87,5 +92,5 @@ func (u *Upgrade) checkUpdate(current string) string {
// update cache // update cache
u.env.Cache().Set(UPGRADECACHEKEY, string(cacheJSON), cacheTimeout) u.env.Cache().Set(UPGRADECACHEKEY, string(cacheJSON), cacheTimeout)
return latest return cacheData, nil
} }

View file

@ -39,8 +39,9 @@ import Config from "@site/src/components/Config.js";
### Properties ### Properties
| Name | Type | Description | | Name | Type | Description |
| ---------- | -------- | ---------------------- | | ---------- | -------- | ----------------------------------- |
| `.Version` | `string` | the new version number | | `.Current` | `string` | the current version number |
| `.Latest` | `string` | the latest available version number |
[templates]: /docs/configuration/templates [templates]: /docs/configuration/templates