feat(notice): disable/enable upgrade notice

This commit is contained in:
Jan De Dobbeleer 2023-03-13 20:13:46 +01:00 committed by Jan De Dobbeleer
parent 9f70320525
commit 71f81a0b79
10 changed files with 128 additions and 5 deletions

27
src/cli/disable.go Normal file
View file

@ -0,0 +1,27 @@
package cli
import (
"fmt"
"github.com/spf13/cobra"
)
// getCmd represents the get command
var disableCmd = &cobra.Command{
Use: fmt.Sprintf(toggleUse, "disable"),
Short: "Disable a feature",
Long: fmt.Sprintf(toggleLong, "Disable", "disable"),
ValidArgs: toggleArgs,
Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
toggleFeature(cmd, args[0], false)
},
}
func init() { //nolint:gochecknoinits
RootCmd.AddCommand(disableCmd)
}

63
src/cli/enable.go Normal file
View file

@ -0,0 +1,63 @@
package cli
import (
"fmt"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"
"github.com/spf13/cobra"
)
var (
toggleUse = "%s [notice]"
toggleLong = `%s a feature
This command is used to %s one of the following features:
- notice`
toggleArgs = []string{
"notice",
}
)
// getCmd represents the get command
var enableCmd = &cobra.Command{
Use: fmt.Sprintf(toggleUse, "enable"),
Short: "Enable a feature",
Long: fmt.Sprintf(toggleLong, "Enable", "enable"),
ValidArgs: toggleArgs,
Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
toggleFeature(cmd, args[0], true)
},
}
func init() { //nolint:gochecknoinits
RootCmd.AddCommand(enableCmd)
}
func toggleFeature(cmd *cobra.Command, feature string, enable bool) {
env := &platform.Shell{
CmdFlags: &platform.Flags{
Shell: shellName,
Version: cliVersion,
},
}
env.Init()
defer env.Close()
switch feature {
case "notice":
if enable {
env.Cache().Delete(upgrade.CACHEKEY)
return
}
env.Cache().Set(upgrade.CACHEKEY, "disabled", -1)
default:
_ = cmd.Help()
}
}

View file

@ -50,7 +50,7 @@ var (
) )
func init() { //nolint:gochecknoinits func init() { //nolint:gochecknoinits
RootCmd.PersistentFlags().StringVarP(&config, "config", "c", "", "config (required)") RootCmd.PersistentFlags().StringVarP(&config, "config", "c", "", "config file path")
RootCmd.Flags().BoolVarP(&initialize, "init", "i", false, "init (deprecated)") RootCmd.Flags().BoolVarP(&initialize, "init", "i", false, "init (deprecated)")
RootCmd.Flags().BoolVar(&displayVersion, "version", false, "version") RootCmd.Flags().BoolVar(&displayVersion, "version", false, "version")
RootCmd.Flags().StringVarP(&shellName, "shell", "s", "", "shell (deprecated)") RootCmd.Flags().StringVarP(&shellName, "shell", "s", "", "shell (deprecated)")

View file

@ -42,3 +42,8 @@ func (_m *MockedCache) Init(home string) {
func (_m *MockedCache) Set(key, value string, ttl int) { func (_m *MockedCache) Set(key, value string, ttl int) {
_m.Called(key, value, ttl) _m.Called(key, value, ttl)
} }
// delete provides a mock function with given fields: key
func (_m *MockedCache) Delete(key string) {
_m.Called(key)
}

View file

@ -85,3 +85,9 @@ func (fc *fileCache) Set(key, value string, ttl int) {
}) })
fc.dirty = true fc.dirty = true
} }
// delete the key from the cache
func (fc *fileCache) Delete(key string) {
fc.cache.Delete(key)
fc.dirty = true
}

View file

@ -28,6 +28,12 @@ func (c *ConcurrentMap) Get(key string) (interface{}, bool) {
return "", false return "", false
} }
func (c *ConcurrentMap) Delete(key string) {
c.RLock()
defer c.RUnlock()
delete(c.values, key)
}
func (c *ConcurrentMap) List() map[string]interface{} { func (c *ConcurrentMap) List() map[string]interface{} {
return c.values return c.values
} }

View file

@ -98,6 +98,8 @@ type Cache interface {
// Sets a value for a given key. // Sets a value for a given key.
// The ttl indicates how may minutes to cache the value. // The ttl indicates how may minutes to cache the value.
Set(key, value string, ttl int) Set(key, value string, ttl int)
// Deletes a key from the cache.
Delete(key string)
} }
type HTTPRequestModifier func(request *http.Request) type HTTPRequestModifier func(request *http.Request)

View file

@ -39,7 +39,7 @@ https://ohmyposh.dev/docs/installation/windows#update`
unix = "To upgrade, use your favorite package manager or, if you used Homebrew to install, run: 'brew upgrade oh-my-posh'" unix = "To upgrade, use your favorite package manager or, if you used Homebrew to install, run: 'brew upgrade oh-my-posh'"
darwin = "To upgrade, run: 'brew upgrade oh-my-posh'" darwin = "To upgrade, run: 'brew upgrade oh-my-posh'"
UPGRADECACHEKEY = "upgrade_check" CACHEKEY = "upgrade_check"
) )
func getLatestVersion(env platform.Environment) (string, error) { func getLatestVersion(env platform.Environment) (string, error) {
@ -59,7 +59,7 @@ func getLatestVersion(env platform.Environment) (string, error) {
// The upgrade check is only performed every other week. // The upgrade check is only performed every other week.
func Notice(env platform.Environment) (string, bool) { func Notice(env platform.Environment) (string, bool) {
// do not check when last validation was < 1 week ago // do not check when last validation was < 1 week ago
if _, OK := env.Cache().Get(UPGRADECACHEKEY); OK { if _, OK := env.Cache().Get(CACHEKEY); OK {
return "", false return "", false
} }
@ -69,7 +69,7 @@ func Notice(env platform.Environment) (string, bool) {
} }
oneWeek := 10080 oneWeek := 10080
env.Cache().Set(UPGRADECACHEKEY, latest, oneWeek) env.Cache().Set(CACHEKEY, latest, oneWeek)
version := fmt.Sprintf("v%s", env.Flags().Version) version := fmt.Sprintf("v%s", env.Flags().Version)
if latest == version { if latest == version {

View file

@ -35,7 +35,7 @@ func TestCanUpgrade(t *testing.T) {
Version: tc.CurrentVersion, Version: tc.CurrentVersion,
}) })
cache := &mock.MockedCache{} cache := &mock.MockedCache{}
cache.On("Get", UPGRADECACHEKEY).Return("", tc.Cache) cache.On("Get", CACHEKEY).Return("", tc.Cache)
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything) cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
env.On("Cache").Return(cache) env.On("Cache").Return(cache)
env.On("GOOS").Return(tc.GOOS) env.On("GOOS").Return(tc.GOOS)

View file

@ -9,6 +9,20 @@ import TabItem from "@theme/TabItem";
Before validating anything, make sure you're on the [latest version][latest] of Oh My Posh and your terminal and shell are up-to-date. Before validating anything, make sure you're on the [latest version][latest] of Oh My Posh and your terminal and shell are up-to-date.
### I want to disable the upgrade notice
You can disable the upgrade notice using the following command once:
```bash
oh-my-posh disable notice
```
If you want to enable it again, run:
```bash
oh-my-posh enable notice
```
### The prompt is slow (delay in showing the prompt between commands) ### The prompt is slow (delay in showing the prompt between commands)
:::tip Windows :::tip Windows