Skip to content

Commit

Permalink
Allow overwriting plugin output with command's stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
rtim75 committed Mar 27, 2024
1 parent d3027c8 commit dc47063
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
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
10 changes: 9 additions & 1 deletion internal/view/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,15 @@ 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 dc47063

Please sign in to comment.