Added a sync_dir option to the config so you can have a static directory that gets synced. WARNING: The Go default CopyFS won't copy files to targets that exist so I have to remove the target to do the sync.

master
Zed A. Shaw 3 weeks ago
parent d3d581c783
commit 679aa4650b
  1. 1
      config/loader.go
  2. 2
      example/ssgod.toml
  3. 1
      example/static/subdir/test.html
  4. 3
      example/static/test.css
  5. 35
      main.go

@ -10,6 +10,7 @@ type config struct {
Layout string `toml:"layout"` Layout string `toml:"layout"`
Target string `toml:"target"` Target string `toml:"target"`
WatchDelay string `toml:"watch_delay"` WatchDelay string `toml:"watch_delay"`
SyncDir string `toml:"sync_dir"`
} }
var Settings config var Settings config

@ -1,5 +1,5 @@
views = "pages" views = "pages"
layout = "pages/layouts/main.html" layout = "pages/layouts/main.html"
target = "public" target = "public"
watch_delay = "500ms" watch_delay = "500ms"
sync_dir = "static"

@ -0,0 +1 @@
<h1>I'm a static html file.</h1>

@ -0,0 +1,3 @@
body {
color: #eee;
}

@ -22,8 +22,14 @@ views = "pages"
layout = "pages/layouts/main.html" layout = "pages/layouts/main.html"
target = "public" target = "public"
watch_delay = "500ms" 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 { func Fail(err error, format string, v ...any) error {
err_format := fmt.Sprintf("ERROR: %v; %s", err, format) err_format := fmt.Sprintf("ERROR: %v; %s", err, format)
log.Printf(err_format, v...) log.Printf(err_format, v...)
@ -159,6 +165,32 @@ func ProcessDirEntry(path string, d fs.DirEntry, err error) error {
return nil 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() { func RenderPages() {
err := filepath.WalkDir(config.Settings.Views, err := filepath.WalkDir(config.Settings.Views,
func (path string, d fs.DirEntry, err error) error { func (path string, d fs.DirEntry, err error) error {
@ -196,6 +228,7 @@ func WatchDir() {
doit.Reset(wait_time) doit.Reset(wait_time)
} }
case <-doit.C: case <-doit.C:
SyncStaticDir()
RenderPages() RenderPages()
case err, ok := <-watcher.Errors: case err, ok := <-watcher.Errors:
if !ok { return } if !ok { return }
@ -240,6 +273,7 @@ func main() {
flag.Parse() flag.Parse()
command := flag.Arg(0) command := flag.Arg(0)
switch command { switch command {
case "watch": case "watch":
config.Load(config_file) config.Load(config_file)
@ -248,6 +282,7 @@ func main() {
InitConfig(config_file) InitConfig(config_file)
default: default:
config.Load(config_file) config.Load(config_file)
SyncStaticDir();
RenderPages(); RenderPages();
} }
} }

Loading…
Cancel
Save