all repos — website @ c258ad4dfcd394c212073d6bd809de9754c30955

My website

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.

Alan Pearce
commit

c258ad4dfcd394c212073d6bd809de9754c30955

parent

9ece57315f2cfee0b2fb64d1fd9fb05fb7fb6301

1 file changed, 20 insertions(+), 9 deletions(-)

changed files
M cmd/build/build.gocmd/build/build.go
@@ -25,6 +25,7 @@ "github.com/a-h/htmlformat"
"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 @@
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)) - } + + ioConfig := IOConfig{} + conf.Parse("", &ioConfig) - outDir := "public" - err = os.RemoveAll("public") + 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")