Return error from Save method
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2019-02-19 07:34:52 +01:00
parent f1695c3136
commit 47783e70ba
2 changed files with 16 additions and 14 deletions

18
file.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"github.com/sergi/go-diff/diffmatchpatch" "github.com/sergi/go-diff/diffmatchpatch"
"html" "html"
"html/template" "html/template"
@ -37,35 +38,32 @@ func (fp *FilePages) Get(p string) Page {
return Page{Content: string(body)} return Page{Content: string(body)}
} }
func (fp *FilePages) Save(p string, page Page, summary, author string) { func (fp *FilePages) Save(p string, page Page, summary, author string) error {
f, err := os.Create(filepath.Join(fp.dirname, strings.Replace(p, " ", "_", -1))) f, err := os.Create(filepath.Join(fp.dirname, strings.Replace(p, " ", "_", -1)))
if err != nil { if err != nil {
return return err
} }
defer f.Close() defer f.Close()
f.WriteString(strings.Replace(page.Content, "\r\n", "\n", -1)) f.WriteString(strings.Replace(page.Content, "\r\n", "\n", -1))
saveWithGit(fp, p, summary, author) return saveWithGit(fp, p, summary, author)
return
} }
func saveWithGit(fp *FilePages, p string, summary, author string) { func saveWithGit(fp *FilePages, p string, summary, author string) error {
cmd := exec.Command("git", "add", ".") cmd := exec.Command("git", "add", ".")
cmd.Dir = fp.dirname cmd.Dir = fp.dirname
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
log.Println(err) return fmt.Errorf("while adding page %s: %s", p, err)
return
} }
cmd = exec.Command("git", "commit", "-m", "Changes to "+p+" by "+author+"\n\n"+summary) cmd = exec.Command("git", "commit", "-m", "Changes to "+p+" by "+author+"\n\n"+summary)
cmd.Dir = fp.dirname cmd.Dir = fp.dirname
err = cmd.Run() err = cmd.Run()
if err != nil { if err != nil {
log.Println(err) return fmt.Errorf("while commiting page %s: %s", p, err)
return
} }
return nil
} }
func (fp *FilePages) Exist(p string) bool { func (fp *FilePages) Exist(p string) bool {

View File

@ -47,7 +47,7 @@ type Change struct {
type PagesRepository interface { type PagesRepository interface {
Get(p string) Page Get(p string) Page
Save(p string, page Page, summary, author string) Save(p string, page Page, summary, author string) error
Exist(p string) bool Exist(p string) bool
PageHistory(p string) ([]Revision, error) PageHistory(p string) ([]Revision, error)
RecentChanges() ([]Change, error) RecentChanges() ([]Change, error)
@ -264,7 +264,11 @@ func (h *saveHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
page := r.PostForm.Get("p") page := r.PostForm.Get("p")
summary := r.PostForm.Get("summary") summary := r.PostForm.Get("summary")
mp.Save(page, Page{Content: r.PostForm.Get("content")}, summary, sess.Me) pageData := Page{Content: r.PostForm.Get("content")}
err = mp.Save(page, pageData, summary, sess.Me)
if err != nil {
log.Println(err)
}
http.Redirect(w, r, "/"+page, http.StatusFound) http.Redirect(w, r, "/"+page, http.StatusFound)
} }