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 2 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"`
Target string `toml:"target"`
WatchDelay string `toml:"watch_delay"`
SyncDir string `toml:"sync_dir"`
}
var Settings config

@ -1,5 +1,5 @@
views = "pages"
layout = "pages/layouts/main.html"
target = "public"
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"
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();
}
}

Loading…
Cancel
Save