|
|
|
@ -5,7 +5,8 @@ import ( |
|
|
|
|
"io/ioutil" |
|
|
|
|
"os" |
|
|
|
|
"os/exec" |
|
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
"git.stephensearles.com/stephen/idleshut" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type HugoInteractor interface { |
|
|
|
@ -13,55 +14,56 @@ type HugoInteractor interface { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type HugoRenderer interface { |
|
|
|
|
WriteContent(contents string) error |
|
|
|
|
Start() error |
|
|
|
|
Stop() error |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type HugoCmdRenderer struct { |
|
|
|
|
SrcDir string |
|
|
|
|
TmpDir string |
|
|
|
|
Filename string |
|
|
|
|
|
|
|
|
|
cmd *exec.Cmd |
|
|
|
|
mtx sync.Mutex |
|
|
|
|
} |
|
|
|
|
func HugoCmdProcessConfig(ch *CaddyHugo, es *editSession, touchFn func()) idleshut.Config { |
|
|
|
|
cmd := exec.Command("hugo", "--watch", "-D", "-d", es.tmpdir) |
|
|
|
|
cmd.Dir = es.tmpdir |
|
|
|
|
|
|
|
|
|
func (hcr *HugoCmdRenderer) Render(contents string) error { |
|
|
|
|
err := ioutil.WriteFile(hcr.Filename, []byte(contents), 0644) |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
return idleshut.Config{ |
|
|
|
|
Tick: WebsocketFileTicker, |
|
|
|
|
MaxIdleTicks: uint(IdleWebsocketTimeout/WebsocketFileTicker) + 1, |
|
|
|
|
Start: func() error { |
|
|
|
|
|
|
|
|
|
func (hcr *HugoCmdRenderer) Start() error { |
|
|
|
|
hcr.mtx.Lock() |
|
|
|
|
defer hcr.mtx.Unlock() |
|
|
|
|
err := cmd.Start() |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("error starting hugo: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
hcr.cmd = exec.Command("hugo", "--watch", "-D", "-d", hcr.TmpDir) |
|
|
|
|
hcr.cmd.Dir = hcr.TmpDir |
|
|
|
|
return nil |
|
|
|
|
}, |
|
|
|
|
Stop: func() error { |
|
|
|
|
if cmd == nil { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := hcr.cmd.Start() |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("error starting hugo: %v", err) |
|
|
|
|
} |
|
|
|
|
err := cmd.Process.Signal(os.Interrupt) |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("error signalling hugo to stop: %v", err) |
|
|
|
|
} |
|
|
|
|
err = cmd.Wait() |
|
|
|
|
if err != nil { |
|
|
|
|
return fmt.Errorf("error waiting for hugo to stop: %v", err) |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
es.doc.Close() |
|
|
|
|
os.RemoveAll(es.tmpdir) |
|
|
|
|
delete(ch.docs, es.filename) |
|
|
|
|
|
|
|
|
|
func (hcr *HugoCmdRenderer) Stop() error { |
|
|
|
|
hcr.mtx.Lock() |
|
|
|
|
defer hcr.mtx.Unlock() |
|
|
|
|
|
|
|
|
|
if hcr.cmd == nil { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err := hcr.cmd.Process.Signal(os.Interrupt) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
return nil |
|
|
|
|
}, |
|
|
|
|
IdleProcessError: func(err error) { |
|
|
|
|
fmt.Println("error processing draft:", err) |
|
|
|
|
}, |
|
|
|
|
IdleTick: func() error { |
|
|
|
|
return ioutil.WriteFile(es.filename, []byte(es.doc.Contents()), 0644) |
|
|
|
|
}, |
|
|
|
|
ActiveTick: func() error { |
|
|
|
|
return ioutil.WriteFile(es.filename, []byte(es.doc.Contents()), 0644) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
err = hcr.cmd.Wait() |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|