From c258ad4dfcd394c212073d6bd809de9754c30955 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Sun, 21 Apr 2024 13:49:30 +0200 Subject: serve site from filesystem rather than embedding It's cool to embed, but it requires server.go to be at the root. Also, I'd like to be able to update the built site separately in the future. --- cmd/build/build.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'cmd') diff --git a/cmd/build/build.go b/cmd/build/build.go index 058b82f..ea611aa 100644 --- a/cmd/build/build.go +++ b/cmd/build/build.go @@ -25,6 +25,7 @@ import ( "github.com/adrg/frontmatter" "github.com/antchfx/xmlquery" "github.com/antchfx/xpath" + "github.com/ardanlabs/conf/v3" mapset "github.com/deckarep/golang-set/v2" cp "github.com/otiai10/copy" "github.com/pkg/errors" @@ -519,29 +520,39 @@ func build(outDir string) error { return nil } +type IOConfig struct { + Source string `conf:"default:.,short:s"` + Destination string `conf:"default:public,short:d"` +} + func main() { if os.Getenv("DEBUG") != "" { slog.SetLogLoggerLevel(slog.LevelDebug) } slog.Debug("starting build process") - _, err := os.Getwd() - if err != nil { - log.Panic(errors.Errorf("working directory does not exist: %v", err)) - } - outDir := "public" - err = os.RemoveAll("public") + ioConfig := IOConfig{} + conf.Parse("", &ioConfig) + + if ioConfig.Source != "." { + err := os.Chdir(ioConfig.Source) + if err != nil { + log.Panic("could not change to source directory") + } + } + err := os.RemoveAll(ioConfig.Destination) if err != nil { log.Panic(errors.Errorf("could not remove public directory: %v", err)) } - err = cp.Copy("static", outDir, cp.Options{ - PreserveTimes: true, + err = cp.Copy(path.Join(ioConfig.Source, "static"), ioConfig.Destination, cp.Options{ + PreserveTimes: true, + PermissionControl: cp.AddPermission(0755), }) if err != nil { log.Panic(errors.Errorf("could not copy static files: %v", err)) } - if err := build(outDir); err != nil { + if err := build(ioConfig.Destination); err != nil { switch cause := errors.Cause(err).(type) { case *fs.PathError: slog.Info("pathError") -- cgit 1.4.1