staticnotes.org

TIL about just and justfiles

⋅ 2 minute read

just external link is just a command runner that I recently started using to define aliases for commands in my projects, e.g. build, test, or deploy commands.

Installation

just can be installed via homebrew:

1brew install just

Example

I want to create alias commands for my dbt external link project to build my dbt models in production vs. development environments. At the moment, I need to run the commands:

1# prod warehouse
2poetry run dbt run --target prod
3# dev warehouse
4poetry run dbt run --target dev

To define shorter just commands for this project I add a justfile into the project folder.

1# justfile
2run-prod *ARGS:
3    poetry run dbt run --target prod {{ARGS}}
4
5run-dev *ARGS:
6    poetry run dbt run --target dev {{ARGS}}

If I am inside the project folder I can now run just run-prod and just run-dev. Moreover, I can pass in any dbt keyword arguments, e.g.

1just run-dev --select mymodel

will resolve to

1poetry run dbt run --target dev --select mymodel

Is this not just make?

just is inspired by the Unix build tool make external link from 1978. However, just is not a build system and focuses solely on command running. This avoids having to deal with things like .PHONY that can can catch you out in make recipes. I also like two additional features of just:

  1. I can run my just commands from any subfolder of the project folder that contains the justfile. This is useful when I work on this blog. I have to run hugo serve from the top-level project folder and jupyter-to-markdown converter nb2hugo from the notebooks/project_x/ subfolder. With just command aliases it doesn’t matter from which folder I run the command.

  2. just allows you to load environmental variables from an .env file.

    Example .env:

    1DATABASE=localhost:1111
    2USERACCOUNT=rob1234
    

    Add set load-dotenv to your justfile and the environment variables will be available in your shell:

    1set dotenv-load
    2
    3check-vars:
    4    @echo "Env vars are DATABASE:$DATABASE and USER:$USERACCOUNT"                                                                
    

    Running just check-vars will output:

    1Env vars are DATABASE:localhost:1111 and USER:rob1234
    

If you have any thoughts, questions, or feedback about this post, I would love to hear it. Please reach out to me via email.

#dbt