Allow overwriting plugin output with command's stdout (#2644)

* Allow overwriting plugin output with command's stdout

* Update README.md

* remove 1  indentation level
mine
Ruslan Timofieiev 2024-04-20 21:02:26 +03:00 committed by GitHub
parent 5fdaa6cdd3
commit 21e091b237
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 28 deletions

View File

@ -650,6 +650,7 @@ A plugin is defined as follows:
* Command represents ad-hoc commands the plugin runs upon activation
* Background specifies whether or not the command runs in the background
* Args specifies the various arguments that should apply to the command above
* OverwriteOutput options allows plugin developers to provide custom messages on plugin execution
K9s does provide additional environment variables for you to customize your plugins arguments. Currently, the available environment variables are as follows:

View File

@ -21,6 +21,7 @@
},
"command": { "type": "string" },
"background": { "type": "boolean" },
"overwriteOutput": { "type": "boolean" },
"args": {
"type": "array",
"items": { "type": ["string", "number"] }

View File

@ -27,16 +27,17 @@ type Plugins struct {
// Plugin describes a K9s plugin.
type Plugin struct {
Scopes []string `yaml:"scopes"`
Args []string `yaml:"args"`
ShortCut string `yaml:"shortCut"`
Override bool `yaml:"override"`
Pipes []string `yaml:"pipes"`
Description string `yaml:"description"`
Command string `yaml:"command"`
Confirm bool `yaml:"confirm"`
Background bool `yaml:"background"`
Dangerous bool `yaml:"dangerous"`
Scopes []string `yaml:"scopes"`
Args []string `yaml:"args"`
ShortCut string `yaml:"shortCut"`
Override bool `yaml:"override"`
Pipes []string `yaml:"pipes"`
Description string `yaml:"description"`
Command string `yaml:"command"`
Confirm bool `yaml:"confirm"`
Background bool `yaml:"background"`
Dangerous bool `yaml:"dangerous"`
OverwriteOutput bool `yaml:"overwriteOutput"`
}
func (p Plugin) String() string {

View File

@ -22,23 +22,25 @@ var pluginYmlTestData = Plugin{
}
var test1YmlTestData = Plugin{
Scopes: []string{"po", "dp"},
Args: []string{"-n", "$NAMESPACE", "-boolean"},
ShortCut: "shift-s",
Description: "blee",
Command: "duh",
Confirm: true,
Background: false,
Scopes: []string{"po", "dp"},
Args: []string{"-n", "$NAMESPACE", "-boolean"},
ShortCut: "shift-s",
Description: "blee",
Command: "duh",
Confirm: true,
Background: false,
OverwriteOutput: true,
}
var test2YmlTestData = Plugin{
Scopes: []string{"svc", "ing"},
Args: []string{"-n", "$NAMESPACE", "-oyaml"},
ShortCut: "shift-r",
Description: "bla",
Command: "duha",
Confirm: false,
Background: true,
Scopes: []string{"svc", "ing"},
Args: []string{"-n", "$NAMESPACE", "-oyaml"},
ShortCut: "shift-r",
Description: "bla",
Command: "duha",
Confirm: false,
Background: true,
OverwriteOutput: false,
}
func TestPluginLoad(t *testing.T) {

View File

@ -206,7 +206,13 @@ func pluginAction(r Runner, p config.Plugin) ui.ActionHandler {
}
go func() {
for st := range statusChan {
r.App().Flash().Infof("Plugin command launched successfully: %q", st)
if !p.OverwriteOutput {
r.App().Flash().Infof("Plugin command launched successfully: %q", st)
} else if strings.Contains(st, outputPrefix) {
infoMsg := strings.TrimPrefix(st, outputPrefix)
r.App().Flash().Info(strings.TrimSpace(infoMsg))
return
}
}
}()

View File

@ -34,8 +34,9 @@ import (
)
const (
shellCheck = `command -v bash >/dev/null && exec bash || exec sh`
bannerFmt = "<<K9s-Shell>> Pod: %s | Container: %s \n"
shellCheck = `command -v bash >/dev/null && exec bash || exec sh`
bannerFmt = "<<K9s-Shell>> Pod: %s | Container: %s \n"
outputPrefix = "[output]"
)
var editorEnvVars = []string{"KUBE_EDITOR", "K9S_EDITOR", "EDITOR"}
@ -492,7 +493,7 @@ func pipe(_ context.Context, opts shellOpts, statusChan chan<- string, w, e *byt
} else {
for _, l := range strings.Split(w.String(), "\n") {
if l != "" {
statusChan <- fmt.Sprintf("[output] %s", l)
statusChan <- fmt.Sprintf("%s %s", outputPrefix, l)
}
}
statusChan <- fmt.Sprintf("Command completed successfully: %q", render.Truncate(cmd.String(), 20))