Skip to content

Commit c7ef264

Browse files
placintaalexandruthorbenbelow
authored andcommitted
Fix K9S_EDITOR (derailed#3043)
1 parent 05c6250 commit c7ef264

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

internal/view/exec.go

+31-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const (
3939
outputPrefix = "[output]"
4040
)
4141

42-
var editorEnvVars = []string{"KUBE_EDITOR", "K9S_EDITOR", "EDITOR"}
42+
var editorEnvVars = []string{"K9S_EDITOR", "KUBE_EDITOR", "EDITOR"}
4343

4444
type shellOpts struct {
4545
clear, background bool
@@ -130,7 +130,22 @@ func edit(a *App, opts shellOpts) bool {
130130
if env == "" {
131131
continue
132132
}
133-
if bin, err = exec.LookPath(env); err == nil {
133+
134+
// There may be situations where the user sets the editor as the binary
135+
// followed by some arguments (e.g. "code -w" to make it work with vscode)
136+
//
137+
// In such cases, the actual binary is only the first token
138+
envTokens := strings.Split(env, " ")
139+
140+
if bin, err = exec.LookPath(envTokens[0]); err == nil {
141+
// Make sure the path is at the end (this allows running editors
142+
// with custom options)
143+
if len(envTokens) > 1 {
144+
originalArgs := opts.args
145+
opts.args = envTokens[1:]
146+
opts.args = append(opts.args, originalArgs...)
147+
}
148+
134149
break
135150
}
136151
}
@@ -181,6 +196,20 @@ func execute(opts shellOpts, statusChan chan<- string) error {
181196
cmds := make([]*exec.Cmd, 0, 1)
182197
cmd := exec.CommandContext(ctx, opts.binary, opts.args...)
183198
log.Debug().Msgf("RUNNING> %s", opts)
199+
200+
if env := os.Getenv("K9S_EDITOR"); env != "" {
201+
// There may be situations where the user sets the editor as the binary
202+
// followed by some arguments (e.g. "code -w" to make it work with vscode)
203+
//
204+
// In such cases, the actual binary is only the first token
205+
binTokens := strings.Split(env, " ")
206+
207+
if bin, err := exec.LookPath(binTokens[0]); err == nil {
208+
binTokens[0] = bin
209+
cmd.Env = append(os.Environ(), fmt.Sprintf("KUBE_EDITOR=%s", strings.Join(binTokens, " ")))
210+
}
211+
}
212+
184213
cmds = append(cmds, cmd)
185214

186215
for _, p := range opts.pipes {

0 commit comments

Comments
 (0)