Repository: JanDeDobbeleer/oh-my-posh
Pull request #3581 (2023-03-13 19:07:00)

feat(notice): disable/enable upgrade notice
feat(notice): disable/enable upgrade notice



Patch:
diff --git a/src/cli/disable.go b/src/cli/disable.go
new file mode 100644
index 00000000..47e3e3c2
--- /dev/null
+++ b/src/cli/disable.go
@@ -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)
+}
diff --git a/src/cli/enable.go b/src/cli/enable.go
new file mode 100644
index 00000000..62a359f4
--- /dev/null
+++ b/src/cli/enable.go
@@ -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()
+	}
+}
diff --git a/src/cli/root.go b/src/cli/root.go
index a85bc56e..682b340f 100644
--- a/src/cli/root.go
+++ b/src/cli/root.go
@@ -50,7 +50,7 @@ var (
 )
 
 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().BoolVar(&displayVersion, "version", false, "version")
 	RootCmd.Flags().StringVarP(&shellName, "shell", "s", "", "shell (deprecated)")
diff --git a/src/mock/cache.go b/src/mock/cache.go
index e033ded0..b1e79f4d 100644
--- a/src/mock/cache.go
+++ b/src/mock/cache.go
@@ -42,3 +42,8 @@ func (_m *MockedCache) Init(home string) {
 func (_m *MockedCache) Set(key, value string, ttl int) {
 	_m.Called(key, value, ttl)
 }
+
+// delete provides a mock function with given fields: key
+func (_m *MockedCache) Delete(key string) {
+	_m.Called(key)
+}
diff --git a/src/platform/cache.go b/src/platform/cache.go
index 96341bd0..c80ea4f7 100644
--- a/src/platform/cache.go
+++ b/src/platform/cache.go
@@ -85,3 +85,9 @@ func (fc *fileCache) Set(key, value string, ttl int) {
 	})
 	fc.dirty = true
 }
+
+// delete the key from the cache
+func (fc *fileCache) Delete(key string) {
+	fc.cache.Delete(key)
+	fc.dirty = true
+}
diff --git a/src/platform/concurrent_map.go b/src/platform/concurrent_map.go
index 5a8c47d4..696c8345 100644
--- a/src/platform/concurrent_map.go
+++ b/src/platform/concurrent_map.go
@@ -28,6 +28,12 @@ func (c *ConcurrentMap) Get(key string) (interface{}, bool) {
 	return "", false
 }
 
+func (c *ConcurrentMap) Delete(key string) {
+	c.RLock()
+	defer c.RUnlock()
+	delete(c.values, key)
+}
+
 func (c *ConcurrentMap) List() map[string]interface{} {
 	return c.values
 }
diff --git a/src/platform/shell.go b/src/platform/shell.go
index 5f28f359..406890df 100644
--- a/src/platform/shell.go
+++ b/src/platform/shell.go
@@ -98,6 +98,8 @@ type Cache interface {
 	// Sets a value for a given key.
 	// The ttl indicates how may minutes to cache the value.
 	Set(key, value string, ttl int)
+	// Deletes a key from the cache.
+	Delete(key string)
 }
 
 type HTTPRequestModifier func(request *http.Request)
diff --git a/src/upgrade/notice.go b/src/upgrade/notice.go
index 96e70a24..9b6e3592 100644
--- a/src/upgrade/notice.go
+++ b/src/upgrade/notice.go
@@ -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'"
 	darwin = "To upgrade, run: 'brew upgrade oh-my-posh'"
 
-	UPGRADECACHEKEY = "upgrade_check"
+	CACHEKEY = "upgrade_check"
 )
 
 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.
 func Notice(env platform.Environment) (string, bool) {
 	// 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
 	}
 
@@ -69,7 +69,7 @@ func Notice(env platform.Environment) (string, bool) {
 	}
 
 	oneWeek := 10080
-	env.Cache().Set(UPGRADECACHEKEY, latest, oneWeek)
+	env.Cache().Set(CACHEKEY, latest, oneWeek)
 
 	version := fmt.Sprintf("v%s", env.Flags().Version)
 	if latest == version {
diff --git a/website/docs/faq.mdx b/website/docs/faq.mdx
index fe83d163..7ebdc12e 100644
--- a/website/docs/faq.mdx
+++ b/website/docs/faq.mdx
@@ -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.
 
+### 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)
 
 :::tip Windows


Test patch:
diff --git a/src/upgrade/notice_test.go b/src/upgrade/notice_test.go
index e52d0c4f..410fc139 100644
--- a/src/upgrade/notice_test.go
+++ b/src/upgrade/notice_test.go
@@ -35,7 +35,7 @@ func TestCanUpgrade(t *testing.T) {
 			Version: tc.CurrentVersion,
 		})
 		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)
 		env.On("Cache").Return(cache)
 		env.On("GOOS").Return(tc.GOOS)