kube-ps1.plugin.zsh 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/bin/zsh
  2. # Kubernetes prompt helper for bash/zsh
  3. # Displays current context and namespace
  4. # Copyright 2017 Jon Mosco
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. # Debug
  18. [[ -n $DEBUG ]] && set -x
  19. setopt PROMPT_SUBST
  20. add-zsh-hook precmd _kube_ps1_load
  21. zmodload zsh/stat
  22. # Default values for the prompt
  23. # Override these values in ~/.zshrc or ~/.bashrc
  24. KUBE_PS1_DEFAULT="${KUBE_PS1_DEFAULT:=true}"
  25. KUBE_PS1_PREFIX="("
  26. KUBE_PS1_DEFAULT_LABEL="${KUBE_PS1_DEFAULT_LABEL:="⎈ "}"
  27. KUBE_PS1_DEFAULT_LABEL_IMG="${KUBE_PS1_DEFAULT_LABEL_IMG:=false}"
  28. KUBE_PS1_SEPERATOR="|"
  29. KUBE_PS1_PLATFORM="${KUBE_PS1_PLATFORM:="kubectl"}"
  30. KUBE_PS1_DIVIDER=":"
  31. KUBE_PS1_SUFFIX=")"
  32. KUBE_PS1_UNAME=$(uname)
  33. KUBE_PS1_LAST_TIME=0
  34. kube_ps1_label () {
  35. [[ "${KUBE_PS1_DEFAULT_LABEL_IMG}" == false ]] && return
  36. if [[ "${KUBE_PS1_DEFAULT_LABEL_IMG}" == true ]]; then
  37. local KUBE_LABEL="☸️ "
  38. fi
  39. KUBE_PS1_DEFAULT_LABEL="${KUBE_LABEL}"
  40. }
  41. _kube_ps1_split() {
  42. type setopt >/dev/null 2>&1 && setopt SH_WORD_SPLIT
  43. local IFS=$1
  44. echo $2
  45. }
  46. _kube_ps1_file_newer_than() {
  47. local mtime
  48. local file=$1
  49. local check_time=$2
  50. mtime=$(stat +mtime "${file}")
  51. [ "${mtime}" -gt "${check_time}" ]
  52. }
  53. _kube_ps1_load() {
  54. # kubectl will read the environment variable $KUBECONFIG
  55. # otherwise set it to ~/.kube/config
  56. KUBECONFIG="${KUBECONFIG:=$HOME/.kube/config}"
  57. for conf in $(_kube_ps1_split : "${KUBECONFIG}"); do
  58. # TODO: check existence of $conf
  59. if _kube_ps1_file_newer_than "${conf}" "${KUBE_PS1_LAST_TIME}"; then
  60. _kube_ps1_get_context_ns
  61. return
  62. fi
  63. done
  64. }
  65. _kube_ps1_get_context_ns() {
  66. # Set the command time
  67. KUBE_PS1_LAST_TIME=$(date +%s)
  68. if [[ "${KUBE_PS1_DEFAULT}" == true ]]; then
  69. local KUBE_BINARY="${KUBE_PS1_PLATFORM}"
  70. elif [[ "${KUBE_PS1_DEFAULT}" == false ]] && [[ "${KUBE_PS1_PLATFORM}" == "kubectl" ]];then
  71. local KUBE_BINARY="kubectl"
  72. elif [[ "${KUBE_PS1_PLATFORM}" == "oc" ]]; then
  73. local KUBE_BINARY="oc"
  74. fi
  75. KUBE_PS1_CONTEXT="$(${KUBE_BINARY} config current-context)"
  76. KUBE_PS1_NAMESPACE="$(${KUBE_BINARY} config view --minify --output 'jsonpath={..namespace}')"
  77. # Set namespace to default if it is not defined
  78. KUBE_PS1_NAMESPACE="${KUBE_PS1_NAMESPACE:-default}"
  79. }
  80. # source our symbol
  81. kube_ps1_label
  82. # Build our prompt
  83. kube_ps1 () {
  84. local reset_color="%f"
  85. local blue="%F{blue}"
  86. local red="%F{red}"
  87. local cyan="%F{cyan}"
  88. KUBE_PS1="${reset_color}$KUBE_PS1_PREFIX"
  89. KUBE_PS1+="${blue}$KUBE_PS1_DEFAULT_LABEL"
  90. KUBE_PS1+="${reset_color}$KUBE_PS1_SEPERATOR"
  91. KUBE_PS1+="${red}$KUBE_PS1_CONTEXT${reset_color}"
  92. KUBE_PS1+="$KUBE_PS1_DIVIDER"
  93. KUBE_PS1+="${cyan}$KUBE_PS1_NAMESPACE${reset_color}"
  94. KUBE_PS1+="$KUBE_PS1_SUFFIX"
  95. echo "${KUBE_PS1}"
  96. }