-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy patheditor.go
156 lines (130 loc) · 3.06 KB
/
editor.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package github
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/github/hub/v2/cmd"
"github.com/github/hub/v2/git"
"github.com/kballard/go-shellquote"
)
const Scissors = "------------------------ >8 ------------------------"
func NewEditor(filename, topic, message string) (editor *Editor, err error) {
gitDir, err := git.Dir()
if err != nil {
return
}
messageFile := filepath.Join(gitDir, filename)
program, err := git.Editor()
if err != nil {
return
}
cs, err := git.CommentChar(message)
if err != nil {
return
}
editor = &Editor{
Program: program,
Topic: topic,
File: messageFile,
Message: message,
CS: cs,
openEditor: openTextEditor,
}
return
}
type Editor struct {
Program string
Topic string
File string
Message string
CS string
addedFirstComment bool
openEditor func(program, file string) error
}
func (e *Editor) AddCommentedSection(text string) {
if !e.addedFirstComment {
scissors := e.CS + " " + Scissors + "\n"
scissors += e.CS + " Do not modify or remove the line above.\n"
scissors += e.CS + " Everything below it will be ignored.\n"
e.Message = e.Message + "\n" + scissors
e.addedFirstComment = true
}
e.Message = e.Message + "\n" + text
}
func (e *Editor) DeleteFile() error {
return os.Remove(e.File)
}
func (e *Editor) EditContent() (content string, err error) {
b, err := e.openAndEdit()
if err != nil {
return
}
b = bytes.TrimSpace(b)
reader := bytes.NewReader(b)
scanner := bufio.NewScanner(reader)
unquotedLines := []string{}
scissorsLine := e.CS + " " + Scissors
for scanner.Scan() {
line := scanner.Text()
if line == scissorsLine {
break
}
unquotedLines = append(unquotedLines, line)
}
if err = scanner.Err(); err != nil {
return
}
content = strings.Join(unquotedLines, "\n")
return
}
func (e *Editor) openAndEdit() (content []byte, err error) {
err = e.writeContent()
if err != nil {
return
}
err = e.openEditor(e.Program, e.File)
if err != nil {
err = fmt.Errorf("error using text editor for %s message", e.Topic)
defer e.DeleteFile()
return
}
content, err = e.readContent()
return
}
func (e *Editor) writeContent() (err error) {
if !e.isFileExist() {
err = ioutil.WriteFile(e.File, []byte(e.Message), 0644)
if err != nil {
return
}
}
return
}
func (e *Editor) isFileExist() bool {
_, err := os.Stat(e.File)
return err == nil || !os.IsNotExist(err)
}
func (e *Editor) readContent() (content []byte, err error) {
return ioutil.ReadFile(e.File)
}
func openTextEditor(program, file string) error {
programArgs, err := shellquote.Split(program)
if err != nil {
return err
}
editCmd := cmd.NewWithArray(programArgs)
r := regexp.MustCompile(`\b(?:[gm]?vim)(?:\.exe)?$`)
if r.MatchString(editCmd.Name) {
editCmd.WithArg("--cmd")
editCmd.WithArg("set ft=gitcommit tw=0 wrap lbr")
}
editCmd.WithArg(file)
// Reattach stdin to the console before opening the editor
setConsole(editCmd)
return editCmd.Spawn()
}