diff --git a/config_cmd.go b/config_cmd.go index 6dfc835..a4cb584 100644 --- a/config_cmd.go +++ b/config_cmd.go @@ -1,14 +1,12 @@ package main import ( - "errors" "fmt" "os" - "os/exec" "path" "path/filepath" - "strings" + "github.com/charmbracelet/glow/editor" gap "github.com/muesli/go-app-paths" "github.com/spf13/cobra" ) @@ -32,11 +30,6 @@ var configCmd = &cobra.Command{ Example: paragraph("glow config\nglow config --config path/to/config.yml"), Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - editor := strings.Fields(os.Getenv("EDITOR")) - if len(editor) == 0 { - return errors.New("no EDITOR environment variable set") - } - if configFile == "" { scope := gap.NewScope(gap.User, "glow") @@ -71,11 +64,7 @@ var configCmd = &cobra.Command{ return err } - var eargs []string - if len(editor) > 1 { - eargs = editor[1:] - } - c := exec.Command(editor[0], append(eargs, configFile)...) // nolint: gosec + c := editor.Cmd(configFile) c.Stdin = os.Stdin c.Stdout = os.Stdout c.Stderr = os.Stderr diff --git a/editor/editor.go b/editor/editor.go new file mode 100644 index 0000000..bbfe6ff --- /dev/null +++ b/editor/editor.go @@ -0,0 +1,27 @@ +package editor + +import ( + "os" + "os/exec" + "strings" +) + +const defaultEditor = "nano" + +// Cmd returns a *exec.Cmd editing the given path with $EDITOR or nano if no +// $EDITOR is set. +func Cmd(path string) *exec.Cmd { + editor, args := getEditor() + return exec.Command(editor, append(args, path)...) +} + +func getEditor() (string, []string) { + editor := strings.Fields(os.Getenv("EDITOR")) + if len(editor) > 1 { + return editor[0], editor[1:] + } + if len(editor) == 1 { + return editor[0], []string{} + } + return defaultEditor, []string{} +} diff --git a/editor/editor_test.go b/editor/editor_test.go new file mode 100644 index 0000000..2d1b583 --- /dev/null +++ b/editor/editor_test.go @@ -0,0 +1,26 @@ +package editor + +import ( + "reflect" + "testing" +) + +func TestEditor(t *testing.T) { + filename := "README.md" + for k, v := range map[string][]string{ + "": {"nano", filename}, + "nvim": {"nvim", filename}, + "vim": {"vim", filename}, + "vscode --foo": {"vscode", "--foo", filename}, + "nvim -a -b": {"nvim", "-a", "-b", filename}, + } { + t.Run(k, func(t *testing.T) { + t.Setenv("EDITOR", k) + cmd := Cmd("README.md") + got := cmd.Args + if !reflect.DeepEqual(got, v) { + t.Fatalf("expected %v; got %v", v, got) + } + }) + } +} diff --git a/ui/editor.go b/ui/editor.go index 60cc320..c38ef01 100644 --- a/ui/editor.go +++ b/ui/editor.go @@ -1,33 +1,15 @@ package ui import ( - "os" - "os/exec" - "strings" - tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/glow/editor" ) -const defaultEditor = "nano" - type editorFinishedMsg struct{ err error } func openEditor(path string) tea.Cmd { - editor, args := getEditor() - cmd := exec.Command(editor, append(args, path)...) cb := func(err error) tea.Msg { return editorFinishedMsg{err} } - return tea.ExecProcess(cmd, cb) -} - -func getEditor() (string, []string) { - editor := strings.Fields(os.Getenv("EDITOR")) - if len(editor) > 1 { - return editor[0], editor[1:] - } - if len(editor) == 1 { - return editor[0], []string{} - } - return defaultEditor, []string{} + return tea.ExecProcess(editor.Cmd(path), cb) }