From 3137b2b55a00581c8dac0bbc97b7ffc10854745b Mon Sep 17 00:00:00 2001 From: Jayson Wang Date: Thu, 4 Jan 2024 13:56:21 +0800 Subject: [PATCH] supports referencing envs in hotkeys (#2420) * supports referencing envs in hotkeys * add testcase for hotkeys * rename attr name to keepHistory --- README.md | 8 ++++++++ internal/config/hotkey.go | 1 + internal/config/hotkey_test.go | 1 + internal/config/testdata/hotkeys.yaml | 1 + internal/view/actions.go | 13 ++++++++++--- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7a1c80e4fb..8fa956f30c 100644 --- a/README.md +++ b/README.md @@ -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! --- diff --git a/internal/config/hotkey.go b/internal/config/hotkey.go index fa292a98f9..b98c7c436e 100644 --- a/internal/config/hotkey.go +++ b/internal/config/hotkey.go @@ -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. diff --git a/internal/config/hotkey_test.go b/internal/config/hotkey_test.go index 80b244c735..28936d3490 100644 --- a/internal/config/hotkey_test.go +++ b/internal/config/hotkey_test.go @@ -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) } diff --git a/internal/config/testdata/hotkeys.yaml b/internal/config/testdata/hotkeys.yaml index 255555b3e1..55fadf8491 100644 --- a/internal/config/testdata/hotkeys.yaml +++ b/internal/config/testdata/hotkeys.yaml @@ -3,3 +3,4 @@ hotKeys: shortCut: shift-0 description: Launch pod view command: pods + keepHistory: true diff --git a/internal/view/actions.go b/internal/view/actions.go index 7ce1e6da6b..b6c14afca2 100644 --- a/internal/view/actions.go +++ b/internal/view/actions.go @@ -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 } }