From 831b10b09bca8a46c3e16441be81cdc6952ce099 Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Sun, 17 Dec 2017 19:21:00 -0800 Subject: add filesystem watcher --- main.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 5536742..97aad6f 100644 --- a/main.go +++ b/main.go @@ -10,23 +10,25 @@ import ( "adammathes.com/snkt/vlog" "adammathes.com/snkt/web" "fmt" + "github.com/fsnotify/fsnotify" flag "github.com/ogier/pflag" ) func main() { var configFile, init_dir string - var build, preview, version, verbose, help bool + var build, preview, version, verbose, help, watch bool flag.StringVarP(&configFile, "config", "c", "config.yml", "`configuration` file") flag.StringVarP(&init_dir, "init", "i", "", "initialize new site at `directory`") - flag.BoolVarP(&build, "build", "b", false, "build site") - flag.BoolVarP(&preview, "preview", "p", false, "preview site with local HTTP server") - flag.BoolVarP(&help, "help", "h", false, "print help message") - flag.BoolVarP(&verbose, "verbose", "v", false, "log actions during build to STDOUT") + flag.BoolVarP(&build, "build", "b", false, "generates site from input files and templates") + flag.BoolVarP(&preview, "preview", "p", false, "preview site via spawned HTTP server") + flag.BoolVarP(&help, "help", "h", false, "print usage information") + flag.BoolVarP(&verbose, "verbose", "v", false, "log operations during build to STDOUT") + flag.BoolVarP(&watch, "watch", "w", false, "watch configured input text dir, rebuild on changes") flag.Parse() - if !help && !build && !preview && !version && init_dir == "" { + if !watch && !help && !build && !preview && !version && init_dir == "" { flag.Usage() return } @@ -36,26 +38,68 @@ func main() { return } if help { - fmt.Printf("please see README.md\n") + flag.Usage() + fmt.Printf("code and docs: https://github.com/adammathes/snkt \n") return } + config.Init(configFile) if verbose { config.Config.Verbose = true } - render.Init() + if build { - var s site.Site - vlog.Printf("reading posts...\n") - s.Read() - vlog.Printf("writing posts and archives...\n") - s.Write() + buildSite() } if preview { - fmt.Printf("spawning preview at [%s] of [%s]\n", - config.Config.PreviewDir, config.Config.PreviewServer) - web.Serve(config.Config.PreviewServer, config.Config.PreviewDir) + fmt.Printf("spawning preview server [%s] of [%s]\n", + config.Config.PreviewServer, config.Config.PreviewDir) + go func() { + web.Serve(config.Config.PreviewServer, config.Config.PreviewDir) + }() + } + + if watch { + fmt.Printf("watching directory %s\n", config.Config.TxtDir) + watchSite() + } +} + +func buildSite() { + var s site.Site + vlog.Printf("reading posts...\n") + s.Read() + vlog.Printf("writing posts and archives...\n") + s.Write() +} + +func watchSite() { + watcher, err := fsnotify.NewWatcher() + if err != nil { + panic(err) + } + defer watcher.Close() + + done := make(chan bool) + go func() { + for { + select { + case event := <-watcher.Events: + if event.Op&fsnotify.Write == fsnotify.Write { + fmt.Printf("rebuilding\n") + buildSite() + } + case err := <-watcher.Errors: + vlog.Printf("error: %v", err) + } + } + }() + + err = watcher.Add(config.Config.TxtDir) + if err != nil { + panic(err) } + <-done } -- cgit v1.2.3