Skip to content

Commit

Permalink
take editor args
Browse files Browse the repository at this point in the history
  • Loading branch information
hassansin committed Jul 12, 2018
1 parent 260eef9 commit ac691a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export PATH=$PATH:$GOPATH/bin
3. (optional) When editing release message, it will open the editor found in `$EDITOR` environment variable, will fallback to `vim` if not found. You can set the environment variable to the path of your editor executable.

```
export EDITOR=/usr/bin/code
export EDITOR="/usr/bin/code -w"
```

## Usage
Expand Down
26 changes: 17 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ var (

func main() {
mustBeGitRepo()
if err := do(); err != nil {
editorCmd := mustFindEditor()
if err := do(editorCmd); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -100,7 +101,7 @@ func wrap(err error, msg string) error {
return err
}

func do() error {
func do(editorCmd []string) error {
token, err := getToken()
if err != nil {
return err
Expand Down Expand Up @@ -159,7 +160,7 @@ func do() error {
return err
}

ed, err := newEditor()
ed, err := newEditor(editorCmd)
if err != nil {
return err
}
Expand Down Expand Up @@ -345,25 +346,31 @@ func releaseNotes(title string, commits []github.RepositoryCommit) string {
%v`, title, notes)
}

func newEditor() (*editor, error) {
func mustFindEditor() []string {
env := os.Getenv("EDITOR")
if env == "" {
env = defaultEditor
}
path, err := exec.LookPath(env)
re := regexp.MustCompile("\\s")
parts := re.Split(env, -1)
path, err := exec.LookPath(parts[0])
if err != nil {
return nil, fmt.Errorf("unable to find editor(%v): %v", path, err)
panic(fmt.Errorf("unable to find editor(%v): %v", parts[0], err))
}
return append([]string{path}, parts[1:]...)
}

func newEditor(cmd []string) (*editor, error) {
return &editor{
path: path,
cmd: cmd,
file: releaseMsgFile,
mode: 0644,
}, nil

}

type editor struct {
path string
cmd []string
file string
mode os.FileMode
}
Expand All @@ -372,7 +379,8 @@ func (ed editor) edit(msg string) (string, string, error) {
if err := ioutil.WriteFile(ed.file, []byte(msg), ed.mode); err != nil {
return "", "", fmt.Errorf("unable to write release message: %v", err)
}
cmd := exec.Command(ed.path, ed.file)

cmd := exec.Command(ed.cmd[0], append(ed.cmd[1:], ed.file)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand Down

0 comments on commit ac691a3

Please sign in to comment.