Skip to content

Instantly share code, notes, and snippets.

@joekiller
Last active November 21, 2016 18:43
Show Gist options
  • Save joekiller/d9c29a17dfcd2edc17142c762a4126b0 to your computer and use it in GitHub Desktop.
Save joekiller/d9c29a17dfcd2edc17142c762a4126b0 to your computer and use it in GitHub Desktop.
alias bash profiles based on client names

Say you support multiple clients or multiple projects but don't want everything on your shell at once. This script in your .bashrc file will allow you to automatically create aliases for any directory that has it's own .bashrc file. It starts a new bash shell so no existing variables are overwritten and you can exit when needed.

For example, say you have project or client named superx and another named johndoe. You create a directory superx and johndoe. Within each you pust a custom .bashrc file and optional .superx-secrets and .johndoe-secrets file and then your shell (upon next login) will detect those files and alias them to the parent directory's name. So if I want superx stuff, I just run superx and vice versa for johndoe.

Some references:

http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html#cli-environment http://www.linuxjournal.com/content/tech-tip-dereference-variable-names-inside-bash-functions http://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash

UnsetThenSetVariable() {
if [ ! -z ${1+x} ]; then
eval unset ${1}
fi
eval export ${1}=${2}
}
InitShellAlias () {
ALIAS="bash --init-file <(echo \". /etc/profile;. $HOME/.bash_profile;$(if [[ -f $1 ]]; then echo '. '$1';';fi)"
}
DynamicAlias () {
[[ ! -z ${ALIAS} ]] && alias $(echo $1)="${ALIAS}$([[ ! -z $2 ]] && echo "cd $2;")\")"
}
# CLIENT ALIASES
ROOT_DIRS=$(find $HOME -maxdepth 1 ! -path . -type d ! -path $HOME'/.*' -print | tr '\n' ' ')
for path in $ROOT_DIRS; do
ALIAS=
FULL_PATH=$(cd ${path}; pwd)
[[ -f ${FULL_PATH}/.bashrc ]] && InitShellAlias ${FULL_PATH}/.bashrc
[[ -f ${FULL_PATH}/.$(basename ${path})-secrets ]] && ALIAS="${ALIAS}. ${FULL_PATH}/.$(basename ${path})-secrets;"
DynamicAlias $(basename ${path})
done
# END CLIENT ALIASES
# PROJECT ALIASES
ProjectAlias () {
ALIAS=
InitShellAlias $2
DynamicAlias $1 $3
}
# END PROJECT ALIASES
ProjectAlias myproject $HOME/some/python/virtualenv/bin/activate $HOME/path/to/myproject
HOME_VAR=`basename $(dirname $( readlink -f ${BASH_SOURCE[0]}))`_HOME
eval export ${HOME_VAR}="$(dirname $( readlink -f ${BASH_SOURCE[0]}))"
parent_dir=`basename ${!HOME_VAR}`
command -v deactivate >/dev/null 2>&1 && deactivate
python_dir=${HOME}/python/${parent_dir}
if [ -d "${python_dir}" ]; then
. ${python_dir}/bin/activate
PATH=$PATH:${python_dir}/bin
if [ -f "${python_dir}/bin/aws_completer" ]; then
complete -C "${python_dir}/bin/aws_completer" aws
fi
fi
cd ${!HOME_VAR}
UnsetThenSetVariable AWS_ACCESS_KEY_ID AKIAIOSFODNN7EXAMPLE
UnsetThenSetVariable AWS_SECRET_ACCESS_KEY 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
UnsetThenSetVariable AWS_DEFAULT_REGION us-east-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment