Optionally allow plugin commands to have confirm dialog
parent
f0b56964ea
commit
2777a7d510
|
|
@ -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"`
|
||||||
|
|
|
||||||
|
|
@ -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,19 +125,28 @@ 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() {
|
||||||
r.App().Flash().Info("Plugin command launched successfully!")
|
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 {
|
} else {
|
||||||
r.App().Flash().Info("Plugin command failed!")
|
fn()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue