Skip to content

Commit 21e091b

Browse files
authored
Allow overwriting plugin output with command's stdout (#2644)
* Allow overwriting plugin output with command's stdout * Update README.md * remove 1 indentation level
1 parent 5fdaa6c commit 21e091b

File tree

6 files changed

+40
-28
lines changed

6 files changed

+40
-28
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,7 @@ A plugin is defined as follows:
650650
* Command represents ad-hoc commands the plugin runs upon activation
651651
* Background specifies whether or not the command runs in the background
652652
* Args specifies the various arguments that should apply to the command above
653+
* OverwriteOutput options allows plugin developers to provide custom messages on plugin execution
653654

654655
K9s does provide additional environment variables for you to customize your plugins arguments. Currently, the available environment variables are as follows:
655656

internal/config/json/schemas/plugins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
},
2222
"command": { "type": "string" },
2323
"background": { "type": "boolean" },
24+
"overwriteOutput": { "type": "boolean" },
2425
"args": {
2526
"type": "array",
2627
"items": { "type": ["string", "number"] }

internal/config/plugin.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,17 @@ type Plugins struct {
2727

2828
// Plugin describes a K9s plugin.
2929
type Plugin struct {
30-
Scopes []string `yaml:"scopes"`
31-
Args []string `yaml:"args"`
32-
ShortCut string `yaml:"shortCut"`
33-
Override bool `yaml:"override"`
34-
Pipes []string `yaml:"pipes"`
35-
Description string `yaml:"description"`
36-
Command string `yaml:"command"`
37-
Confirm bool `yaml:"confirm"`
38-
Background bool `yaml:"background"`
39-
Dangerous bool `yaml:"dangerous"`
30+
Scopes []string `yaml:"scopes"`
31+
Args []string `yaml:"args"`
32+
ShortCut string `yaml:"shortCut"`
33+
Override bool `yaml:"override"`
34+
Pipes []string `yaml:"pipes"`
35+
Description string `yaml:"description"`
36+
Command string `yaml:"command"`
37+
Confirm bool `yaml:"confirm"`
38+
Background bool `yaml:"background"`
39+
Dangerous bool `yaml:"dangerous"`
40+
OverwriteOutput bool `yaml:"overwriteOutput"`
4041
}
4142

4243
func (p Plugin) String() string {

internal/config/plugin_test.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,25 @@ var pluginYmlTestData = Plugin{
2222
}
2323

2424
var test1YmlTestData = Plugin{
25-
Scopes: []string{"po", "dp"},
26-
Args: []string{"-n", "$NAMESPACE", "-boolean"},
27-
ShortCut: "shift-s",
28-
Description: "blee",
29-
Command: "duh",
30-
Confirm: true,
31-
Background: false,
25+
Scopes: []string{"po", "dp"},
26+
Args: []string{"-n", "$NAMESPACE", "-boolean"},
27+
ShortCut: "shift-s",
28+
Description: "blee",
29+
Command: "duh",
30+
Confirm: true,
31+
Background: false,
32+
OverwriteOutput: true,
3233
}
3334

3435
var test2YmlTestData = Plugin{
35-
Scopes: []string{"svc", "ing"},
36-
Args: []string{"-n", "$NAMESPACE", "-oyaml"},
37-
ShortCut: "shift-r",
38-
Description: "bla",
39-
Command: "duha",
40-
Confirm: false,
41-
Background: true,
36+
Scopes: []string{"svc", "ing"},
37+
Args: []string{"-n", "$NAMESPACE", "-oyaml"},
38+
ShortCut: "shift-r",
39+
Description: "bla",
40+
Command: "duha",
41+
Confirm: false,
42+
Background: true,
43+
OverwriteOutput: false,
4244
}
4345

4446
func TestPluginLoad(t *testing.T) {

internal/view/actions.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,13 @@ func pluginAction(r Runner, p config.Plugin) ui.ActionHandler {
206206
}
207207
go func() {
208208
for st := range statusChan {
209-
r.App().Flash().Infof("Plugin command launched successfully: %q", st)
209+
if !p.OverwriteOutput {
210+
r.App().Flash().Infof("Plugin command launched successfully: %q", st)
211+
} else if strings.Contains(st, outputPrefix) {
212+
infoMsg := strings.TrimPrefix(st, outputPrefix)
213+
r.App().Flash().Info(strings.TrimSpace(infoMsg))
214+
return
215+
}
210216
}
211217
}()
212218

internal/view/exec.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ import (
3434
)
3535

3636
const (
37-
shellCheck = `command -v bash >/dev/null && exec bash || exec sh`
38-
bannerFmt = "<<K9s-Shell>> Pod: %s | Container: %s \n"
37+
shellCheck = `command -v bash >/dev/null && exec bash || exec sh`
38+
bannerFmt = "<<K9s-Shell>> Pod: %s | Container: %s \n"
39+
outputPrefix = "[output]"
3940
)
4041

4142
var editorEnvVars = []string{"KUBE_EDITOR", "K9S_EDITOR", "EDITOR"}
@@ -492,7 +493,7 @@ func pipe(_ context.Context, opts shellOpts, statusChan chan<- string, w, e *byt
492493
} else {
493494
for _, l := range strings.Split(w.String(), "\n") {
494495
if l != "" {
495-
statusChan <- fmt.Sprintf("[output] %s", l)
496+
statusChan <- fmt.Sprintf("%s %s", outputPrefix, l)
496497
}
497498
}
498499
statusChan <- fmt.Sprintf("Command completed successfully: %q", render.Truncate(cmd.String(), 20))

0 commit comments

Comments
 (0)