Skip to content

Commit

Permalink
Allow overwriting plugin output with command's stdout (#2644)
Browse files Browse the repository at this point in the history
* Allow overwriting plugin output with command's stdout

* Update README.md

* remove 1  indentation level
  • Loading branch information
rtim75 authored Apr 20, 2024
1 parent 5fdaa6c commit 21e091b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
1 change: 1 addition & 0 deletions internal/config/json/schemas/plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"command": { "type": "string" },
"background": { "type": "boolean" },
"overwriteOutput": { "type": "boolean" },
"args": {
"type": "array",
"items": { "type": ["string", "number"] }
Expand Down
21 changes: 11 additions & 10 deletions internal/config/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 16 additions & 14 deletions internal/config/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion internal/view/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}()

Expand Down
7 changes: 4 additions & 3 deletions internal/view/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 21e091b

Please sign in to comment.