Path it your way

I really like the cool products the team at HashiCorp cook up.  Unless you have been frozen in carbonite for the past 3+ years, you’ve likely used or heard about Vagrant.  What a wonderful tool that lets you rapidly build and test on VMs and containers on VMs using Virtualbox.  As if that wasn’t awesome enough, this past year Mitchell and his team have introduced multiple awesome projects including terraform, consul, serf, and packer.  I wanted to get my hands on these tools and use them locally on my Mac.  As with Vagrant, each of these products are packaged into a nice downloadable binary which you can then extract to the directory of your choice for use.   However, after reading the documentation carefully, you’ll find that setting your path to these executables is a key part of your environment setup.

Setting path variables is not a big deal at all.  However, I wanted all my HashiCorp products executables to live in a specific directory configuration.

mkdir -p $HOME/hashicorp/{consul,packer,serf,terraform}

$tree $HOME/hashicorp -d
/$HOME/hashicorp
├── consul
├── packer
├── serf
└── terraform

Since there are multiple projects from the same company to be used, I wanted each one in its own subdirectory.  Rather than list each HashiCorp product in my .bash_profile PATH statements, and to prevent other things from getting broken in the process of editing the paths (RVM, etc), I decided to dig around on our good ole friend StackExchange and found some good tips for doing paths for multiple subdirectories.

So, I did the following in .bash_profile to have the shell iterate over each subdirectory and add to the $PATH export.

$vim .bash_profile
# HASHICORP PATHS
for p in $HOME/hashicorp/*/; do
 export HASHIPATH="$p"
 PATH="$PATH:$HASHIPATH"
done

Note: It is likely a security anti-pattern to have recursive paths if I were to get malicious code from Hashicorp since I am setting up a specific path root for all the tools from them I intend to drop into this directory structure. Again, this structure is just a personal preference to keep everything in a single root directory with release versions. I could have chosen to do symlinks or symlink tools like stow, but I just wanted something quick and functional with the directory structure I desired.

Now, the next awesome product that the folks at HashiCorp release that I want to use, I can simply drop it into the subdirectory of my choice with the versioning directory I like, and I’m good to go.