forked from mislav/hub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.go
162 lines (130 loc) · 2.96 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
157
158
159
160
161
162
package github
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/github/hub/cmd"
"github.com/github/hub/git"
)
func NewEditor(filePrefix, topic, message string) (editor *Editor, err error) {
messageFile, err := getMessageFile(filePrefix)
if err != nil {
return
}
program, err := git.Editor()
if err != nil {
return
}
cs := git.CommentChar()
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
openEditor func(program, file string) error
}
func (e *Editor) DeleteFile() error {
return os.Remove(e.File)
}
func (e *Editor) EditTitleAndBody() (title, body string, err error) {
content, err := e.openAndEdit()
if err != nil {
return
}
content = bytes.TrimSpace(content)
reader := bytes.NewReader(content)
title, body, err = readTitleAndBody(reader, e.CS)
if err != nil || title == "" {
defer e.DeleteFile()
}
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) {
// only write message if file doesn't exist
if !e.isFileExist() && e.Message != "" {
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 {
editCmd := cmd.New(program)
r := regexp.MustCompile("[mg]?vi[m]$")
if r.MatchString(program) {
editCmd.WithArg("-c")
editCmd.WithArg("set ft=gitcommit tw=0 wrap lbr")
}
editCmd.WithArg(file)
return editCmd.Spawn()
}
func readTitleAndBody(reader io.Reader, cs string) (title, body string, err error) {
var titleParts, bodyParts []string
r := regexp.MustCompile("\\S")
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, cs) {
continue
}
if len(bodyParts) == 0 && r.MatchString(line) {
titleParts = append(titleParts, line)
} else {
bodyParts = append(bodyParts, line)
}
}
if err = scanner.Err(); err != nil {
return
}
title = strings.Join(titleParts, " ")
title = strings.TrimSpace(title)
body = strings.Join(bodyParts, "\n")
body = strings.TrimSpace(body)
return
}
func getMessageFile(about string) (string, error) {
gitDir, err := git.Dir()
if err != nil {
return "", err
}
return filepath.Join(gitDir, fmt.Sprintf("%s_EDITMSG", about)), nil
}