From c609179dfa1676536ce3f6536ec1fcb63618d34c Mon Sep 17 00:00:00 2001 From: Stephen Searles Date: Wed, 30 Aug 2017 16:38:24 -0600 Subject: [PATCH] fixes from looking at PR --- caddyhugo.go | 10 ++++------ client.go | 19 +++++++++++++++---- http.go | 29 ----------------------------- hugo.go | 5 ++--- 4 files changed, 21 insertions(+), 42 deletions(-) diff --git a/caddyhugo.go b/caddyhugo.go index 91446f7..36730c4 100644 --- a/caddyhugo.go +++ b/caddyhugo.go @@ -75,15 +75,13 @@ func (ch *CaddyHugo) docFilename(orig string) string { // Publish really renders new content into the public directory func (ch *CaddyHugo) Publish() error { - ch.mtx.Lock() - for _, es := range ch.docs { - ch.persistEdits(es) + err := ch.persistAllEdits() + if err != nil { + return err } - ch.mtx.Unlock() - cmd := exec.Command("hugo") cmd.Dir = ch.Dir - _, err := cmd.CombinedOutput() + _, err = cmd.CombinedOutput() if err != nil { return err } diff --git a/client.go b/client.go index f8527ba..65ee767 100644 --- a/client.go +++ b/client.go @@ -42,8 +42,7 @@ func (ch *CaddyHugo) newEditSession(docName string) (*editSession, error) { docname: docName, filename: filename, doc: acedoc.NewString(string(contents)), - // tmpfs: afero.NewCopyOnWriteFs(afero.NewBasePathFs(afero.NewOsFs(), ch.Dir+"/"), afero.NewMemMapFs()), - tmpfs: afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs()), + tmpfs: afero.NewCopyOnWriteFs(afero.NewOsFs(), afero.NewMemMapFs()), } err = es.doc.LogToFile(path.Join(ch.Dir, "logs", docName)) @@ -71,7 +70,7 @@ func (ch *CaddyHugo) renderDraft(es *editSession) error { } } - cfg, err := HugoCmdProcessConfig(ch, es, f) + cfg, err := HugoInternalProcessConfig(ch, es, f) if err != nil { return fmt.Errorf("rendering draft: %v", err) } @@ -80,7 +79,19 @@ func (ch *CaddyHugo) renderDraft(es *editSession) error { return proc.Start() } -func (ch *CaddyHugo) persistEdits(es *editSession) error { +func (ch *CaddyHugo) persistAllEdits() error { + ch.mtx.Lock() + defer ch.mtx.Unlock() + for _, es := range ch.docs { + err := ch.persistEditsForSession(es) + if err != nil { + return err + } + } + return nil +} + +func (ch *CaddyHugo) persistEditsForSession(es *editSession) error { err := afero.WriteFile(afero.NewOsFs(), es.filename, []byte(es.doc.Contents()), 0644) if err != nil { return err diff --git a/http.go b/http.go index 4bb140f..ca46bba 100644 --- a/http.go +++ b/http.go @@ -215,35 +215,6 @@ func (ch *CaddyHugo) serveDraft(w http.ResponseWriter, r *http.Request) (int, er return 200, nil } -func printTree(fs afero.Fs) { - const tab = " " - - afero.Walk(fs, "/", filepath.WalkFunc(func(path string, info os.FileInfo, err error) error { - if filepath.Base(path) == ".git" { - return filepath.SkipDir - } - - if err != nil && !os.IsNotExist(err) { - fmt.Println(err) - return err - } else if err != nil { - return nil - } - - nIndent := len(strings.Split(path, string(filepath.Separator))) - 1 - indent := strings.Repeat(tab, nIndent) - - if info.IsDir() { - fmt.Printf("%s├ %s\n", indent, info.Name()) - nIndent++ - } else { - fmt.Printf("%s| %s\t%d\n", indent, info.Name(), info.Size()) - } - - return nil - })) -} - type aferoHTTP struct { afero.Fs } diff --git a/hugo.go b/hugo.go index 980d2cb..f05d8b3 100644 --- a/hugo.go +++ b/hugo.go @@ -28,7 +28,7 @@ type HugoRenderer interface { Stop() error } -func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) { +func HugoInternalProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idleshut.Config, error) { var err error hugoCfg := &deps.DepsCfg{Fs: hugofs.NewFrom(es.tmpfs, ch.HugoCfg.Cfg)} @@ -46,7 +46,6 @@ func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idles err = hugoSites.Build(hugolib.BuildCfg{ResetState: true}) if err != nil { - // TODO better return idleshut.Config{}, fmt.Errorf("caddy-hugo: building site: %v", err) } @@ -54,7 +53,7 @@ func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) (idles TickDuration: WebsocketFileTicker, MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1, Stop: func() error { - ch.persistEdits(es) + ch.persistEditsForSession(es) es.doc.Close()