Skip to content

Instantly share code, notes, and snippets.

@Chili-Man
Last active August 29, 2015 14:04
Show Gist options
  • Save Chili-Man/5147f1505a4d57a5059e to your computer and use it in GitHub Desktop.
Save Chili-Man/5147f1505a4d57a5059e to your computer and use it in GitHub Desktop.
Auto Load ChefDK when in a Chef cookbook directory (when a .chedk file is present). You only need one of the files depending on the shell you use.
# For BASH users
# If you use BASH as your main shell, then add these lines to the end our source from a seperate file.
# It will make it so it loads the ChefDK automatically when you enter the chef cookbook directory or
# directory whose ancestor is a cookbook directory and unload the ChefDK when you leave the cookbook directory.
# If you want to force load the ChefDK environment you should:
# load_chefdk 1
#
# Then to force unload:
# unload_chefdk 1
### For working with the ChefDK (with rvm)
function parent_dir() {
path=$PWD
if [ "$#" -eq 1 ]; then path=$1; fi
regex='(.+)(/.+$)'
[[ $path =~ $regex ]]
if [ -z "${BASH_REMATCH[1]}" ]; then
echo '/'
else
echo "${BASH_REMATCH[1]}"
fi
}
function check_for_chefdk_marker() {
cwd=$PWD
while [ "$cwd" != '/' ]; do
[ -f "${cwd}/.chefdk" ] && return 0
cwd=$(parent_dir "$cwd")
done
return 1
}
function __autoload_chefdk() {
if check_for_chefdk_marker; then
load_chefdk
elif [ "$CHEFDK_FORCE_LOADED" != "1" ]; then
unload_chefdk
fi
}
# Force load pass any value after it. You'll need to force unload if you want
# unload the chefdk.
function load_chefdk() {
if [ "$CHEFDK" != "1" ]; then
old_path="$PATH"
old_gem_home="$GEM_HOME"
old_gem_path="$GEM_PATH"
old_gem_root="$GEM_ROOT"
eval "$(chef shell-init bash)"
if [ "$?" -eq 0 ]; then
if [ "$#" -eq 1 ]; then export CHEFDK_FORCE_LOADED=1; fi
export CHEFDK_OLD_PATH="$old_path"
export CHEFDK_OLD_GEM_HOME="$old_gem_home"
export CHEFDK_OLD_GEM_PATH="$old_gem_path"
export CHEFDK_OLD_GEM_ROOT="$old_gem_root"
export CHEFDK=1
echo "ChefDK loaded."
fi
fi
}
# To fore unload, pass any argument to the function.
function unload_chefdk() {
if [ "$CHEFDK" == "1" ]; then
if [ "$#" -eq 1 ]; then export CHEFDK_FORCE_LOADED=0; fi
if [ -n "$CHEFDK_OLD_PATH" ]; then export PATH="$CHEFDK_OLD_PATH"; fi
if [ -n "$CHEFDK_OLD_GEM_HOME" ]; then export GEM_HOME="$CHEFDK_OLD_GEM_HOME"; else unset GEM_HOME; fi
if [ -n "$CHEFDK_OLD_GEM_PATH" ]; then export GEM_PATH="$CHEFDK_OLD_GEM_PATH"; else unset GEM_PATH; fi
if [ -n "$CHEFDK_OLD_GEM_ROOT" ]; then export GEM_ROOT="$CHEFDK_OLD_GEM_ROOT"; else unset GEM_ROOT; fi
unset CHEFDK_OLD_PATH
unset CHEFDK_OLD_GEM_HOME
unset CHEFDK_OLD_GEM_PATH
unset CHEFDK_OLD_GEM_ROOT
unset CHEFDK
echo 'Unloaded ChefDK.'
fi
}
# So that it autoloads by itself
PROMPT_COMMAND=__autoload_chefdk
# For ZSH users
# Add this to your .zshrc or a source it from a seperate file.It will make it so it loads
# the ChefDK automatically when you enter the chef cookbook directory or directory whose ancestor
# is a cookbook directory and unload the ChefDK when you leave the cookbook directory.
# If you want to force load the ChefDK environment you should:
# load_chefdk 1
#
# Then to force unload:
# unload_chefdk 1
# For working with the ChefDK
function parent_dir() {
path=$PWD
if [ "$#" -eq 1 ]; then path=$1; fi
regex='(.+)(/.+$)'
[[ $path =~ $regex ]]
if [ -z "${match[1]}" ]; then
echo '/'
else
echo "${match[1]}"
fi
unset match
}
function check_for_chefdk_marker() {
cwd=$PWD
while [ "$cwd" != '/' ]; do
[ -f "${cwd}/.chefdk" ] && return 0
cwd=$(parent_dir "$cwd")
done
return 1
}
function __autoload_chefdk() {
if check_for_chefdk_marker; then
load_chefdk
elif [ "$CHEFDK_FORCE_LOADED" != "1" ]; then
unload_chefdk
fi
}
# Force load pass any value after it. You'll need to force unload if you want
# unload the chefdk.
function load_chefdk() {
if [ "$CHEFDK" != "1" ]; then
old_path=$PATH
old_gem_home=$GEM_HOME
old_gem_path=$GEM_PATH
old_gem_root=$GEM_ROOT
eval "$(chef shell-init bash)"
if [ "$?" -eq 0 ]; then
if [ "$#" -eq 1 ]; then export CHEFDK_FORCE_LOADED=1; fi
export CHEFDK_OLD_PATH=$old_path
export CHEFDK_OLD_GEM_HOME=$old_gem_home
export CHEFDK_OLD_GEM_PATH=$old_gem_path
export CHEFDK_OLD_GEM_ROOT=$old_gem_root
export CHEFDK=1
echo "ChefDK loaded."
fi
fi
}
# To fore unload, pass any argument to the function.
function unload_chefdk() {
if [ "$CHEFDK" = "1" ]; then
if [ "$#" -eq 1 ]; then export CHEFDK_FORCE_LOADED=0; fi
if [ -n "$CHEFDK_OLD_PATH" ]; then export PATH="$CHEFDK_OLD_PATH"; fi
if [ -n "$CHEFDK_OLD_GEM_HOME" ]; then export GEM_HOME="$CHEFDK_OLD_GEM_HOME"; else unset GEM_HOME; fi
if [ -n "$CHEFDK_OLD_GEM_PATH" ]; then export GEM_PATH="$CHEFDK_OLD_GEM_PATH"; else unset GEM_PATH; fi
if [ -n "$CHEFDK_OLD_GEM_ROOT" ]; then export GEM_ROOT="$CHEFDK_OLD_GEM_ROOT"; else unset GEM_ROOT; fi
unset CHEFDK_OLD_PATH
unset CHEFDK_OLD_GEM_HOME
unset CHEFDK_OLD_GEM_PATH
unset CHEFDK_OLD_GEM_ROOT
unset CHEFDK
echo 'Unloaded ChefDK.'
fi
}
# Add this after the chefdk functions to your .zshrc
function chpwd() {
__autoload_chefdk
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment