diff --git a/.functions/kubernetes.sh b/.functions/kubernetes.sh index c13eef5..eb66cd7 100644 --- a/.functions/kubernetes.sh +++ b/.functions/kubernetes.sh @@ -1,15 +1,5 @@ #!/bin/bash -function kc(){ - # print all kubernetes contexts - kubectl config get-contexts -} - -function kuc(){ - # use kubernetes context - kubectl config use-context "$1" -} - function kubernetes_configs(){ # set KUBECONFIG to default kubeconfig and all *.yaml files in .kube local KUBE_CONFIG_DEFAULT="$HOME/.kube/config" @@ -24,5 +14,20 @@ function kubernetes_configs(){ export KUBECONFIG="$file:$KUBECONFIG" done } - kubernetes_configs + +function kc(){ + # print all kubernetes contexts + kubectl config get-contexts +} + +function kuc(){ + # use kubernetes context + kubectl config use-context "$1" +} + +#bash completion for function kuc +if [[ $(command -v kubectl) ]];then + complete -W "$(kubectl config get-contexts -o name)" kuc +fi + diff --git a/.functions/ps1.sh b/.functions/ps1.sh index dc5669a..efc5c67 100644 --- a/.functions/ps1.sh +++ b/.functions/ps1.sh @@ -2,37 +2,36 @@ function set_bash_prompt () { local LAST_EXIT_STATUS=$? local EXIT_STATUS="" - local Color_Off='\033[0m' # Text Reset + local GIT_BRANCH="" + local PYTHON_ENV="" + local K8S_CURRENT_CONTEXT="" + local COLOR_OFF='\033[0m' local COLOR_DIVIDER="\033[0;32m" local COLOR_USERNAME="\[\e[34;1m\]" local COLOR_USERHOSTAT="\[\e[34;1m\]" local COLOR_HOSTNAME="\[\e[34;1m\]" local COLOR_GITBRANCH="\[\e[33;1m\]" local COLOR_VENV="\033[0;36m" - local PATH_COLOR="\033[0;37m" + local COLOR_PATH="\033[0;37m" + local COLOR_EXIT_STATUS="[\033[1;37m\033[41m" + local COLOR_KUBERNETES="\033[1;37m\033[44m" - if [[ $LAST_EXIT_STATUS != 0 ]];then - EXIT_STATUS="${COLOR_DIVIDER}[\033[1;37m\033[41m${LAST_EXIT_STATUS}${Color_Off}${COLOR_DIVIDER}]" + local CURRENT_CONTEXT="" + + [[ $LAST_EXIT_STATUS != 0 ]] && EXIT_STATUS="${COLOR_DIVIDER}${COLOR_EXIT_STATUS}${LAST_EXIT_STATUS}${COLOR_DIVIDER}]" + [[ $(type -t __git_ps1) == function ]] && GIT_BRANCH="${COLOR_GITBRANCH}$(__git_ps1)${COLOR_DIVIDER}" + [[ -n "$VIRTUAL_ENV" ]] && PYTHON_ENV="${COLOR_DIVIDER}(${COLOR_VENV}${VIRTUAL_ENV##*/}${COLOR_DIVIDER})" + if [[ $(type kubectl) ]]; then + CURRENT_CONTEXT="$(kubectl config current-context)" + if [[ "$CURRENT_CONTEXT" != "default" ]];then + K8S_CURRENT_CONTEXT="${COLOR_DIVIDER}[${COLOR_KUBERNETES}${CURRENT_CONTEXT}${COLOR_DIVIDER}]" + fi fi - PS1="\n${COLOR_DIVIDER}┌──(${COLOR_USERNAME}\u${COLOR_USERHOSTAT}@${COLOR_HOSTNAME}\h${COLOR_DIVIDER})─[${PATH_COLOR}\w${COLOR_DIVIDER}]${EXIT_STATUS}" - PS1="$PS1${COLOR_GITBRANCH}$(__git_ps1)${COLOR_DIVIDER}" -# # Add git branch portion of the prompt" -# if ! git_loc="$(type -p "$git_command_name")" || [ -z "$git_loc" ]; then -# # Git is installed -# if [ -d .git ] || git rev-parse --is-inside-work-tree > /dev/null 2>&1; then -# # Inside of a git repository -# GIT_BRANCH=$(git symbolic-ref --short HEAD) -# PS1="${PS1}:${COLOR_GITBRANCH}${GIT_BRANCH}${COLOR_DIVIDER}" -# fi -# fi - # Add Python VirtualEnv portion of the prompt, this adds ":venvname" - if ! test -z "$VIRTUAL_ENV" ; then - PS1="${PS1}(${COLOR_VENV}${VIRTUAL_ENV##*/}${COLOR_DIVIDER})" - fi - PS1="${PS1}\n${COLOR_DIVIDER}└─\$${Color_Off} " + PS1="\n${COLOR_DIVIDER}┌──(${COLOR_USERNAME}\u${COLOR_USERHOSTAT}@${COLOR_HOSTNAME}\h${COLOR_DIVIDER})─[${COLOR_PATH}\w${COLOR_DIVIDER}]${EXIT_STATUS}${PYTHON_ENV}${K8S_CURRENT_CONTEXT}${GIT_BRANCH}" + PS1="${PS1}\n${COLOR_DIVIDER}└─\$${COLOR_OFF} " } -# Tell Bash to run the above function for every prompt + export PROMPT_COMMAND=set_bash_prompt