Making The Blog: First Thoughts
March 31, 2024This is a general series about the technicals of the blog. How are things implemented, why these decisions were made, and you would use the software to do such a thing yourself.
If you'd like to follow along at home, you can grab the repo at git.glaka.us/git/dewblog.git.
Writing from where I am now, I have static content generation set up. This means that
I have a program, dewblog
, written in go.
The dewblog binary uses the subcommands
module to implement a few different features.
- findroot, in order to discover where the root of the blog would be.
- init, in order to initialize the blog file and specify a root.
- build, which generates static HTML pages from .page files and .tmpl templates.
Hello, World!
Unfortunately, I'd already installed go, so you don't get to read another "How to install Go" manual. Darn.
The first thing I wrote was the README, this is similar to the Amazon practice of writing the press release for a project first. Toxic workplaces aside, this is a pretty interesting way of focusing the MVP of the project. You can read it, but the initial version described the three above commands.
Knowing all that, I went for the simplest one of the three commands, findroot
.
The logic this command reports is a dependency of the other two, so working out how to
do it was good to tackle first.
findroot
The findRoot
function is fairly basic.
func findRoot(filepath string) (string, os.FileInfo, error) {
cleanpath := path.Clean(filepath)
parent := cleanpath
for {
newpath := path.Join(parent, configFile)
match, err := doesPathMatch(newpath)
if err == nil {
return parent, match, nil
}
last := parent
parent = path.Dir(parent)
if parent == last {
return "", nil, fmt.Errorf("No root file found")
}
}
}
// Search for the dewblog config file in this or any ancestor directory.
func doesPathMatch(filepath string) (os.FileInfo, error) {
base := path.Base(filepath)
if base != configFile {
return nil, fmt.Errorf("Not a matching file")
}
fileinfo, err := os.Stat(filepath)
if err != nil {
// println("Error reading path file: ", err.Error())
return nil, err
}
if fileinfo.IsDir() {
return nil, fmt.Errorf("%s is not a readable, regular file", filepath)
}
return fileinfo, nil
}
- Give it a path, it'll clean it up.
- For each path, check if
"${path}/blogcfg"
exists - If it does, return the path.
- If not, call
filepath.Dir
to go up. - Detect if we're at the root, return, otherwise we can loop back to step 2.
I think I'll stop for tonight. Next stop: initializing, then building the blog