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
mine
Othmane EL MASSARI 2024-02-07 17:19:48 +01:00 committed by GitHub
parent 9ca46ab027
commit 914bffc0a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 17 additions and 5 deletions

View File

@ -532,9 +532,10 @@ In order to surface hotkeys globally please follow these steps:
shortCut: Shift-2 shortCut: Shift-2
description: Xray Deployments description: Xray Deployments
command: xray deploy command: xray deploy
# Hitting Ctrl-U view the resources in the namespace of your current selection # Hitting Shift-S view the resources in the namespace of your current selection
ctrl-u: shift-s:
shortCut: Ctrl-U shortCut: Shift-S
override: true # => will override the default shortcut related action if set to true (default to false)
description: Namespaced resources description: Namespaced resources
command: "$RESOURCE_NAME $NAMESPACE" command: "$RESOURCE_NAME $NAMESPACE"
keepHistory: true # whether you can return to the previous view 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: A plugin is defined as follows:
* Shortcut option represents the key combination a user would type to activate the plugin * 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 * 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 * 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. * 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. # Defines a plugin to provide a `ctrl-l` shortcut to tail the logs while in pod view.
fred: fred:
shortCut: Ctrl-L shortCut: Ctrl-L
override: false
confirm: false confirm: false
description: Pod logs description: Pod logs
scopes: scopes:

View File

@ -20,6 +20,7 @@ type HotKeys struct {
// HotKey describes a K9s hotkey. // HotKey describes a K9s hotkey.
type HotKey struct { type HotKey struct {
ShortCut string `yaml:"shortCut"` ShortCut string `yaml:"shortCut"`
Override bool `yaml:"override"`
Description string `yaml:"description"` Description string `yaml:"description"`
Command string `yaml:"command"` Command string `yaml:"command"`
KeepHistory bool `yaml:"keepHistory"` KeepHistory bool `yaml:"keepHistory"`

View File

@ -10,6 +10,7 @@
"type": "object", "type": "object",
"properties": { "properties": {
"shortCut": {"type": "string"}, "shortCut": {"type": "string"},
"override": { "type": "boolean" },
"description": {"type": "string"}, "description": {"type": "string"},
"command": {"type": "string"}, "command": {"type": "string"},
"keepHistory": {"type": "boolean"} "keepHistory": {"type": "boolean"}

View File

@ -11,6 +11,7 @@
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"shortCut": { "type": "string" }, "shortCut": { "type": "string" },
"override": { "type": "boolean" },
"description": { "type": "string" }, "description": { "type": "string" },
"confirm": { "type": "boolean" }, "confirm": { "type": "boolean" },
"scopes": { "scopes": {

View File

@ -29,6 +29,7 @@ type Plugin struct {
Scopes []string `yaml:"scopes"` Scopes []string `yaml:"scopes"`
Args []string `yaml:"args"` Args []string `yaml:"args"`
ShortCut string `yaml:"shortCut"` ShortCut string `yaml:"shortCut"`
Override bool `yaml:"override"`
Pipes []string `yaml:"pipes"` Pipes []string `yaml:"pipes"`
Description string `yaml:"description"` Description string `yaml:"description"`
Command string `yaml:"command"` Command string `yaml:"command"`

View File

@ -76,9 +76,11 @@ func hotKeyActions(r Runner, aa ui.KeyActions) error {
continue continue
} }
_, ok := aa[key] _, 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)) errs = errors.Join(errs, fmt.Errorf("duplicated hotkeys found for %q in %q", hk.ShortCut, k))
continue 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) command, err := r.EnvFn()().Substitute(hk.Command)
@ -125,14 +127,17 @@ func pluginActions(r Runner, aa ui.KeyActions) error {
continue continue
} }
key, err := asKey(plugin.ShortCut) key, err := asKey(plugin.ShortCut)
if err != nil { if err != nil {
errs = errors.Join(errs, err) errs = errors.Join(errs, err)
continue continue
} }
_, ok := aa[key] _, 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)) errs = errors.Join(errs, fmt.Errorf("duplicated plugin key found for %q in %q", plugin.ShortCut, k))
continue 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( aa[key] = ui.NewKeyActionWithOpts(
plugin.Description, plugin.Description,