-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathextedit.go
91 lines (74 loc) · 1.87 KB
/
extedit.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// extedit provides functionality to open an editor with to let the user edit
// some content and get the changes the user made to process them as
// part of the user iterface of a command line programm.
package extedit
import (
"bufio"
"io"
"io/ioutil"
"os"
"os/exec"
)
const default_editor = "vim"
// Session represents
type Session struct {
input Content
result Content
SplitFunc bufio.SplitFunc
}
// Session.Invoke starts a text-editor with the contents of content.
// After the user has closed the editor Invoke returns an
// io.Reader with the edited content.
func (s *Session) Invoke(content io.Reader) (Diff, error) {
d := Diff{}
input, err := contentFromReader(content, s.SplitFunc)
if err != nil {
return d, err
}
fileName, err := writeTmpFile(input)
if err != nil {
return d, err
}
cmd := editorCmd(fileName)
err = cmd.Run()
if err != nil {
return d, err
}
result, err := contentFromFile(fileName, s.SplitFunc)
if err != nil {
return d, err
}
return NewDiff(input, result), nil
}
func NewSession() *Session {
return &Session{SplitFunc: bufio.ScanLines}
}
// Invoke is a shortcut to the Invoke method of a default session.
func Invoke(content io.Reader) (Diff, error) {
s := NewSession()
return s.Invoke(content)
}
// writeTmpFile writes content to a temporary file and returns
// the path to the file
func writeTmpFile(content io.Reader) (string, error) {
f, err := ioutil.TempFile("", "")
if err != nil {
return "", err
}
io.Copy(f, content)
f.Close()
return f.Name(), nil
}
// editorCmd creates a os/exec.Cmd to open
// filename in an editor ready to be run()
func editorCmd(filename string) *exec.Cmd {
editorPath := os.Getenv("EDITOR")
if editorPath == "" {
editorPath = default_editor
}
editor := exec.Command(editorPath, filename)
editor.Stdin = os.Stdin
editor.Stdout = os.Stdout
editor.Stderr = os.Stderr
return editor
}