about summary refs log tree commit diff stats
path: root/cmd/build
diff options
context:
space:
mode:
authorAlan Pearce2024-04-21 13:49:30 +0200
committerAlan Pearce2024-04-21 13:49:30 +0200
commitc258ad4dfcd394c212073d6bd809de9754c30955 (patch)
tree6b9750c76aa5c0edf164b8fdec0a8838de15af67 /cmd/build
parent9ece57315f2cfee0b2fb64d1fd9fb05fb7fb6301 (diff)
downloadwebsite-c258ad4dfcd394c212073d6bd809de9754c30955.tar.lz
website-c258ad4dfcd394c212073d6bd809de9754c30955.tar.zst
website-c258ad4dfcd394c212073d6bd809de9754c30955.zip
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.
Diffstat (limited to 'cmd/build')
-rw-r--r--cmd/build/build.go29
1 files changed, 20 insertions, 9 deletions
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")