Command Line Blogging in 2 Commands
samwho keyboard logo

Command Line Blogging in 2 Commands

It's exam season for me at the moment and I really hate revising. Because of that, I'm doing what I call "useful procrastination". I work on something useful that isn't to do with exams for the sake of avoiding having to revise.

During the most recent round of useful procrastination, I created something I'm calling "echochamber", which gives me the ability to write and deploy a blog post in two simple commands.

Prerequisite knowledge: Some command line fu and perseverance if you want to get this working for yourself.

Before we start, I want to say that this is very DIY. I made echochamber specifically for myself. There's nothing stopping you taking my code and adapting it to your own purposes but it really isn't designed for that and there will be work involved in getting it working on your system. It's better to understand how I've done it rather than trying to take my code and retrofit it to your environment. In future, I might work on making echochamber something that can be distributed as a gem.

# Writing a Post

So, here's how a typical new blog post is started with echochamber:

$ echochamber "This is a post title"

That will open up whatever my editor is, defined by the EDITOR environment variable, with some "front matter" at the top. The term "front matter" will be explained later.

# Deployment

Done writing?

$ echochamber deploy

Done.

# Wait, how does that work?

This is the part where it becomes evident why this project is specific to my setup.

# Deployment

The actual website I publish to, echo.samwho.co.uk is hosted on Heroku. This is what the deploy command does in the background:

$ cd /path/to/echochamber/project
$ jekyll
$ git add -A
$ git commit
$ git push heroku master

In this process, I get prompted for a commit message thanks to "git commit" not receiving a -m argument. The jekyll command regenerates the website using Tom Preston-Werner's awesome jekyll project. If you haven't already, I recommend you check it out.

# Post creation

As the previous section noted, echochamber uses the Jekyll project at its core. Jekyll is a static site generator that allows you to quickly create a website structure and get a blog off the ground. It's well worth looking up how it works if you want to mess around with the code in echochamber.

Jekyll uses a very specific format for naming posts. Posts must be named YYYY-MM-DD-title.format, where format can be markdown, textile or a few other things. The echochamber command looks for your editor in the EDITOR environment variable or defaults to vim if it doesn't find anything.

Currently it's a bit hacky. If it finds vim, it sends some specific commands for populating the post and moving the cursor to a convenient location ready for typing. This is convenient to me and if you use a different editor, the post will initially have nothing in it. Jekyll allows you to have front matter, which is YAML data that you can attach to your post. Stuff things is post title, published date and layout (to find out more about layouts, click here).

If you aren't using vim, you will need to include the front matter manually or send me a pull request with code specific to your favourite editor.

# Previewing posts

Echochamber comes with the ability to preview posts before you post them:

$ echochamber preview

This is essentially just a delegation to the following command:

$ jekyll --server --auto

Which runs the Jekyll server on port 4000 and automatically regenerates the site if files change. Nifty to have running while you write your post.

# Continuing where you left off

Because it's not very often a blog post is written in one sitting, echochamber has a command that lets you reopen the latest post you were working on:

$ echochamber latest

This will literally just open up the last edited post, based on file change times, in your editor (again, based on the EDITOR environment variable).

# Setting up on your machine

The process is very cumbersome at the moment. Here's what you would have to do.

# Add the bin/ directory to your PATH

Using whatever method you prefer, you would need to add the bin/ directory under echochamber to your PATH. Personally I have this line in my .bashrc:

PATH=$PATH:/path/to/echochamber/bin

Alternately, you could add an alias:

alias echochamber='/path/to/echochamber/bin/echochamber'

# Adding your Heroku remote

This will definitely need to change in a future iteration. What if you don't want to deploy to Heroku, for example? But for now, you would kind of have to unless you wanted to edit the code, which I would be totally cool about.

You would need to create a new application on Heroku and add that remote to the echochamber git repository on your system (you would, of course, not to clone the repository to do any of this).

Heroku have a great getting started guide. I recommend you read over it to get a feel for how Heroku works and learn how to setup Heroku and create an application. You will need to create an application in the echochamber directory for echochamber deploy to work.

# Moving forward

Yes, this is still rough around the edges. I just wanted to show off the mechanism I had created for making blogging easy :) In future I think I will probably be making a lot of changes to how echochamber works so that it's much, much more usable for others. Until then, if you really are compelled to use it and aren't having any luck getting it working, drop my an email! You can find it in my contact page up at the top.

powered by buttondown