use storage.Writer interface in builder
Alan Pearce alan@alanpearce.eu
Sun, 26 Jan 2025 21:34:06 +0100
2 files changed, 34 insertions(+), 6 deletions(-)
M internal/builder/builder.go → internal/builder/builder.go
@@ -13,6 +13,7 @@ "go.alanpearce.eu/website/internal/config" "go.alanpearce.eu/website/internal/content" "go.alanpearce.eu/website/internal/sitemap" + "go.alanpearce.eu/website/internal/storage" "go.alanpearce.eu/website/internal/storage/files" "go.alanpearce.eu/website/templates" "go.alanpearce.eu/x/log" @@ -69,11 +70,12 @@ }) } -func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, error) { - storage := files.NewWriter(ioConfig.Destination, log, &files.Options{ - Compress: !ioConfig.Development, - }) - +func build( + storage storage.Writer, + ioConfig *IOConfig, + config *config.Config, + log *log.Logger, +) (*Result, error) { joinSource := joinSourcePath(ioConfig.Source) log.Debug("output", "dir", ioConfig.Destination) @@ -236,5 +238,10 @@ templates.Setup() loadCSS(ioConfig.Source) - return build(ioConfig, cfg, log) + var storage storage.Writer + storage = files.NewWriter(ioConfig.Destination, log, &files.Options{ + Compress: !ioConfig.Development, + }) + + return build(storage, ioConfig, cfg, log) }
A internal/storage/interface.go
@@ -0,0 +1,21 @@+package storage + +import ( + "io" + + "github.com/a-h/templ" +) + +type Writer interface { + Mkdirp(path string) error + + Open(filename string) (io.ReadCloser, error) + + OpenFileAndVariants(filename string) (io.WriteCloser, error) + + OutputToFile(output io.Reader, filename string) error + + RenderToFile(component templ.Component, filename string) error + + WriterToFile(writer io.WriterTo, filename string) error +}