oh-my-posh/src/upgrade/notice.go

78 lines
2.1 KiB
Go
Raw Normal View History

2023-03-03 11:21:35 -08:00
package upgrade
import (
"encoding/json"
"fmt"
"time"
2023-07-02 11:53:50 -07:00
"github.com/jandedobbeleer/oh-my-posh/src/build"
2023-03-03 11:21:35 -08:00
"github.com/jandedobbeleer/oh-my-posh/src/platform"
)
type release struct {
URL string `json:"url"`
HTMLURL string `json:"html_url"`
AssetsURL string `json:"assets_url"`
UploadURL string `json:"upload_url"`
TarballURL string `json:"tarball_url"`
ZipballURL string `json:"zipball_url"`
DiscussionURL string `json:"discussion_url"`
ID int `json:"id"`
NodeID string `json:"node_id"`
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string `json:"name"`
Body string `json:"body"`
Draft bool `json:"draft"`
Prerelease bool `json:"prerelease"`
CreatedAt time.Time `json:"created_at"`
PublishedAt time.Time `json:"published_at"`
}
const (
RELEASEURL = "https://api.github.com/repos/jandedobbeleer/oh-my-posh/releases/latest"
CACHEKEY = "upgrade_check"
2023-03-03 11:21:35 -08:00
)
2023-05-18 10:42:57 -07:00
func Latest(env platform.Environment) (string, error) {
body, err := env.HTTPRequest(RELEASEURL, nil, 1000)
2023-03-03 11:21:35 -08:00
if err != nil {
return "", err
}
var release release
// this can't fail
_ = json.Unmarshal(body, &release)
return release.TagName, nil
}
// Returns the upgrade notice if a new version is available
// that should be displayed to the user.
//
// The upgrade check is only performed every other week.
2024-05-31 07:02:34 -07:00
func Notice(env platform.Environment, force bool) (string, bool) {
2023-03-03 11:21:35 -08:00
// do not check when last validation was < 1 week ago
2024-05-31 07:02:34 -07:00
if _, OK := env.Cache().Get(CACHEKEY); OK && !force {
2023-03-03 11:21:35 -08:00
return "", false
}
// never validate when we install using the Windows Store
if env.Getenv("POSH_INSTALLER") == "ws" {
return "", false
}
2023-05-18 10:42:57 -07:00
latest, err := Latest(env)
2023-03-03 11:21:35 -08:00
if err != nil {
return "", false
}
oneWeek := 10080
env.Cache().Set(CACHEKEY, latest, oneWeek)
2023-03-03 11:21:35 -08:00
2023-07-02 11:53:50 -07:00
version := fmt.Sprintf("v%s", build.Version)
2023-03-03 11:21:35 -08:00
if latest == version {
return "", false
}
return fmt.Sprintf(upgradeNotice, version, latest), true
2023-03-03 11:21:35 -08:00
}