fix(upgrade): enable for all platforms

resolves #5184
This commit is contained in:
Jan De Dobbeleer 2024-06-29 12:07:04 +02:00 committed by Jan De Dobbeleer
parent dc002fc1e1
commit 3537f3fd49
11 changed files with 60 additions and 138 deletions

View file

@ -73,7 +73,6 @@ func runInit(shellName string) {
shell.Tooltips = len(cfg.Tooltips) > 0
shell.ShellIntegration = cfg.ShellIntegration
shell.PromptMark = shellName == shell.FISH && cfg.ITermFeatures != nil && cfg.ITermFeatures.Contains(ansi.PromptMark)
shell.AutoUpgrade = cfg.AutoUpgrade && upgrade.Supported
for i, block := range cfg.Blocks {
// only fetch cursor position when relevant

View file

@ -3,6 +3,7 @@ package cli
import (
"fmt"
"runtime"
"slices"
"github.com/jandedobbeleer/oh-my-posh/src/engine"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
@ -19,7 +20,13 @@ var upgradeCmd = &cobra.Command{
Long: "Upgrade when a new version is available.",
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, _ []string) {
if runtime.GOOS == platform.LINUX {
supportedPlatforms := []string{
platform.WINDOWS,
platform.DARWIN,
platform.LINUX,
}
if !slices.Contains(supportedPlatforms, runtime.GOOS) {
fmt.Print("\n⚠ upgrade is not supported on this platform\n\n")
return
}

View file

@ -10,7 +10,7 @@ require (
github.com/alecthomas/repr v0.4.0 // indirect
github.com/esimov/stackblur-go v1.1.0
github.com/fogleman/gg v1.3.0
github.com/google/uuid v1.6.0
github.com/google/uuid v1.6.0 // indirect
github.com/gookit/color v1.5.4
github.com/huandu/xstrings v1.5.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
@ -35,6 +35,7 @@ require (
github.com/goccy/go-yaml v1.11.3
github.com/gookit/goutil v0.6.15
github.com/hashicorp/hcl/v2 v2.21.0
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
github.com/mattn/go-runewidth v0.0.15
github.com/pelletier/go-toml/v2 v2.2.2
github.com/spf13/cobra v1.8.1

View file

@ -86,6 +86,8 @@ github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8=
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf/go.mod h1:hyb9oH7vZsitZCiBt0ZvifOrB+qc8PS5IiilCIb87rg=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jandedobbeleer/clipboard v0.1.4-1 h1:rJehm5W0a3hvjcxyB3snqLBV4yvMBBc12JyMP7ngNQw=

View file

@ -1,5 +1,3 @@
//go:build windows || darwin
package upgrade
import (
@ -23,16 +21,6 @@ var (
title string
)
const (
upgradeNotice = `
A new release of Oh My Posh is available: %s %s
To upgrade, run: 'oh-my-posh upgrade'
To enable automated upgrades, set 'auto_upgrade' to 'true' in your configuration.
`
Supported = true
)
type resultMsg string
type state int
@ -71,7 +59,7 @@ func (m *model) Init() tea.Cmd {
return
}
program.Send(resultMsg(successMsg))
program.Send(resultMsg("🚀 Upgrade successful, restart your shell to take full advantage of the new functionality."))
}()
}()

View file

@ -1,46 +0,0 @@
package upgrade
import (
"errors"
"fmt"
"io"
"os"
"runtime"
)
var successMsg = "🚀 Upgrade successful, restart your shell to take full advantage of the new functionality."
func install() error {
setState(validating)
executable, err := os.Executable()
if err != nil {
return err
}
file, err := os.OpenFile(executable, os.O_WRONLY, 0755)
if err != nil {
return errors.New("we don't have permissions to upgrade oh-my-posh, please use elevated permissions to upgrade")
}
defer file.Close()
setState(downloading)
asset := fmt.Sprintf("posh-%s-%s", runtime.GOOS, runtime.GOARCH)
data, err := downloadAsset(asset)
if err != nil {
return err
}
defer data.Close()
setState(installing)
_, err = io.Copy(file, data)
if err != nil {
return err
}
return nil
}

View file

@ -1,19 +0,0 @@
//go:build linux || freebsd || openbsd || netbsd
package upgrade
import "github.com/jandedobbeleer/oh-my-posh/src/platform"
const (
upgradeNotice = `
A new release of Oh My Posh is available: %s %s
To upgrade, use your favorite package manager or, if you used Homebrew to install, run: 'brew update; brew upgrade oh-my-posh'
`
)
func Run(_ platform.Environment) {}
const (
Supported = false
)

View file

@ -1,56 +0,0 @@
package upgrade
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/google/uuid"
)
var successMsg = "Oh My Posh is installing in the background.\nRestart your shell in a minute to take full advantage of the new functionality."
func install() error {
setState(downloading)
temp := os.Getenv("TEMP")
if len(temp) == 0 {
return errors.New("failed to get TEMP environment variable")
}
id := uuid.New().String()
fileName := fmt.Sprintf("oh-my-posh-install-%s.exe", id)
path := filepath.Join(temp, fileName)
file, err := os.Create(path)
if err != nil {
return err
}
asset := fmt.Sprintf("install-%s.exe", runtime.GOARCH)
data, err := downloadAsset(asset)
if err != nil {
return err
}
_, err = io.Copy(file, data)
if err != nil {
return err
}
data.Close()
file.Close()
// We need to run the installer in a separate process to avoid being blocked by the current process
go func() {
cmd := exec.Command(path, "/VERYSILENT", "/FORCECLOSEAPPLICATIONS")
_ = cmd.Run()
}()
return nil
}

39
src/upgrade/install.go Normal file
View file

@ -0,0 +1,39 @@
package upgrade
import (
"fmt"
"os"
"runtime"
"github.com/inconshreveable/go-update"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
)
func install() error {
setState(validating)
executable, err := os.Executable()
if err != nil {
return err
}
extension := ""
if runtime.GOOS == platform.WINDOWS {
extension = ".exe"
}
asset := fmt.Sprintf("posh-%s-%s%s", runtime.GOOS, runtime.GOARCH, extension)
setState(downloading)
data, err := downloadAsset(asset)
if err != nil {
return err
}
defer data.Close()
return update.Apply(data, update.Options{
TargetPath: executable,
})
}

View file

@ -32,6 +32,13 @@ type release struct {
const (
RELEASEURL = "https://api.github.com/repos/jandedobbeleer/oh-my-posh/releases/latest"
CACHEKEY = "upgrade_check"
upgradeNotice = `
A new release of Oh My Posh is available: %s %s
To upgrade, run: 'oh-my-posh upgrade'
To enable automated upgrades, set 'auto_upgrade' to 'true' in your configuration.
`
)
func Latest(env platform.Environment) (string, error) {

View file

@ -137,7 +137,7 @@ For example, the following is a valid `--config` flag:
| `disable_cursor_positioning` | `boolean` | disable fetching the cursor position in bash and zsh in case of unwanted side-effects |
| `patch_pwsh_bleed` | `boolean` | patch a PowerShell bug where the background colors bleed into the next line at the end of the buffer (can be removed when [this][pwsh-bleed] is merged) |
| `disable_notice` | `boolean` | disables the notice that a new upgrade is available when `auto_upgrade` is disabled, and the notice that no new upgrade is available when `auto_upgrade` is enabled |
| `auto_upgrade` | `boolean` | enable automatic upgrades for Oh My Posh (supports Windows/macOS only) |
| `auto_upgrade` | `boolean` | enable automatic upgrades for Oh My Posh (supports Windows, macOS and Linux) |
| `iterm_features` | `[]string` | enable iTerm2 specific features:<ul><li>`prompt_mark`: add the `iterm2_prompt_mark` [function][iterm2-si] for supported shells</li><li>`current_dir`: expose the current directory for iTerm2</li><li>`remote_host`: expose the current remote and user for iTerm2</li></ul> |
### JSON Schema Validation