Skip to content

Instantly share code, notes, and snippets.

@lkwg82
Created May 3, 2020 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkwg82/b68f95095dcd7a4c81a0ddc9a9b831a5 to your computer and use it in GitHub Desktop.
Save lkwg82/b68f95095dcd7a4c81a0ddc9a9b831a5 to your computer and use it in GitHub Desktop.
bash prompt with terraform workspace and git info
#!/bin/bash
if [[ ! -f $(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh ]]; then
echo "installing first time bash-git-prompt"
brew install bash-git-prompt
fi
if [[ -f $(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh ]]; then
__GIT_PROMPT_DIR=$(brew --prefix)/opt/bash-git-prompt/share
# configs
# see https://github.com/magicmonty/bash-git-prompt#all-configs-for-bashrc
GIT_PROMPT_ONLY_IN_REPO=1
GIT_PROMPT_THEME=Solarized
GIT_PROMPT_SHOW_UPSTREAM=1
function prompt_callback() {
# combine with terraform
if command -v terraform >/dev/null && [ -d .terraform ]; then
echo " [\[\e[1m\]workspace\e[0m:$(terraform workspace show 2>/dev/null || echo "<error>")]"
fi
}
source "$(brew --prefix)/opt/bash-git-prompt/share/gitprompt.sh"
fi
# terraform workspace
if command -v terraform >/dev/null; then
function terraform_prompt() {
if [ -d .terraform ]; then
# consider git-prompt
if type -t git_prompt_config >/dev/null; then
repo=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ -e $repo ]]; then
# inside of git repository and with git-prompt we do nothing
return
fi
fi
# save old prompt
if [[ -z $OLD_PS1 ]]; then
OLD_PS1=${PS1}
fi
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\e[38;5;99m\]\[\e[1m\]\n\[\033[1;34m\]$(date +%H:%M)\[\033[0;0m\] [workspace:$(terraform workspace show 2>/dev/null || echo "<error>")]\e[0m \$ '
else
# recover old prompt
if [[ -n $OLD_PS1 ]]; then
PS1=${OLD_PS1}
fi
fi
}
PROMPT_COMMAND="terraform_prompt; $PROMPT_COMMAND"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment