supports referencing envs in hotkeys (#2420)

* supports referencing envs in hotkeys

* add testcase for hotkeys

* rename attr name to keepHistory
mine
Jayson Wang 2024-01-04 13:56:21 +08:00 committed by GitHub
parent d0f874e01a
commit 3137b2b55a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 3 deletions

View File

@ -525,6 +525,12 @@ 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
ctrl-u:
shortCut: Ctrl-U
description: Namespaced resources
command: "$RESOURCE_NAME $NAMESPACE"
keepHistory: true # whether you can return to the previous view
``` ```
Not feeling so hot? Your custom hotkeys will be listed in the help view `?`. Not feeling so hot? Your custom hotkeys will be listed in the help view `?`.
@ -532,6 +538,8 @@ In order to surface hotkeys globally please follow these steps:
You can choose any keyboard shortcuts that make sense to you, provided they are not part of the standard K9s shortcuts list. You can choose any keyboard shortcuts that make sense to you, provided they are not part of the standard K9s shortcuts list.
Similarly, referencing environment variables in hotkeys is also supported. The available environment variables can refer to the description in the [Plugins](#plugins) section.
> NOTE: This feature/configuration might change in future releases! > NOTE: This feature/configuration might change in future releases!
--- ---

View File

@ -19,6 +19,7 @@ type HotKey struct {
ShortCut string `yaml:"shortCut"` ShortCut string `yaml:"shortCut"`
Description string `yaml:"description"` Description string `yaml:"description"`
Command string `yaml:"command"` Command string `yaml:"command"`
KeepHistory bool `yaml:"keepHistory"`
} }
// NewHotKeys returns a new plugin. // NewHotKeys returns a new plugin.

View File

@ -21,4 +21,5 @@ func TestHotKeyLoad(t *testing.T) {
assert.Equal(t, "shift-0", k.ShortCut) assert.Equal(t, "shift-0", k.ShortCut)
assert.Equal(t, "Launch pod view", k.Description) assert.Equal(t, "Launch pod view", k.Description)
assert.Equal(t, "pods", k.Command) assert.Equal(t, "pods", k.Command)
assert.Equal(t, true, k.KeepHistory)
} }

View File

@ -3,3 +3,4 @@ hotKeys:
shortCut: shift-0 shortCut: shift-0
description: Launch pod view description: Launch pod view
command: pods command: pods
keepHistory: true

View File

@ -74,16 +74,23 @@ func hotKeyActions(r Runner, aa ui.KeyActions) {
log.Warn().Err(fmt.Errorf("HOT-KEY Doh! you are trying to override an existing command `%s", k)).Msg("Invalid shortcut") log.Warn().Err(fmt.Errorf("HOT-KEY Doh! you are trying to override an existing command `%s", k)).Msg("Invalid shortcut")
continue continue
} }
command, err := r.EnvFn()().Substitute(hk.Command)
if err != nil {
log.Warn().Err(err).Msg("Invalid shortcut command")
continue
}
aa[key] = ui.NewSharedKeyAction( aa[key] = ui.NewSharedKeyAction(
hk.Description, hk.Description,
gotoCmd(r, hk.Command, ""), gotoCmd(r, command, "", !hk.KeepHistory),
false) false)
} }
} }
func gotoCmd(r Runner, cmd, path string) ui.ActionHandler { func gotoCmd(r Runner, cmd, path string, clearStack bool) ui.ActionHandler {
return func(evt *tcell.EventKey) *tcell.EventKey { return func(evt *tcell.EventKey) *tcell.EventKey {
r.App().gotoResource(cmd, path, true) r.App().gotoResource(cmd, path, clearStack)
return nil return nil
} }
} }