about summary refs log tree commit diff stats
path: root/internal
diff options
context:
space:
mode:
authorAlan Pearce2025-01-26 11:52:12 +0100
committerAlan Pearce2025-01-26 13:38:16 +0100
commit7d9e98a0996eca0372e38cee4b8826d84a9ace2b (patch)
tree6ae88100a923012f04ec316a45990b6902140136 /internal
parent93c01942cb379b448dafab7ceffd78c005772928 (diff)
downloadwebsite-7d9e98a0996eca0372e38cee4b8826d84a9ace2b.tar.lz
website-7d9e98a0996eca0372e38cee4b8826d84a9ace2b.tar.zst
website-7d9e98a0996eca0372e38cee4b8826d84a9ace2b.zip
refactor outputs->storage for generalisation
Diffstat (limited to 'internal')
-rw-r--r--internal/builder/builder.go30
-rw-r--r--internal/storage/files/writer.go (renamed from internal/outputs/files.go)26
2 files changed, 28 insertions, 28 deletions
diff --git a/internal/builder/builder.go b/internal/builder/builder.go
index 6e5cfc9..68eec14 100644
--- a/internal/builder/builder.go
+++ b/internal/builder/builder.go
@@ -9,8 +9,8 @@ import (
 
 	"go.alanpearce.eu/website/internal/config"
 	"go.alanpearce.eu/website/internal/content"
-	"go.alanpearce.eu/website/internal/outputs"
 	"go.alanpearce.eu/website/internal/sitemap"
+	"go.alanpearce.eu/website/internal/storage/files"
 	"go.alanpearce.eu/website/templates"
 	"go.alanpearce.eu/x/log"
 
@@ -35,8 +35,8 @@ func joinSourcePath(src string) func(string) string {
 }
 
 func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, error) {
-	output := outputs.NewFilesOutput(ioConfig.Destination, log, &outputs.Options{
-		CompressFiles: !ioConfig.Development,
+	storage := files.NewWriter(ioConfig.Destination, log, &files.Options{
+		Compress: !ioConfig.Development,
 	})
 
 	joinSource := joinSourcePath(ioConfig.Source)
@@ -46,7 +46,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result,
 		Hashes: make([]string, 0),
 	}
 
-	err := output.CopyRecursive(joinSource("static"))
+	err := storage.CopyRecursive(joinSource("static"))
 	if err != nil {
 		return nil, errors.WithMessage(err, "could not copy static files")
 	}
@@ -69,13 +69,13 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result,
 	for _, post := range posts {
 		log.Debug("rendering post", "post", post.Basename)
 		sitemap.AddPath(post.URL, post.Date)
-		if err := output.RenderToFile(templates.PostPage(config, post), post.Output); err != nil {
+		if err := storage.RenderToFile(templates.PostPage(config, post), post.Output); err != nil {
 			return nil, err
 		}
 	}
 
 	log.Debug("rendering tags list")
-	if err := output.RenderToFile(
+	if err := storage.RenderToFile(
 		templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"),
 		path.Join("tags", "index.html"),
 	); err != nil {
@@ -92,7 +92,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result,
 		}
 		log.Debug("rendering tags page", "tag", tag)
 		url := "/tags/" + tag
-		if err := output.RenderToFile(
+		if err := storage.RenderToFile(
 			templates.TagPage(config, tag, matchingPosts, url),
 			path.Join("tags", tag, "index.html"),
 		); err != nil {
@@ -110,13 +110,13 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result,
 		if err != nil {
 			return nil, errors.WithMessage(err, "could not render tag feed page")
 		}
-		if err := output.WriterToFile(feed, path.Join("tags", tag, "atom.xml")); err != nil {
+		if err := storage.WriterToFile(feed, path.Join("tags", tag, "atom.xml")); err != nil {
 			return nil, err
 		}
 	}
 
 	log.Debug("rendering list page")
-	if err := output.RenderToFile(
+	if err := storage.RenderToFile(
 		templates.ListPage(config, posts, "/post"),
 		path.Join("post", "index.html"),
 	); err != nil {
@@ -129,7 +129,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result,
 	if err != nil {
 		return nil, errors.WithMessage(err, "could not render feed")
 	}
-	if err := output.WriterToFile(feed, "atom.xml"); err != nil {
+	if err := storage.WriterToFile(feed, "atom.xml"); err != nil {
 		return nil, err
 	}
 
@@ -138,7 +138,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result,
 	if err != nil {
 		return nil, errors.WithMessage(err, "could not render feed styles")
 	}
-	if err := output.OutputToFile(feedStyles, "feed-styles.xsl"); err != nil {
+	if err := storage.OutputToFile(feedStyles, "feed-styles.xsl"); err != nil {
 		return nil, err
 	}
 	_, err = feedStyles.Seek(0, 0)
@@ -160,14 +160,14 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result,
 	if err != nil {
 		return nil, err
 	}
-	if err := output.RenderToFile(templates.Homepage(config, posts, content), "index.html"); err != nil {
+	if err := storage.RenderToFile(templates.Homepage(config, posts, content), "index.html"); err != nil {
 		return nil, err
 	}
 	// it would be nice to set LastMod here, but using the latest post
 	// date would be wrong as the homepage has its own content file
 	// without a date, which could be newer
 	sitemap.AddPath("/", time.Time{})
-	f, err := output.Open("index.html")
+	f, err := storage.Open("index.html")
 	if err != nil {
 		return nil, err
 	}
@@ -176,7 +176,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result,
 	r.Hashes = append(r.Hashes, h)
 
 	log.Debug("rendering sitemap")
-	if err := output.WriterToFile(sitemap, "sitemap.xml"); err != nil {
+	if err := storage.WriterToFile(sitemap, "sitemap.xml"); err != nil {
 		return nil, err
 	}
 
@@ -185,7 +185,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result,
 	if err != nil {
 		return nil, err
 	}
-	if err := output.OutputToFile(rob, "robots.txt"); err != nil {
+	if err := storage.OutputToFile(rob, "robots.txt"); err != nil {
 		return nil, err
 	}
 
diff --git a/internal/outputs/files.go b/internal/storage/files/writer.go
index e8da259..6f10fe7 100644
--- a/internal/outputs/files.go
+++ b/internal/storage/files/writer.go
@@ -1,4 +1,4 @@
-package outputs
+package files
 
 import (
 	"compress/gzip"
@@ -21,25 +21,25 @@ const (
 	brotliLevel = 9
 )
 
-type FilesOutput struct {
+type Files struct {
 	outputDirectory string
 	options         *Options
 	log             *log.Logger
 }
 
 type Options struct {
-	CompressFiles bool
+	Compress bool
 }
 
-func NewFilesOutput(outputDirectory string, logger *log.Logger, opts *Options) *FilesOutput {
-	return &FilesOutput{
+func NewWriter(outputDirectory string, logger *log.Logger, opts *Options) *Files {
+	return &Files{
 		outputDirectory: outputDirectory,
 		options:         opts,
 		log:             logger,
 	}
 }
 
-func (f *FilesOutput) CopyRecursive(src string) error {
+func (f *Files) CopyRecursive(src string) error {
 	return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
 		if err != nil {
 			return err
@@ -70,11 +70,11 @@ func (f *FilesOutput) CopyRecursive(src string) error {
 	})
 }
 
-func (f *FilesOutput) Open(filename string) (io.ReadCloser, error) {
+func (f *Files) Open(filename string) (io.ReadCloser, error) {
 	return os.Open(filepath.Join(f.outputDirectory, filename))
 }
 
-func (f *FilesOutput) OutputToFile(output io.Reader, filename string) error {
+func (f *Files) OutputToFile(output io.Reader, filename string) error {
 	fn := path.Join(f.outputDirectory, filename)
 	if err := f.mkdirp(filepath.Dir(filename)); err != nil {
 		return err
@@ -93,7 +93,7 @@ func (f *FilesOutput) OutputToFile(output io.Reader, filename string) error {
 	return nil
 }
 
-func (f *FilesOutput) RenderToFile(component templ.Component, filename string) error {
+func (f *Files) RenderToFile(component templ.Component, filename string) error {
 	fn := path.Join(f.outputDirectory, filename)
 	if err := f.mkdirp(filepath.Dir(filename)); err != nil {
 		return err
@@ -112,7 +112,7 @@ func (f *FilesOutput) RenderToFile(component templ.Component, filename string) e
 	return nil
 }
 
-func (f *FilesOutput) WriterToFile(writer io.WriterTo, filename string) error {
+func (f *Files) WriterToFile(writer io.WriterTo, filename string) error {
 	fn := path.Join(f.outputDirectory, filename)
 	if err := f.mkdirp(filepath.Dir(filename)); err != nil {
 		return err
@@ -196,15 +196,15 @@ func multiOpenFile(filename string) (*MultiWriteCloser, error) {
 	}, nil
 }
 
-func (f *FilesOutput) openFileAndVariants(filename string) (io.WriteCloser, error) {
-	if f.options.CompressFiles {
+func (f *Files) openFileAndVariants(filename string) (io.WriteCloser, error) {
+	if f.options.Compress {
 		return multiOpenFile(filename)
 	}
 
 	return openFileWrite(filename)
 }
 
-func (f *FilesOutput) mkdirp(dir string) error {
+func (f *Files) mkdirp(dir string) error {
 	f.log.Debug("creating directory", "dir", dir)
 	err := os.MkdirAll(path.Join(f.outputDirectory, dir), 0755)