Read cached file in editor if it exists

This commit is contained in:
Jingwen Owen Ou 2014-03-02 11:20:04 -08:00
Родитель df430232d4
Коммит e4a6c9ce5e
2 изменённых файлов: 74 добавлений и 26 удалений

Просмотреть файл

@ -27,46 +27,67 @@ func NewEditor(filePrefix, topic, message string) (editor *Editor, err error) {
}
editor = &Editor{
Program: program,
Topic: topic,
File: messageFile,
Message: message,
doEdit: doTextEditorEdit,
Program: program,
Topic: topic,
File: messageFile,
Message: message,
openEditor: doTextEditorEdit,
}
return
}
type Editor struct {
Program string
Topic string
File string
Message string
doEdit func(program, file string) error
Program string
Topic string
File string
Message string
openEditor func(program, file string) error
}
func (e *Editor) Edit() (content []byte, err error) {
if e.Message != "" {
func (e *Editor) isFileExist() bool {
_, err := os.Stat(e.File)
return err == nil || !os.IsNotExist(err)
}
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) readContent() (content []byte, err error) {
return ioutil.ReadFile(e.File)
}
func (e *Editor) openAndEdit() (content []byte, err error) {
err = e.WriteContent()
if err != nil {
return
}
// always remove file after editing is done
defer os.Remove(e.File)
err = e.doEdit(e.Program, e.File)
err = e.openEditor(e.Program, e.File)
if err != nil {
err = fmt.Errorf("error using text editor for %s message", e.Topic)
return
}
content, err = ioutil.ReadFile(e.File)
content, err = e.readContent()
return
}
func (e *Editor) EditTitleAndBody() (title, body string, err error) {
content, err := e.Edit()
content, err := e.openAndEdit()
if err != nil {
return
}

Просмотреть файл

@ -2,27 +2,30 @@ package github
import (
"bufio"
"github.com/bmizerany/assert"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/bmizerany/assert"
)
func TestEditor_Edit(t *testing.T) {
func TestEditor_openAndEdit_fileExist(t *testing.T) {
tempFile, _ := ioutil.TempFile("", "editor-test")
ioutil.WriteFile(tempFile.Name(), []byte("hello"), 0644)
editor := Editor{
Program: "memory",
File: tempFile.Name(),
doEdit: func(program string, file string) error {
openEditor: func(program string, file string) error {
assert.Equal(t, "memory", program)
assert.Equal(t, tempFile.Name(), file)
return ioutil.WriteFile(file, []byte("hello"), 0644)
return nil
},
}
content, err := editor.Edit()
content, err := editor.openAndEdit()
assert.Equal(t, nil, err)
assert.Equal(t, "hello", string(content))
@ -31,14 +34,38 @@ func TestEditor_Edit(t *testing.T) {
assert.T(t, os.IsNotExist(err))
}
func TestEditor_EditTitleAndBody(t *testing.T) {
tempFile, _ := ioutil.TempFile("", "editor-test")
func TestEditor_openAndEdit_fileNotExist(t *testing.T) {
tempDir, _ := ioutil.TempDir("", "editor-test")
tempFile := filepath.Join(tempDir, "PULLREQ")
editor := Editor{
Program: "memory",
File: tempFile.Name(),
doEdit: func(program string, file string) error {
File: tempFile,
openEditor: func(program string, file string) error {
assert.Equal(t, "memory", program)
assert.Equal(t, tempFile.Name(), file)
assert.Equal(t, tempFile, file)
return ioutil.WriteFile(file, []byte("hello"), 0644)
},
}
content, err := editor.openAndEdit()
assert.Equal(t, nil, err)
assert.Equal(t, "hello", string(content))
// file is removed after edit
_, err = os.Stat(tempFile)
assert.T(t, os.IsNotExist(err))
}
func TestEditor_EditTitleAndBody(t *testing.T) {
tempDir, _ := ioutil.TempDir("", "editor-test")
tempFile := filepath.Join(tempDir, "PULLREQ")
editor := Editor{
Program: "memory",
File: tempFile,
openEditor: func(program string, file string) error {
assert.Equal(t, "memory", program)
assert.Equal(t, tempFile, file)
message := `A title
A title continues
@ -57,7 +84,7 @@ A body continues
assert.Equal(t, "A body\nA body continues", body)
// file is removed after edit
_, err = os.Stat(tempFile.Name())
_, err = os.Stat(tempFile)
assert.T(t, os.IsNotExist(err))
}