Return error from Save method
the build was successful Details

single-block-api
Peter Stuifzand 5 years ago
parent f1695c3136
commit 47783e70ba

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

@ -47,7 +47,7 @@ type Change struct {
type PagesRepository interface {
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
PageHistory(p string) ([]Revision, error)
RecentChanges() ([]Change, error)
@ -78,7 +78,7 @@ type recentPage struct {
Session *Session
Title string
Name string
Recent []Change
Recent []Change
}
type indexHandler struct{}
@ -264,7 +264,11 @@ func (h *saveHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
page := r.PostForm.Get("p")
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)
}
@ -401,7 +405,7 @@ func (h *recentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Session: sess,
Title: "Recent changes",
Name: "Recent changes",
Recent: changes,
Recent: changes,
})
if err != nil {
http.Error(w, err.Error(), 500)

Loading…
Cancel
Save