brewi
Preview¶
1 2 3 4 5 6 7 8 9 10 11 | |
> brewi -h
Facilitates checking a brew formula before installing it.
Usage:
brewi <formula> [cask]
Script args:
--formula str Name of the formula to install.
-c, --cask Enable if it's a cask.
Tutorial: Building brewi¶
Motivation¶
I tend to run brew info before installing formulas just to double-check that I've got the right one.
Most of the time, it is, so I follow that with a brew install.
Rather than writing out these two commands manually each time, it'd be neat if I had an alias, which not only saved some characters, but did this workflow for me.
Writing the script¶
We can use rad to create the script file for us. The -s flag gives us a minimal template with just a shebang, and -o code opens it in VS Code.
rad new brewi -s -o code
First, we want to quickly describe what the script is aiming to do, so we'll add a file header.
1 2 3 4 | |
We want the script to accept a formula as an argument, i.e. the formula we may be installing. It'll be a string, so let's add this in an arg block. We'll include a little comment to improve our usage string.
1 2 3 4 5 6 | |
formula will serve both as the variable name for the rest of the script, and be exposed to the user as the script's CLI API.
Setup done. First thing we'll wanna do is run brew info with the formula. We'll do this via a shell command.
Specifically, we'll use a critical shell command, because if the command fails (including if the formula just doesn't exist), we want to just print the error and exit the script.
This uses the $ syntax.
1 2 3 4 5 6 7 8 | |
We use string interpolation to insert the formula into the command.
You can try running the command now! Make sure it's executable (chmod +x ./brewi).
Next, we want to ask the user if they'd like to proceed with installing the formula. For that, Rad offers the confirm function.
The default prompt is Confirm? [y/n] >, which works fine for us here, so we'll do a simple 0-arg confirm() call. The function returns a bool for yes/no, so we'll put it in an if statement.
1 2 3 4 5 6 7 8 9 10 | |
Feel free to try it again now!
One last touch: we should also allow installing casks with this script. We'll aim to offer a simple -c flag users can set which modifies the command.
We'll add the bool arg, and insert an additional interpolation in our brew install command, leveraging Rad's ternary syntax.
We need to pay close attention to whitespace so we make sure the command comes out correct in the cask and non-cask cases.
1 2 3 4 5 6 7 8 9 10 11 | |
Note the shorthand flag in cask c bool.
Done! You've now got a great, convenient helper script for installing things with brew :)
Concepts demonstrated¶
| Concept | Where |
|---|---|
| File header | Script description for --help |
| Args block | formula str, cask c bool |
| Short flags | cask c bool gives -c |
| Shell commands | $\brew info {formula}`` |
| String interpolation | {formula} in shell command |
confirm() |
Interactive yes/no prompt |
| Ternary operator | cask ? " --cask" : "" |