Skip to content

Commit

Permalink
supports referencing envs in hotkeys (#2420)
Browse files Browse the repository at this point in the history
* supports referencing envs in hotkeys

* add testcase for hotkeys

* rename attr name to keepHistory
  • Loading branch information
wjiec authored Jan 4, 2024
1 parent d0f874e commit 3137b2b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,13 +525,21 @@ 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 `?`.
Also your hotkeys file will be automatically reloaded so you can readily use your hotkeys as you define them.

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!

---
Expand Down
1 change: 1 addition & 0 deletions internal/config/hotkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions internal/config/hotkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions internal/config/testdata/hotkeys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ hotKeys:
shortCut: shift-0
description: Launch pod view
command: pods
keepHistory: true
13 changes: 10 additions & 3 deletions internal/view/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 3137b2b

Please sign in to comment.