diff --git a/config/loader.go b/config/loader.go index a614c1d..9edf0e7 100644 --- a/config/loader.go +++ b/config/loader.go @@ -10,6 +10,7 @@ type config struct { Layout string `toml:"layout"` Target string `toml:"target"` WatchDelay string `toml:"watch_delay"` + SyncDir string `toml:"sync_dir"` } var Settings config diff --git a/example/ssgod.toml b/example/ssgod.toml index 0fb1e95..71a1a2c 100644 --- a/example/ssgod.toml +++ b/example/ssgod.toml @@ -1,5 +1,5 @@ - views = "pages" layout = "pages/layouts/main.html" target = "public" watch_delay = "500ms" +sync_dir = "static" diff --git a/example/static/subdir/test.html b/example/static/subdir/test.html new file mode 100644 index 0000000..70f7ad9 --- /dev/null +++ b/example/static/subdir/test.html @@ -0,0 +1 @@ +

I'm a static html file.

diff --git a/example/static/test.css b/example/static/test.css new file mode 100644 index 0000000..6b40b71 --- /dev/null +++ b/example/static/test.css @@ -0,0 +1,3 @@ +body { + color: #eee; +} diff --git a/main.go b/main.go index bd95803..8ea2bf0 100644 --- a/main.go +++ b/main.go @@ -22,8 +22,14 @@ views = "pages" layout = "pages/layouts/main.html" target = "public" watch_delay = "500ms" +sync_dir = "static" ` +func Fatal(err error, format string, v ...any) { + err_format := fmt.Sprintf("ERROR: %v; %s", err, format) + log.Fatalf(err_format, v...) +} + func Fail(err error, format string, v ...any) error { err_format := fmt.Sprintf("ERROR: %v; %s", err, format) log.Printf(err_format, v...) @@ -159,6 +165,32 @@ func ProcessDirEntry(path string, d fs.DirEntry, err error) error { return nil } +func SyncStaticDir() { + target := config.Settings.Target + sync_dir := config.Settings.SyncDir + if sync_dir == "" { return } + + log.Printf("removing target directory: %s", target) + + err := os.RemoveAll(target) + if err != nil { + Fatal(err, "can't remove target directory: %s", target) + } + + err = os.MkdirAll(target, 0750) + if err != nil { + Fatal(err, "can't recreate target directory: %s", target) + } + + source := os.DirFS(sync_dir) + + log.Printf("SYNC %s -> %s", sync_dir, target) + err = os.CopyFS(target, source) + if err != nil { + Fatal(err, "can't sync %s to target directory: %s", sync_dir, target) + } +} + func RenderPages() { err := filepath.WalkDir(config.Settings.Views, func (path string, d fs.DirEntry, err error) error { @@ -196,6 +228,7 @@ func WatchDir() { doit.Reset(wait_time) } case <-doit.C: + SyncStaticDir() RenderPages() case err, ok := <-watcher.Errors: if !ok { return } @@ -240,6 +273,7 @@ func main() { flag.Parse() command := flag.Arg(0) + switch command { case "watch": config.Load(config_file) @@ -248,6 +282,7 @@ func main() { InitConfig(config_file) default: config.Load(config_file) + SyncStaticDir(); RenderPages(); } }