if command -v ::KABU:: >/dev/null 2>&1; then
source <(::KABU:: completions bash)
fi
__kabu_cmd() {
::KABU:: "$@"
}
kabu() {
if [ "${1:-}" = "cd" ] && [ $# -eq 1 ]; then
local dest
dest=$(__kabu_cmd path) || return $?
if [ -n "$dest" ]; then
builtin cd "$dest"
fi
elif [ "${1:-}" = "add" ]; then
local cd_to
cd_to=$(__kabu_cmd config get auto_cd.after_add 2>/dev/null) || cd_to=""
local tmpfile
tmpfile=$(mktemp)
__kabu_cmd "$@" 2>&1 | tee "$tmpfile"
local cmd_status=${PIPESTATUS[0]}
if [ $cmd_status -eq 0 ] && [ "$cd_to" = "true" ]; then
local new_path
new_path=$(tail -1 "$tmpfile")
if [ -d "$new_path" ]; then
builtin cd "$new_path"
fi
fi
rm -f "$tmpfile"
return $cmd_status
elif [ "${1:-}" = "remove" ] || [ "${1:-}" = "rm" ]; then
local current_dir="$PWD"
local cd_to
cd_to=$(__kabu_cmd config get auto_cd.after_remove 2>/dev/null) || cd_to=""
local main_path
main_path=$(__kabu_cmd path --main 2>/dev/null) || main_path=""
__kabu_cmd "$@" || return $?
if [ ! -d "$current_dir" ]; then
case "$cd_to" in
main)
if [ -n "$main_path" ]; then
builtin cd "$main_path"
fi
;;
select)
local dest
dest=$(__kabu_cmd path) || return $?
if [ -n "$dest" ]; then
builtin cd "$dest"
fi
;;
esac
fi
else
__kabu_cmd "$@"
fi
}
__kabu_trust_check() {
local root config_path current_mtime
root=$(git rev-parse --show-toplevel 2>/dev/null) || {
__kabu_trust_root=""
__kabu_trust_config_mtime=""
__kabu_trust_state=""
return
}
config_path="$root/.kabu/config.yaml"
current_mtime=$(stat -c '%Y' "$config_path" 2>/dev/null || stat -f '%m' "$config_path" 2>/dev/null || echo "")
if [ "$root" != "$__kabu_trust_root" ] || \
[ "$current_mtime" != "$__kabu_trust_config_mtime" ]; then
__kabu_trust_root="$root"
__kabu_trust_config_mtime="$current_mtime"
__kabu_trust_state=""
fi
if ::KABU:: trust --check "$root"; then
__kabu_trust_state="trusted"
return
fi
if [ "$__kabu_trust_state" != "untrusted" ]; then
if [ -t 2 ]; then
printf '\033[31m%s\033[0m\n' "kabu: hooks in config file are not trusted. Run 'kabu trust' to review them." 1>&2
else
printf '%s\n' "kabu: hooks in config file are not trusted. Run 'kabu trust' to review them." 1>&2
fi
fi
__kabu_trust_state="untrusted"
}
__kabu_prompt_hook() {
local exit_code=$?
__kabu_trust_check
return $exit_code
}
if [[ ";${PROMPT_COMMAND:-};" != *";__kabu_prompt_hook;"* ]]; then
if [[ "$(declare -p PROMPT_COMMAND 2>/dev/null)" == "declare -a"* ]]; then
PROMPT_COMMAND=(__kabu_prompt_hook "${PROMPT_COMMAND[@]}")
elif [[ -n "${PROMPT_COMMAND:-}" ]]; then
PROMPT_COMMAND="__kabu_prompt_hook;${PROMPT_COMMAND}"
else
PROMPT_COMMAND="__kabu_prompt_hook"
fi
fi