-
Notifications
You must be signed in to change notification settings - Fork 1
/
bash_prompt
118 lines (100 loc) · 3.26 KB
/
bash_prompt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
exit_err() {
[ "$1" != "" ] && echo >&2 "${1}"
exit 1
}
awsp() {
if [ "$1" = "" ]; then
export AWS_PROFILE="$(aws configure list-profiles | fzy || exit_err '^C')"
elif [ "$1" = "on" ]; then
export PS1_AWSP="on"
elif [ "$1" = "off" ]; then
export PS1_AWSP=""
fi
}
aws_ps1() {
which aws &> /dev/null || return
if [ "$PS1_AWSP" = "on" -a "$AWS_PROFILE" != "" ]; then
echo "☁$AWS_PROFILE"
elif [ "$PS1_AWSP" = "on" -a "$AWS_PROFILE" = "" ]; then
echo "☁NOT SET"
else
echo ""
fi
}
k_current_context() {
echo "$(kubectl config current-context 2> /dev/null || echo "")"
}
k_current_ns() {
if [ "$KUBECTL_CONTEXT" = "" ]; then
echo ""
else
echo "$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${KUBECTL_CONTEXT}\")].context.namespace}")"
fi
}
alias k='kubectl --context="$KUBECTL_CONTEXT" --namespace="$KUBECTL_NAMESPACE"'
alias kubectl='kubectl --context="$KUBECTL_CONTEXT" --namespace="$KUBECTL_NAMESPACE"'
kctx() {
if [ "$1" = "" ]; then
export KUBECTL_CONTEXT="$(kubectl config get-contexts -o name | fzy || exit_err '^C')"
elif [ "$1" = "save" ]; then
if [ "$KUBECTL_CONTEXT" = "" ]; then
export KUBECTL_CONTEXT="$(kubectl config get-contexts -o name | fzy || exit_err '^C')"
fi
if [ "$KUBECTL_NAMESPACE" = "" ]; then
export KUBECTL_NAMESPACE="$(kubectl get namespaces -o=custom-columns=NAME:.metadata.name | fzy || exit_err '^C')"
fi
kubectl config set-context "$KUBECTL_CONTEXT" --namespace="$KUBECTL_NAMESPACE"
kubectl config use-context "$KUBECTL_CONTEXT"
elif [ "$1" = "on" ]; then
export PS1_KCTX="on"
elif [ "$1" = "off" ]; then
export PS1_KCTX=""
fi
}
kns() {
if [ "$1" != "" ]; then
export KUBECTL_NAMESPACE="$1"
else
export KUBECTL_NAMESPACE="$(kubectl get namespaces -o=custom-columns=NAME:.metadata.name | fzy || exit_err '^C')"
fi
}
git_branch_ps1() {
echo "$(git branch 2>/dev/null | grep -e '\* ' | sed 's/^..\(.*\)/{\1}/')"
}
kctx_ps1() {
which kubectl &> /dev/null || return
if [ "$KUBECTL_CONTEXT" = "" ]; then
export KUBECTL_CONTEXT="$(k_current_context)"
fi
if [ "$KUBECTL_NAMESPACE" = "" ]; then
export KUBECTL_NAMESPACE="$(k_current_ns)"
fi
if [ "$PS1_KCTX" = "on" -a "$KUBECTL_CONTEXT" != "" ]; then
echo "⎈$KUBECTL_CONTEXT/$KUBECTL_NAMESPACE"
elif [ "$PS1_KCTX" = "on" -a "$KUBECTL_CONTEXT" = "" ]; then
echo "⎈NOT SET"
else
echo ""
fi
}
bash_prompt() {
result=$?
status="$result $(date +'%F %T')"
reverse_color="\[\033[0;7m\]"
normal_color="\[\033[0m\]"
blue_color="\[\033[0;37;44m\]"
green_color="\[\033[0;42m\]"
yellow_color="\[\033[0;43m\]"
red_color="\[\033[0;41m\]"
if [ "$result" = "0" ]; then
status_color=$yellow_color;
else
status_color=$red_color;
fi
PS1="$status_color${status}$reverse_color \w $green_color\$(git_branch_ps1)\
$blue_color$(kctx_ps1)$red_color$(aws_ps1)$reverse_color$(printf '\n$')$normal_color "
}
export KUBECTL_CONTEXT="$(k_current_context)"
export KUBECTL_NAMESPACE="$(k_current_ns)"
PROMPT_COMMAND=bash_prompt