~dvshkn/planfile

ref: 34ce4118513c1048ed016eed0bde9d14435c1ad5 planfile/utils/editor.go -rw-r--r-- 619 bytes
34ce4118David Knight first post 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package utils

import (
	"errors"
	"fmt"
	"os"
	"os/exec"
)

var EDITORS = []string {
	"/usr/bin/vim",
	"/usr/bin/emacs",
	"/usr/bin/nano",
}

func RunEditor(filename string) error {
	var editor string
	for _, path := range(EDITORS) {
		info, err := os.Stat(path)
		if err == nil && !info.IsDir() {
			editor = path
			break
		}
	}

	if len(editor) == 0 {
		return errors.New("no editor found")
	} else {
		fmt.Println("launching", editor)
	}

	cmd := exec.Command(editor, filename)
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	err := cmd.Run()
	
	if err != nil {
		return err
	}
	return nil
}