From 914bffc0a7c8baed3981b55307d6f7f22742b2c9 Mon Sep 17 00:00:00 2001 From: Othmane EL MASSARI <47815944+othmane399@users.noreply.github.com> Date: Wed, 7 Feb 2024 17:19:48 +0100 Subject: [PATCH] feat: allow override of plugins & hotkeys shortcuts (#2510) * feat: allow override of plugins & hotkeys shortcuts * add docs and log.info when overrides * edit log message * fix doc * Update README.md --- README.md | 9 ++++++--- internal/config/hotkey.go | 1 + internal/config/json/schemas/hotkeys.json | 1 + internal/config/json/schemas/plugins.json | 1 + internal/config/plugin.go | 1 + internal/view/actions.go | 9 +++++++-- 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8b63cf16..efb02d31 100644 --- a/README.md +++ b/README.md @@ -532,9 +532,10 @@ In order to surface hotkeys globally please follow these steps: shortCut: Shift-2 description: Xray Deployments command: xray deploy - # Hitting Ctrl-U view the resources in the namespace of your current selection - ctrl-u: - shortCut: Ctrl-U + # Hitting Shift-S view the resources in the namespace of your current selection + shift-s: + shortCut: Shift-S + override: true # => will override the default shortcut related action if set to true (default to false) description: Namespaced resources command: "$RESOURCE_NAME $NAMESPACE" keepHistory: true # whether you can return to the previous view @@ -642,6 +643,7 @@ K9s allows you to extend your command line and tooling by defining your very own A plugin is defined as follows: * Shortcut option represents the key combination a user would type to activate the plugin +* Override option make that the default action related to the shortcut will be overrided by the plugin * Confirm option (when enabled) lets you see the command that is going to be executed and gives you an option to confirm or prevent execution * Description will be printed next to the shortcut in the k9s menu * Scopes defines a collection of resources names/short-names for the views associated with the plugin. You can specify `all` to provide this shortcut for all views. @@ -678,6 +680,7 @@ plugins: # Defines a plugin to provide a `ctrl-l` shortcut to tail the logs while in pod view. fred: shortCut: Ctrl-L + override: false confirm: false description: Pod logs scopes: diff --git a/internal/config/hotkey.go b/internal/config/hotkey.go index 4aec92ac..91801eb8 100644 --- a/internal/config/hotkey.go +++ b/internal/config/hotkey.go @@ -20,6 +20,7 @@ type HotKeys struct { // HotKey describes a K9s hotkey. type HotKey struct { ShortCut string `yaml:"shortCut"` + Override bool `yaml:"override"` Description string `yaml:"description"` Command string `yaml:"command"` KeepHistory bool `yaml:"keepHistory"` diff --git a/internal/config/json/schemas/hotkeys.json b/internal/config/json/schemas/hotkeys.json index 627730ff..49906422 100644 --- a/internal/config/json/schemas/hotkeys.json +++ b/internal/config/json/schemas/hotkeys.json @@ -10,6 +10,7 @@ "type": "object", "properties": { "shortCut": {"type": "string"}, + "override": { "type": "boolean" }, "description": {"type": "string"}, "command": {"type": "string"}, "keepHistory": {"type": "boolean"} diff --git a/internal/config/json/schemas/plugins.json b/internal/config/json/schemas/plugins.json index 3445bd16..5c41eb48 100644 --- a/internal/config/json/schemas/plugins.json +++ b/internal/config/json/schemas/plugins.json @@ -11,6 +11,7 @@ "additionalProperties": false, "properties": { "shortCut": { "type": "string" }, + "override": { "type": "boolean" }, "description": { "type": "string" }, "confirm": { "type": "boolean" }, "scopes": { diff --git a/internal/config/plugin.go b/internal/config/plugin.go index 7b111294..e0992fe5 100644 --- a/internal/config/plugin.go +++ b/internal/config/plugin.go @@ -29,6 +29,7 @@ type Plugin struct { Scopes []string `yaml:"scopes"` Args []string `yaml:"args"` ShortCut string `yaml:"shortCut"` + Override bool `yaml:"override"` Pipes []string `yaml:"pipes"` Description string `yaml:"description"` Command string `yaml:"command"` diff --git a/internal/view/actions.go b/internal/view/actions.go index 5cbe9f24..2a1f8b1c 100644 --- a/internal/view/actions.go +++ b/internal/view/actions.go @@ -76,9 +76,11 @@ func hotKeyActions(r Runner, aa ui.KeyActions) error { continue } _, ok := aa[key] - if ok { + if ok && !hk.Override { errs = errors.Join(errs, fmt.Errorf("duplicated hotkeys found for %q in %q", hk.ShortCut, k)) continue + } else if ok && hk.Override == true { + log.Info().Msgf("Action %q has been overrided by hotkey in %q", hk.ShortCut, k) } command, err := r.EnvFn()().Substitute(hk.Command) @@ -125,14 +127,17 @@ func pluginActions(r Runner, aa ui.KeyActions) error { continue } key, err := asKey(plugin.ShortCut) + if err != nil { errs = errors.Join(errs, err) continue } _, ok := aa[key] - if ok { + if ok && !plugin.Override { errs = errors.Join(errs, fmt.Errorf("duplicated plugin key found for %q in %q", plugin.ShortCut, k)) continue + } else if ok && plugin.Override == true { + log.Info().Msgf("Action %q has been overrided by plugin in %q", plugin.ShortCut, k) } aa[key] = ui.NewKeyActionWithOpts( plugin.Description,