Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix K9S_EDITOR #3043

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions internal/view/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
outputPrefix = "[output]"
)

var editorEnvVars = []string{"KUBE_EDITOR", "K9S_EDITOR", "EDITOR"}
var editorEnvVars = []string{"K9S_EDITOR", "KUBE_EDITOR", "EDITOR"}

type shellOpts struct {
clear, background bool
Expand Down Expand Up @@ -130,7 +130,22 @@ func edit(a *App, opts shellOpts) bool {
if env == "" {
continue
}
if bin, err = exec.LookPath(env); err == nil {

// There may be situations where the user sets the editor as the binary
// followed by some arguments (e.g. "code -w" to make it work with vscode)
//
// In such cases, the actual binary is only the first token
envTokens := strings.Split(env, " ")

if bin, err = exec.LookPath(envTokens[0]); err == nil {
// Make sure the path is at the end (this allows running editors
// with custom options)
if len(envTokens) > 1 {
originalArgs := opts.args
opts.args = envTokens[1:]
opts.args = append(opts.args, originalArgs...)
}

break
}
}
Expand Down Expand Up @@ -181,6 +196,20 @@ func execute(opts shellOpts, statusChan chan<- string) error {
cmds := make([]*exec.Cmd, 0, 1)
cmd := exec.CommandContext(ctx, opts.binary, opts.args...)
log.Debug().Msgf("RUNNING> %s", opts)

if env := os.Getenv("K9S_EDITOR"); env != "" {
// There may be situations where the user sets the editor as the binary
// followed by some arguments (e.g. "code -w" to make it work with vscode)
//
// In such cases, the actual binary is only the first token
binTokens := strings.Split(env, " ")

if bin, err := exec.LookPath(binTokens[0]); err == nil {
binTokens[0] = bin
cmd.Env = append(os.Environ(), fmt.Sprintf("KUBE_EDITOR=%s", strings.Join(binTokens, " ")))
}
}

cmds = append(cmds, cmd)

for _, p := range opts.pipes {
Expand Down
Loading