Optionally allow plugin commands to have confirm dialog

mine
David Němec 2020-04-29 17:51:12 +02:00
parent f0b56964ea
commit 2777a7d510
No known key found for this signature in database
GPG Key ID: B1064EFFFD11AA75
2 changed files with 20 additions and 8 deletions

View File

@ -18,6 +18,7 @@ type Plugins struct {
// Plugin describes a K9s plugin // Plugin describes a K9s plugin
type Plugin struct { type Plugin struct {
ShortCut string `yaml:"shortCut"` ShortCut string `yaml:"shortCut"`
Confirm bool `yaml:"confirm"`
Scopes []string `yaml:"scopes"` Scopes []string `yaml:"scopes"`
Description string `yaml:"description"` Description string `yaml:"description"`
Command string `yaml:"command"` Command string `yaml:"command"`

View File

@ -2,9 +2,11 @@ package view
import ( import (
"fmt" "fmt"
"strings"
"github.com/derailed/k9s/internal/config" "github.com/derailed/k9s/internal/config"
"github.com/derailed/k9s/internal/ui" "github.com/derailed/k9s/internal/ui"
"github.com/derailed/k9s/internal/ui/dialog"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
@ -107,12 +109,12 @@ func pluginActions(r Runner, aa ui.KeyActions) {
} }
aa[key] = ui.NewKeyAction( aa[key] = ui.NewKeyAction(
plugin.Description, plugin.Description,
execCmd(r, plugin.Command, plugin.Background, plugin.Args...), pluginAction(r, plugin),
true) 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 { return func(evt *tcell.EventKey) *tcell.EventKey {
path := r.GetSelectedItem() path := r.GetSelectedItem()
if path == "" { if path == "" {
@ -123,20 +125,29 @@ func execCmd(r Runner, bin string, bg bool, args ...string) ui.ActionHandler {
return nil return nil
} }
aa := make([]string, len(args)) args := make([]string, len(p.Args))
for i, a := range args { for i, a := range p.Args {
arg, err := r.EnvFn()().Substitute(a) arg, err := r.EnvFn()().Substitute(a)
if err != nil { if err != nil {
log.Error().Err(err).Msg("Plugin Args match failed") log.Error().Err(err).Msg("Plugin Args match failed")
return nil return nil
} }
aa[i] = arg args[i] = arg
} }
if run(r.App(), shellOpts{clear: true, binary: bin, background: bg, args: aa}) { 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!") r.App().Flash().Info("Plugin command launched successfully!")
} else { } else {
r.App().Flash().Info("Plugin command failed!") 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 {
fn()
}
return nil return nil
} }