Homing Pigeon Pattern
Raise your hand if you have run into the following situation. You have a codebase, located in userspace, and you need to load some files from that codebase from a bunch of different places. You can’t guarantee the location of that directory across instances/containers, but you still really want a $REPO_DIR variable you can hit to serve as the root of various relative paths.
There are a lot of ways to solve this problem. Probably too many. I believe I have found, perhaps not the One True Pattern, but at least a Rather Good Pattern for dealing with this.
Put your configs in $CONFIG_HOME
It’s basically that simple.
There are cross-platform libraries (go,
python)
for what is usually called something like $CONFIG_HOME.
On linux, it’s usually at $HOME/.config, and often can be overridden by setting
$XDG_CONFIG_HOME. I still haven’t figured out exactly where the best spot to initialize
this variable is, so for now, I tend to ignore it and just dump configs to
$HOME/.config/myapp/config.yml
This works great out of the box and saves a ton of headache (trust me). But let’s say you still really want to locate the repo directory?
Breadcrumbs back to the repo
Put this file in the root of your repo:
repo_dir.sh:
#!/usr/bin/env bash
HEREDIR=$(cd "$(dirname $(realpath ${0}))" && pwd) # Reliably get the filename and dir
echo ${HEREDIR} # pure function scripts are better than source-ing
Symlink it to your known config location:
ln -sf /path/to/your/app_repo/repo_dir.bash ~/.config/appname/
And access it from any script like such:
REPO_DIR=$(~/.config/appname/repo_dir.sh)
The linking step can be automated with a script which similarly uses the HEREDIR script to
locate the repo in the first place, and then dump the link to .config. Links vs copying
also let you bind-mount your source code inside the container to allow for rapid
development and debugging in-situ.
Better options
Overall, you are probably better off using something like Consul to manage your configs using the dns and kv store features. But that can be a bit cumbersome.
