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

Add support for EDITOR with args to commands #1174

Merged
merged 8 commits into from
Oct 29, 2024
4 changes: 3 additions & 1 deletion cli/consumer_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ func (c *consumerCmd) interactiveEdit(cfg api.ConsumerConfig) (*api.ConsumerConf
if editor == "" {
return &api.ConsumerConfig{}, fmt.Errorf("set EDITOR environment variable to your chosen editor")
}
editor, args := splitCommand(editor)

cj, err := decoratedYamlMarshal(cfg)
if err != nil {
Expand All @@ -584,7 +585,8 @@ func (c *consumerCmd) interactiveEdit(cfg api.ConsumerConfig) (*api.ConsumerConf

tfile.Close()

cmd := exec.Command(editor, tfile.Name())
args = append(args, tfile.Name())
cmd := exec.Command(editor, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
4 changes: 3 additions & 1 deletion cli/context_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func (c *ctxCommand) editCommand(pc *fisk.ParseContext) error {
if editor == "" {
return fmt.Errorf("set EDITOR environment variable to your chosen editor")
}
editor, args := splitCommand(editor)

if !natscontext.IsKnown(c.name) {
return fmt.Errorf("unknown context %q", c.name)
Expand Down Expand Up @@ -289,7 +290,8 @@ func (c *ctxCommand) editCommand(pc *fisk.ParseContext) error {
editFile = f.Name()
}

cmd := exec.Command(editor, editFile)
args = append(args, editFile)
cmd := exec.Command(editor, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
8 changes: 6 additions & 2 deletions cli/errors_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ func (c *errCmd) listAction(_ *fisk.ParseContext) error {
}

func (c *errCmd) editAction(pc *fisk.ParseContext) error {
if os.Getenv("EDITOR") == "" {
editor := os.Getenv("EDITOR")
if editor == "" {
return fmt.Errorf("EDITOR variable is not set")
}
editor, args := splitCommand(editor)

errs, err := c.loadErrors(nil)
if err != nil {
Expand Down Expand Up @@ -162,8 +164,10 @@ func (c *errCmd) editAction(pc *fisk.ParseContext) error {
tfile.Write(fj)
tfile.Close()

args = append(args, tfile.Name())

for {
cmd := exec.Command(os.Getenv("EDITOR"), tfile.Name())
cmd := exec.Command(editor, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
4 changes: 3 additions & 1 deletion cli/stream_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,7 @@ func (c *streamCmd) interactiveEdit(cfg api.StreamConfig) (api.StreamConfig, err
if editor == "" {
return api.StreamConfig{}, fmt.Errorf("set EDITOR environment variable to your chosen editor")
}
editor, args := splitCommand(editor)

cj, err := decoratedYamlMarshal(cfg)
if err != nil {
Expand All @@ -1816,7 +1817,8 @@ func (c *streamCmd) interactiveEdit(cfg api.StreamConfig) (api.StreamConfig, err

tfile.Close()

cmd := exec.Command(editor, tfile.Name())
args = append(args, tfile.Name())
cmd := exec.Command(editor, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down
8 changes: 8 additions & 0 deletions cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ func splitString(s string) []string {
})
}

// Split the string into a command and its arguments.
func splitCommand(s string) (string, []string) {
cmdAndArgs := strings.Fields(s)
joeriddles marked this conversation as resolved.
Show resolved Hide resolved
cmd := cmdAndArgs[0]
args := cmdAndArgs[1:]
return cmd, args
}

func splitCLISubjects(subjects []string) []string {
new := []string{}

Expand Down
18 changes: 18 additions & 0 deletions cli/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package cli

import (
"errors"
"slices"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -85,6 +86,23 @@ func TestSplitString(t *testing.T) {
}
}

func TestSplitCommand(t *testing.T) {
cmd, args := splitCommand("vim")
if cmd != "vim" && len(args) != 0 {
t.Fatalf("Expected vim and [], got %v and %v", cmd, args)
}

cmd, args = splitCommand("code --wait")
if cmd != "code" && !slices.Equal(args, []string{"--wait"}) {
t.Fatalf("Expected code and [\"--wait\"], got %v and %v", cmd, args)
}

cmd, args = splitCommand("code --wait --new-window")
if cmd != "code" && !slices.Equal(args, []string{"--wait", "--new-window"}) {
t.Fatalf("Expected code and [\"--wait\", \"--new-window\"], got %v and %v", cmd, args)
}
}

func TestRandomString(t *testing.T) {
for i := 0; i < 1000; i++ {
if len(randomString(1024, 1024)) != 1024 {
Expand Down