浏览代码

Implements InvokeEditor

arianagiroux 2 周之前
父节点
当前提交
89f0aad4e0
共有 3 个文件被更改,包括 45 次插入5 次删除
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 42 5
      io.go

+ 1 - 0
go.mod

@@ -6,6 +6,7 @@ require (
 	github.com/charmbracelet/bubbles v1.0.0
 	github.com/charmbracelet/bubbletea v1.3.10
 	github.com/charmbracelet/lipgloss v1.1.0
+	github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
 	github.com/stretchr/testify v1.11.1
 )
 

+ 2 - 0
go.sum

@@ -26,6 +26,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
+github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
+github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
 github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag=
 github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
 github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=

+ 42 - 5
io.go

@@ -27,12 +27,15 @@ package issues
 import (
 	"errors"
 	"fmt"
+	"log"
 	"math/rand"
 	"os"
 	"os/exec"
 	"path/filepath"
 	"strings"
 	"time"
+
+	"github.com/google/shlex"
 )
 
 // converts file from []string to string, reports errors
@@ -90,18 +93,52 @@ func ReadTemplate(path string) (lines []string, err error) {
 	return lines, err
 }
 
-// InvokeEditor invokes a preconfigured editor, and reports the output and any errors.
+// InvokeEditor invokes the system's configured editor on the specified path,
+// and reports any errors.
+//
+// InvokeEditor will attempt to determine the editor command in the following
+// order:
+//
+//  1. The $ISSUES_EDITOR environment variable
+//
+//  2. The $GIT_CONFIG environment variable
+//
+//  3. The $EDITOR environment variable
+//
+// Finally, if no configured editor is found, the program will exit with status
+// code 2
 func InvokeEditor(path string) error {
 	// determine editor
 	//	1. Git config
 	//	2. $EDITOR
 	//	3. Panic?
 
+	editor := os.Getenv("ISSUES_EDITOR")
+	// if issue editor wasn't present, check git editor
+	if editor == "" {
+		editor = os.Getenv("GIT_EDITOR")
+	}
+	// if git editor wasn't present, check if editor set
+	if editor == "" {
+		editor = os.Getenv("EDITOR")
+	}
+	// if editor wasn't present, error out
+	if editor == "" {
+		log.Fatal("no editor set by system")
+		os.Exit(2)
+	}
+
 	// execute editor
-	cmd := exec.Command("vim", path)
-	cmd.Stdin = os.Stdin   // capture data
-	cmd.Stdout = os.Stdout // capture data
-	cmd.Stderr = os.Stderr // capture data
+	editorCmd, err := shlex.Split(editor)
+	if err != nil {
+		log.Fatal("could not parse editor")
+		os.Exit(2)
+	}
+	editorCmd = append(editorCmd, path)
+	cmd := exec.Command(editorCmd[0], editorCmd[1:]...)
+	cmd.Stdin = os.Stdin   // pass stdin to cmd
+	cmd.Stdout = os.Stdout // pass stdout to cmd
+	cmd.Stderr = os.Stderr // pass stderr to cmd
 
 	// wait for cmd and error out if err
 	if err := cmd.Run(); err != nil {