CLI: Just

  • Ben Burbage
  • Apr 29, 2024
CLI: Just
Useful Programs cli utility

I’m getting older. My memory and my wrists aren’t as nimble as they once were.

When I do get a sliver of time to work on a personal project, it’s nice to have a couple of project specific one-liners that I can run in my terminal to save me from the inevitable chore of re-reading the documentation of whatever build tool or process I want to use.

Just is a tool that supplements my aging brain wonderfully. It’s essentially a command runner in the guise of a Makefile.

All I have to do is craft a small .justfile in the root directory of a project, whilst the commands are all fresh in my mind. When I finally return to my project, I smash just -l into my terminal and I’ll be welcomed by a list of actions I can execute, based on the project I’m in.

Take this blog, for example. Do I remember the hugo new content… command each year I pressure myself into writing a new blog post? Hell no!

I have a .justfile for my blog, and after checking my usual actions with just (no -l needed for me, you’ll see why), I’m reminded that I can run just post followed by a post title to create a new blog post and open it in a text editor.

Here’s my blog’s .justfile in all its glory:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# `just` override
_default:
    just -l

# Serve the site on localhost
serve:
    hugo serve -D

# Create a new blog post (titles-like-this)
post title:
    hugo new content posts/{{title}}.md
    mkdir -p ./content/posts/{{title}}/assets
    mv ./content/posts/{{title}}.md ./content/posts/{{title}}/index.md
    hx ./content/posts/{{title}}/index.md

# Start a tmux session
tmux:
    tmux new -Pd -s blog -n code
    tmux neww -Pd -t blog -F 2 -n serve
    tmux send-keys -t blog:2 "just serve" Enter
    firefox http://localhost:1313/ &
    tmux attach -d -t blog

Here, there are four recipes, or actions as I sometimes call them. The _default recipe means that I can use the just command without the -l to list my available actions. The serve recipe simply gets my website running locally.

The post recipe creates a new blog entry with a title that I provide at invocation (just post super-sexy-catchy-post-title), wrangles that post into the directory structure that I like and then opens that new post in my text editor of the day.

Finally, the tmux recipe spawns a new tmux session with two windows - one for activities and one for serving my website locally. It also cracks open a browser window, loading the locally hosted instance of my site. This is useful for when I might decide to work on my theme or site functionality.

Using this tool allows me to start being productive quickly, with less typing - saving my poor wrists from extraneous fatigue.

Here’s an example of a .justfile that I might use for in a directory that contains a number of dockerfiles:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Variables for defaults
defaultFile := 'bestOne'

# `just` override
_default:
	just -l

# Build a dockerfile
build file=defaultFile:
	docker build -f ./{{file}}.dockerfile -t {{file}}:taaaag .

# Build and run a dockerfile
run file=defaultFile: (build file)
	docker run -it --rm {{file}}:taaaag

# (Hidden) Build and Tag container image
_tag version file=defaultFile: (build file)
	docker tag {{file}}:taaaag container-registry.com/{{file}}:{{version}}

# Build, Tag and Push a container image
publish version file=defaultFile: (_tag version file)
	docker push container-registry.com/{{file}}:{{version}}

This one’s a little more complex but it shows how you can chain recipes together. For example, the run recipe will first run the build recipe.

I won’t go into much more detail here - hopefully you see how I find this utility useful. If you’re interested in checking Just out, it’s documentation is pretty thorough.

Comments

You can use your Mastodon account to comment on this post by commenting on this thread.