Skip to content

Commit

Permalink
feat(upgrade): select source
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Dec 7, 2024
1 parent 6068b56 commit 06a3724
Show file tree
Hide file tree
Showing 17 changed files with 316 additions and 213 deletions.
10 changes: 8 additions & 2 deletions src/cli/notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cli

import (
"fmt"
"os"

"github.com/jandedobbeleer/oh-my-posh/src/config"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"
"github.com/spf13/cobra"
)

Expand All @@ -23,7 +24,12 @@ var noticeCmd = &cobra.Command{
env.Init(flags)
defer env.Close()

if notice, hasNotice := upgrade.Notice(env, false); hasNotice {
sh := os.Getenv("POSH_SHELL")
configFile := config.Path(configFlag)
cfg := config.Load(configFile, sh, false)
cfg.Upgrade.Cache = env.Cache()

if notice, hasNotice := cfg.Upgrade.Notice(); hasNotice {
fmt.Println(notice)
}
},
Expand Down
29 changes: 18 additions & 11 deletions src/cli/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"slices"

"github.com/jandedobbeleer/oh-my-posh/src/build"
"github.com/jandedobbeleer/oh-my-posh/src/config"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/terminal"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"
Expand All @@ -32,45 +33,51 @@ var upgradeCmd = &cobra.Command{
return
}

sh := os.Getenv("POSH_SHELL")

env := &runtime.Terminal{}
env.Init(nil)
defer env.Close()

terminal.Init(env.Shell())
terminal.Init(sh)
fmt.Print(terminal.StartProgress())

latest, err := upgrade.Latest(env)
configFile := config.Path(configFlag)
cfg := config.Load(configFile, sh, false)
cfg.Upgrade.Cache = env.Cache()

latest, err := cfg.Upgrade.Latest()
if err != nil {
fmt.Printf("\n❌ %s\n\n%s", err, terminal.StopProgress())
os.Exit(1)
return
}

cfg.Upgrade.Version = fmt.Sprintf("v%s", latest)

if force {
executeUpgrade(latest)
executeUpgrade(cfg.Upgrade)
return
}

version := fmt.Sprintf("v%s", build.Version)

if upgrade.IsMajorUpgrade(version, latest) {
if upgrade.IsMajorUpgrade(build.Version, latest) {
message := terminal.StopProgress()
message += fmt.Sprintf("\n🚨 major upgrade available: %s -> %s, use oh-my-posh upgrade --force to upgrade\n\n", version, latest)
message += fmt.Sprintf("\n🚨 major upgrade available: v%s -> v%s, use oh-my-posh upgrade --force to upgrade\n\n", build.Version, latest)
fmt.Print(message)
return
}

if version != latest {
executeUpgrade(latest)
if build.Version != latest {
executeUpgrade(cfg.Upgrade)
return
}

fmt.Print(terminal.StopProgress())
},
}

func executeUpgrade(latest string) {
err := upgrade.Run(latest)
func executeUpgrade(cfg *upgrade.Config) {
err := upgrade.Run(cfg)
fmt.Print(terminal.StopProgress())
if err == nil {
return
Expand Down
16 changes: 9 additions & 7 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/jandedobbeleer/oh-my-posh/src/shell"
"github.com/jandedobbeleer/oh-my-posh/src/template"
"github.com/jandedobbeleer/oh-my-posh/src/terminal"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"
)

const (
Expand All @@ -30,26 +31,27 @@ type Config struct {
SecondaryPrompt *Segment `json:"secondary_prompt,omitempty" toml:"secondary_prompt,omitempty"`
TransientPrompt *Segment `json:"transient_prompt,omitempty" toml:"transient_prompt,omitempty"`
ErrorLine *Segment `json:"error_line,omitempty" toml:"error_line,omitempty"`
ConsoleTitleTemplate string `json:"console_title_template,omitempty" toml:"console_title_template,omitempty"`
Format string `json:"-" toml:"-"`
TerminalBackground color.Ansi `json:"terminal_background,omitempty" toml:"terminal_background,omitempty"`
origin string
PWD string `json:"pwd,omitempty" toml:"pwd,omitempty"`
AccentColor color.Ansi `json:"accent_color,omitempty" toml:"accent_color,omitempty"`
Output string `json:"-" toml:"-"`
TerminalBackground color.Ansi `json:"terminal_background,omitempty" toml:"terminal_background,omitempty"`
ConsoleTitleTemplate string `json:"console_title_template,omitempty" toml:"console_title_template,omitempty"`
Format string `json:"-" toml:"-"`
Upgrade *upgrade.Config `json:"upgrade,omitempty" toml:"upgrade,omitempty"`
Cycle color.Cycle `json:"cycle,omitempty" toml:"cycle,omitempty"`
ITermFeatures terminal.ITermFeatures `json:"iterm_features,omitempty" toml:"iterm_features,omitempty"`
Blocks []*Block `json:"blocks,omitempty" toml:"blocks,omitempty"`
Tooltips []*Segment `json:"tooltips,omitempty" toml:"tooltips,omitempty"`
Version int `json:"version" toml:"version"`
UpgradeNotice bool `json:"upgrade_notice,omitempty" toml:"upgrade_notice,omitempty"`
AutoUpgrade bool `json:"auto_upgrade,omitempty" toml:"auto_upgrade,omitempty"`
AutoUpgrade bool `json:"-" toml:"-"`
ShellIntegration bool `json:"shell_integration,omitempty" toml:"shell_integration,omitempty"`
MigrateGlyphs bool `json:"-" toml:"-"`
PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty" toml:"patch_pwsh_bleed,omitempty"`
EnableCursorPositioning bool `json:"enable_cursor_positioning,omitempty" toml:"enable_cursor_positioning,omitempty"`
updated bool
FinalSpace bool `json:"final_space,omitempty" toml:"final_space,omitempty"`
UpgradeNotice bool `json:"-" toml:"-"`
}

func (cfg *Config) MakeColors(env runtime.Environment) color.String {
Expand Down Expand Up @@ -98,12 +100,12 @@ func (cfg *Config) Features(env runtime.Environment) shell.Features {
feats = append(feats, shell.FTCSMarks)
}

autoUpgrade := cfg.AutoUpgrade
autoUpgrade := cfg.Upgrade.Auto
if _, OK := env.Cache().Get(AUTOUPGRADE); OK {
autoUpgrade = true
}

upgradeNotice := cfg.UpgradeNotice
upgradeNotice := cfg.Upgrade.DisplayNotice
if _, OK := env.Cache().Get(UPGRADENOTICE); OK {
upgradeNotice = true
}
Expand Down
14 changes: 14 additions & 0 deletions src/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/jandedobbeleer/oh-my-posh/src/log"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/path"
"github.com/jandedobbeleer/oh-my-posh/src/shell"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"

json "github.com/goccy/go-json"
yaml "github.com/goccy/go-yaml"
Expand All @@ -32,6 +33,19 @@ func Load(configFile, sh string, migrate bool) *Config {
cfg.BackupAndMigrate()
}

if cfg.Upgrade == nil {
cfg.Upgrade = &upgrade.Config{
Source: upgrade.CDN,
DisplayNotice: cfg.UpgradeNotice,
Auto: cfg.AutoUpgrade,
Interval: cache.ONEWEEK,
}
}

if cfg.Upgrade.Interval.IsEmpty() {
cfg.Upgrade.Interval = cache.ONEWEEK
}

if !cfg.ShellIntegration {
return cfg
}
Expand Down
14 changes: 10 additions & 4 deletions src/segments/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,29 @@ func (u *Upgrade) cachedLatest(current string) (*UpgradeCache, error) {
}

func (u *Upgrade) checkUpdate(current string) (*UpgradeCache, error) {
tag, err := upgrade.Latest(u.env)
duration := u.props.GetString(properties.CacheDuration, string(cache.ONEWEEK))
source := u.props.GetString(Source, string(upgrade.CDN))

cfg := &upgrade.Config{
Source: upgrade.Source(source),
Interval: cache.Duration(duration),
}

latest, err := cfg.Latest()
if err != nil {
return nil, err
}

latest := tag[1:]
cacheData := &UpgradeCache{
Latest: latest,
Current: current,
}

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
Expand Down
24 changes: 9 additions & 15 deletions src/segments/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package segments

import (
"errors"
"fmt"
"testing"

Expand All @@ -16,8 +15,10 @@ import (
)

func TestUpgrade(t *testing.T) {
ugc := &upgrade.Config{}
latest, _ := ugc.Latest()

cases := []struct {
Error error
Case string
CurrentVersion string
LatestVersion string
Expand All @@ -33,33 +34,28 @@ func TestUpgrade(t *testing.T) {
},
{
Case: "On latest",
CurrentVersion: "1.0.1",
LatestVersion: "1.0.1",
},
{
Case: "Error on update check",
Error: errors.New("error"),
CurrentVersion: latest,
},
{
Case: "On previous, from cache",
HasCache: true,
CurrentVersion: "1.0.2",
LatestVersion: "1.0.3",
LatestVersion: latest,
CachedVersion: "1.0.2",
ExpectedEnabled: true,
},
{
Case: "On latest, version changed",
HasCache: true,
CurrentVersion: "1.0.2",
LatestVersion: "1.0.2",
CurrentVersion: latest,
LatestVersion: latest,
CachedVersion: "1.0.1",
},
{
Case: "On previous, version changed",
HasCache: true,
CurrentVersion: "1.0.2",
LatestVersion: "1.0.3",
LatestVersion: latest,
CachedVersion: "1.0.1",
ExpectedEnabled: true,
},
Expand All @@ -73,15 +69,13 @@ func TestUpgrade(t *testing.T) {
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

json := fmt.Sprintf(`{"tag_name":"v%s"}`, tc.LatestVersion)
env.On("HTTPRequest", upgrade.RELEASEURL).Return([]byte(json), tc.Error)

ug := &Upgrade{}
ug.Init(properties.Map{}, env)

Expand Down
48 changes: 24 additions & 24 deletions src/upgrade/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,40 @@ type stateMsg state

type model struct {
error error
config *Config
message string
tag string
spinner spinner.Model
state state
}

func initialModel(tag string) *model {
func initialModel(cfg *Config) *model {
s := spinner.New()
s.Spinner = spinner.Dot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color("170"))
return &model{spinner: s, tag: tag}
return &model{spinner: s, config: cfg}
}

func (m *model) Init() tea.Cmd {
defer func() {
go func() {
if err := install(m.tag); err != nil {
m.error = err
program.Send(resultMsg(fmt.Sprintf("❌ upgrade failed: %v", err)))
return
}
go m.start()

message := "🚀 Upgrade successful"
return m.spinner.Tick
}

current := fmt.Sprintf("v%s", build.Version)
if current != m.tag {
message += ", restart your shell to take full advantage of the new functionality"
}
func (m *model) start() {
if err := install(m.config); err != nil {
m.error = err
program.Send(resultMsg(fmt.Sprintf("❌ upgrade failed: %v", err)))
return
}

program.Send(resultMsg(message))
}()
}()
message := "🚀 Upgrade successful"

return m.spinner.Tick
current := fmt.Sprintf("v%s", build.Version)
if current != m.config.Version {
message += ", restart your shell to take full advantage of the new functionality"
}

program.Send(resultMsg(message))
}

func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand Down Expand Up @@ -113,7 +113,7 @@ func (m *model) View() string {
message = "Validating current installation"
case downloading:
m.spinner.Spinner = spinner.Globe
message = "Downloading latest version"
message = fmt.Sprintf("Downloading latest version from %s", m.config.Source.String())
case verifying:
m.spinner.Spinner = spinner.Moon
message = "Verifying download"
Expand All @@ -125,19 +125,19 @@ func (m *model) View() string {
return title + textStyle.Render(fmt.Sprintf("%s %s", m.spinner.View(), message))
}

func Run(latest string) error {
func Run(cfg *Config) error {
titleStyle := lipgloss.NewStyle().Margin(1, 0, 1, 0)
title = "📦 Upgrading Oh My Posh"

current := build.Version
current := fmt.Sprintf("v%s", build.Version)
if len(current) == 0 {
current = "dev"
}

title = fmt.Sprintf("%s from %s to %s", title, current, latest)
title = fmt.Sprintf("%s from %s to %s", title, current, cfg.Version)
title = titleStyle.Render(title)

program = tea.NewProgram(initialModel(latest))
program = tea.NewProgram(initialModel(cfg))
resultModel, _ := program.Run()

programModel, OK := resultModel.(*model)
Expand Down
Loading

0 comments on commit 06a3724

Please sign in to comment.