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
description: Xray Deployments
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 `?`.
@ -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.
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!
---

View File

@ -19,6 +19,7 @@ type HotKey struct {
ShortCut string `yaml:"shortCut"`
Description string `yaml:"description"`
Command string `yaml:"command"`
KeepHistory bool `yaml:"keepHistory"`
}
// 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, "Launch pod view", k.Description)
assert.Equal(t, "pods", k.Command)
assert.Equal(t, true, k.KeepHistory)
}

View File

@ -3,3 +3,4 @@ hotKeys:
shortCut: shift-0
description: Launch pod view
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")
continue
}
command, err := r.EnvFn()().Substitute(hk.Command)
if err != nil {
log.Warn().Err(err).Msg("Invalid shortcut command")
continue
}
aa[key] = ui.NewSharedKeyAction(
hk.Description,
gotoCmd(r, hk.Command, ""),
gotoCmd(r, command, "", !hk.KeepHistory),
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 {
r.App().gotoResource(cmd, path, true)
r.App().gotoResource(cmd, path, clearStack)
return nil
}
}