~/marc

Adding --options to Just Recipes

I've been using Just a lot lately to to help run tasks in my various personal and work projects, and wanted to add options to a Just recipe to further control it.

My use case was to make running Drush on many remote Pantheon environments a bit easier. I wanted to add a --tag option to Drush that would execute Drush commands, via Terminus, on many sites with the passed tag. The Drush commands should then run in parallel, and optionally sequentially when necessary.

For example, just drush --tag tag-name --env dev status to run status on all of the dev sites tagged with tag-name.

There's no built-in way to do this with Just currently, but Just is flexible enough to be able to implement the functionality within a recipe.

This is what I came up with:

Basically, my drush recipe runs just --justfile <the file> _build-taggable-command which will parse the command line options passed to it, and return a just command to run the proper recipe depending on what options were passed.

If --tag and --env are passed, we append -by-tag <tag> <env> to the incoming recipe name (_drush). So running _build-taggable-command _drush --tag the-tag --env dev would output just --justfile <the justfile path> _drush-by-tag the-tag dev. If you hadn't passed --tag and --env, it would use the _drush recipe instead of the _drush-by-tag recipe. So now these options can determine which recipe gets used in the end.

_drush-by-tag will use other recipes to generate a list of site names with the environment appended, and then run terminus drush <site>.<env> -- <the drush command to run> in parallel on all the environments in the tag.