Skip to content

Instantly share code, notes, and snippets.

@andrepg
Created October 26, 2023 19:21
Show Gist options
  • Save andrepg/2ae1c471b11d563f1f40e789d767cb52 to your computer and use it in GitHub Desktop.
Save andrepg/2ae1c471b11d563f1f40e789d767cb52 to your computer and use it in GitHub Desktop.
Load custom bash profile
# Loads Toolbox's environment variables, by reading specific files
# based on current OCI/container name, searching in a default folder
# for a .bashrc_{container_name} profile
function load_toolbox_profile() {
CONTAINERENV=/run/.containerenv
BASH_PROFILES=$HOME/.config/toolbox/profiles
# Any other value than null here means that we are inside a toolbox
if [[ -f "$CONTAINERENV" ]]; then
## We get the container's name and clean the quotes
TOOLBOX_NAME=$(awk -F'=' '/name/ { print $2}' $CONTAINERENV)
TOOLBOX_NAME=$(sed -e 's/^"//' -e 's/"$//' <<< $TOOLBOX_NAME)
## Defines which bash profile we should load, based on toolbox name
TOOLBOX_PROFILE="${BASH_PROFILES}/profile_${TOOLBOX_NAME}"
## Echo some strings to inform that we're inside a toolbox
echo "Hello! You're entering a Toolbox. We're setting up a few things."
echo "> toolbox name: ${TOOLBOX_NAME}"
echo "> bash profile: ${TOOLBOX_PROFILE}"
## Now we'll read the profile, if exists, using source
if [[ -f $TOOLBOX_PROFILE ]]; then
source $TOOLBOX_PROFILE
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment