From 2777a7d5104ea9e4844a306eb44e27e57b87b99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C4=9Bmec?= Date: Wed, 29 Apr 2020 17:51:12 +0200 Subject: [PATCH] Optionally allow plugin commands to have confirm dialog --- internal/config/plugin.go | 1 + internal/view/actions.go | 27 +++++++++++++++++++-------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/internal/config/plugin.go b/internal/config/plugin.go index b0b50d4e..2979deea 100644 --- a/internal/config/plugin.go +++ b/internal/config/plugin.go @@ -18,6 +18,7 @@ type Plugins struct { // Plugin describes a K9s plugin type Plugin struct { ShortCut string `yaml:"shortCut"` + Confirm bool `yaml:"confirm"` Scopes []string `yaml:"scopes"` Description string `yaml:"description"` Command string `yaml:"command"` diff --git a/internal/view/actions.go b/internal/view/actions.go index 1d5abcc1..c05b38fd 100644 --- a/internal/view/actions.go +++ b/internal/view/actions.go @@ -2,9 +2,11 @@ package view import ( "fmt" + "strings" "github.com/derailed/k9s/internal/config" "github.com/derailed/k9s/internal/ui" + "github.com/derailed/k9s/internal/ui/dialog" "github.com/gdamore/tcell" "github.com/rs/zerolog/log" ) @@ -107,12 +109,12 @@ func pluginActions(r Runner, aa ui.KeyActions) { } aa[key] = ui.NewKeyAction( plugin.Description, - execCmd(r, plugin.Command, plugin.Background, plugin.Args...), + pluginAction(r, plugin), true) } } -func execCmd(r Runner, bin string, bg bool, args ...string) ui.ActionHandler { +func pluginAction(r Runner, p config.Plugin) ui.ActionHandler { return func(evt *tcell.EventKey) *tcell.EventKey { path := r.GetSelectedItem() if path == "" { @@ -123,19 +125,28 @@ func execCmd(r Runner, bin string, bg bool, args ...string) ui.ActionHandler { return nil } - aa := make([]string, len(args)) - for i, a := range args { + args := make([]string, len(p.Args)) + for i, a := range p.Args { arg, err := r.EnvFn()().Substitute(a) if err != nil { log.Error().Err(err).Msg("Plugin Args match failed") return nil } - aa[i] = arg + args[i] = arg } - if run(r.App(), shellOpts{clear: true, binary: bin, background: bg, args: aa}) { - r.App().Flash().Info("Plugin command launched successfully!") + fn := func() { + if run(r.App(), shellOpts{clear: true, binary: p.Command, background: p.Background, args: args}) { + r.App().Flash().Info("Plugin command launched successfully!") + } else { + r.App().Flash().Info("Plugin command failed!") + } + } + if p.Confirm { + dialog.ShowConfirm(r.App().Content.Pages, "Confirm plugin action", fmt.Sprintf("Will execute:\n%s %s", p.Command, strings.Join(args, " ")), func() { + fn() + }, func() {}) } else { - r.App().Flash().Info("Plugin command failed!") + fn() } return nil