all repos — website @ 7d9e98a0996eca0372e38cee4b8826d84a9ace2b

My website

refactor outputs->storage for generalisation
Alan Pearce alan@alanpearce.eu
Sun, 26 Jan 2025 11:52:12 +0100
commit

7d9e98a0996eca0372e38cee4b8826d84a9ace2b

parent

93c01942cb379b448dafab7ceffd78c005772928

2 files changed, 28 insertions(+), 28 deletions(-)

jump to
M internal/builder/builder.gointernal/builder/builder.go
@@ -9,8 +9,8 @@ "time" 
 	"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 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 @@ r := &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 @@ 	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 @@ } 		}
 		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 @@ ) 		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 @@ feed, err := renderFeed(config.Title, config, posts, "feed") 	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 @@ feedStyles, err := renderFeedStyles(ioConfig.Source) 	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 @@ content, err := content.RenderMarkdown(text) 	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 @@ h, _ = getHTMLStyleHash(f) 	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 @@ rob, err := renderRobotsTXT(ioConfig.Source, config) 	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
 	}
 
M internal/outputs/files.gointernal/storage/files/writer.go
@@ -1,4 +1,4 @@-package outputs
+package files
 
 import (
 	"compress/gzip"
@@ -21,25 +21,25 @@ gzipLevel   = 6 	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 @@ return nil 	})
 }
 
-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 @@ 	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 @@ 	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 @@ multiWriter: io.MultiWriter(r, gz, br), 	}, 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)