diff options
| author | Brandon C. Irizarry <brandon.irizarry@gmail.com> | 2026-03-12 21:35:27 -0400 |
|---|---|---|
| committer | Brandon C. Irizarry <brandon.irizarry@gmail.com> | 2026-03-12 21:35:27 -0400 |
| commit | a663dbdd22a5742e219016d64119036a63e4eaba (patch) | |
| tree | d9ed6ca8f690ab0e33e3f60c7a813ecd5ef8cab0 | |
| parent | 4d196001ecd373243227eaa3c818e4383b2c32a4 (diff) | |
Add code sample
| -rw-r--r-- | posts/buildablog-v2.md | 60 |
1 files changed, 55 insertions, 5 deletions
diff --git a/posts/buildablog-v2.md b/posts/buildablog-v2.md index c19a78e..b05c295 100644 --- a/posts/buildablog-v2.md +++ b/posts/buildablog-v2.md @@ -15,9 +15,9 @@ tags = ["go", "buildablog"] # I Finally Did It -This post is how I solved a problem raised in [a previous -post](/posts/2026-03-06). There, I had left things halfway: I had only installed some -infrastructure, in the form of Cgit, that was partially +I finally solved a problem I mentioned in [a previous post](/posts/2026-03-06). There, +I had left things halfway: I had only installed some infrastructure, +in the form of Cgit, that was partially suggestive of a solution. Here, I document how I finally leveraged that piece as part of a @@ -31,11 +31,61 @@ Previously, the steps for updating my blog's content had been: 4. Perform a `cd` into the `brandons_blog` directory, and run `git pull`. -Now, they're simply: +This has now been reduced to two steps: 1. Commit all changes. 2. Push to `https://git.brandonirizarry.xyz/brandons_blog`. -# `go-git` +# How it Works + +Previous versions of Buildablog would read posts directly from the +local filesystem. For starting out, this was an entirely intuitive and +sensible thing to do. However, I had run into a wall, since I couldn't +push to a non-bare remote, and Buildablog needs to see actual files in +order to publish them. + +I also wanted to keep things conceptually simple and avoid something +like a separate call to `scp` or `rsync` — I would only rely on +Git. After all, this is how Git forges themselves work: push, and +everything is just there, present. Not just present, but presumably +usable in some form or another. I wanted my application to take +advantage of this intuitive simplicity. + +Luckily, [go-git](https://go-git.github.io/docs/) comes to the rescue here. At first I tried to +implement Git-based reads alongside conventional filesystem reads, but +couldn't figure out how to make these two methods play nicely in the +same codebase. So I decided to throw out the latter, relying solely on +reading from a Git repo. The `allArticles` function reads all articles +from the given repo. This is what it currently looks like: + +```go +func allArticles[F types.Frontmatter](repo string) ([]types.Article[F], error) { + fs := memfs.New() + genre := (*new(F)).Genre() + + _, err := git.Clone(memory.NewStorage(), fs, &git.CloneOptions{ + URL: repo, + }) + if err != nil { + return nil, fmt.Errorf("can't clone repository %s: %w", repo, err) + } + + log.Printf("Successfully cloned repository %s", repo) + + entries, err := fs.ReadDir("./" + genre) + if err != nil { + return nil, err + } + + log.Printf("Successfully fetched genre entries for '%s'", genre) + + articles, err := entriesToArticles[F](fs, genre, entries) + if err != nil { + return nil, err + } + + return articles, nil +} +``` |
