[
  {
    "path": "LICENSE.txt",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2017  Kazuki Suda <kazuki.suda@gmail.com>\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\nLIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\nOF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\nWITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "# zsh-kubectl-prompt\n\nThis script displays information about the kubectl current context and namespace in zsh prompt.\n\n![Screenshot](./images/screenshot001.png)\n\n## Usage\n\nClone this repository and source the `kubectl.zsh` from your `~/.zshrc` config file, and configure your prompt.\n\n```sh\nautoload -U colors; colors\nsource /path/to/zsh-kubectl-prompt/kubectl.zsh\nRPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'\n```\n\nOr create different style depending on user, context, namespace.\nThe plugin creates 4 variables:\n* ZSH_KUBECTL_CONTEXT\n* ZSH_KUBECTL_NAMESPACE\n* ZSH_KUBECTL_PROMPT\n* ZSH_KUBECTL_USER\n\nFor example, make the prompt red when the username matches admin.\n```sh\nautoload -U colors; colors\nsource /path/to/zsh-kubectl-prompt/kubectl.zsh\nfunction right_prompt() {\n  local color=\"blue\"\n\n  if [[ \"$ZSH_KUBECTL_USER\" =~ \"admin\" ]]; then\n    color=red\n  fi\n\n  echo \"%{$fg[$color]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}\"\n}\nRPROMPT='$(right_prompt)'\n```\n\nAlso you can install with homebrew.\n\n```sh\nbrew tap superbrothers/zsh-kubectl-prompt\nbrew install zsh-kubectl-prompt\n```\n\n## Customization\n\nChange the separator between context and namespace:\n\n```sh\nzstyle ':zsh-kubectl-prompt:' separator '|'\n```\n\nAdd custom character before the prompt:\n\n```sh\nzstyle ':zsh-kubectl-prompt:' preprompt '<'\n```\n\nAdd custom character after the prompt:\n\n```sh\nzstyle ':zsh-kubectl-prompt:' postprompt '>'\n```\n\nDoes not display the current namespace:\n\n```sh\nzstyle ':zsh-kubectl-prompt:' namespace false\n```\n\nUse another binary instead of `kubectl` to get the information (e.g. `oc`):\n\n```sh\nzstyle ':zsh-kubectl-prompt:' binary 'oc'\n```\n\n## With a plugin manager\n\nIf you use [zgen](https://github.com/tarjoilija/zgen), load this repository as follows:\n\n```sh\nsource \"${HOME}/.zgen/zgen.zsh\"\n\n# if the init script doesn't exist\nif ! zgen saved; then\n    # specify plugins here\n    zgen load superbrothers/zsh-kubectl-prompt\n\n    # generate the init script from plugins above\n    zgen save\nfi\n\nautoload -U colors; colors\nRPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'\n```\n\nIf you use [antigen](https://github.com/zsh-users/antigen), load this repository as follows:\n\n```sh\nsource /path-to-antigen/antigen.zsh\n\n# load this plugin\nantigen bundle superbrothers/zsh-kubectl-prompt\n\n# tell antigen that you're done.\nantigen apply\n\nautoload -U colors; colors\nRPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'\n```\n\nIf you use [oh-my-zsh](https://ohmyz.sh/), load this repository as follows:\n\n1. Clone the repo into oh-my-zsh custom plugins folder\n\n```sh\ngit clone https://github.com/superbrothers/zsh-kubectl-prompt.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-kubectl-prompt\n```\n\n2. Activate the plugin your `.zshrc` by appending it to the plugin section\n\n```sh\nplugins=( [plugins...] zsh-kubectl-prompt)\n```\n\n3. Configure your prompt (or check how to customize the theme plugin you are using)\n```sh\nRPROMPT='%{$fg[blue]%}($ZSH_KUBECTL_PROMPT)%{$reset_color%}'\n```\n\n> **Note:** Remember to source the `.zshrc` or restart your shell after step 2\n\n## License\n\nThis script is released under the MIT License.\n"
  },
  {
    "path": "kubectl.zsh",
    "content": "setopt prompt_subst\nautoload -U add-zsh-hook\n\nfunction() {\n    local namespace separator binary\n\n    # Specify the separator between context and namespace\n    zstyle -s ':zsh-kubectl-prompt:' separator separator\n    if [[ -z \"$separator\" ]]; then\n        zstyle ':zsh-kubectl-prompt:' separator '/'\n    fi\n\n    # Display the current namespace if `namespace` is true\n    zstyle -s ':zsh-kubectl-prompt:' namespace namespace\n    if [[ -z \"$namespace\" ]]; then\n        zstyle ':zsh-kubectl-prompt:' namespace true\n    fi\n\n    # Specify the binary to get the information from kubeconfig (e.g. `oc`)\n    zstyle -s ':zsh-kubectl-binary:' binary binary\n    if [[ -z \"$binary\" ]]; then\n        zstyle ':zsh-kubectl-prompt:' binary \"kubectl\"\n    fi\n}\n\nadd-zsh-hook precmd _zsh_kubectl_prompt_precmd\nfunction _zsh_kubectl_prompt_precmd() {\n    local kubeconfig config updated_at now context namespace ns separator modified_time_fmt binary\n\n    zstyle -s ':zsh-kubectl-prompt:' binary binary\n    if ! command -v \"$binary\" >/dev/null; then\n      ZSH_KUBECTL_PROMPT=\"${binary} command not found\"\n      return 1\n    fi\n\n    kubeconfig=\"$HOME/.kube/config\"\n    if [[ -n \"$KUBECONFIG\" ]]; then\n        kubeconfig=\"$KUBECONFIG\"\n    fi\n\n    zstyle -s ':zsh-kubectl-prompt:' modified_time_fmt modified_time_fmt\n    if [[ -z \"$modified_time_fmt\" ]]; then\n      # Check the stat command because it has a different syntax between GNU coreutils and FreeBSD.\n      if stat --help >/dev/null 2>&1; then\n          modified_time_fmt='-c%y' # GNU coreutils\n      else\n          modified_time_fmt='-f%m' # FreeBSD\n      fi\n      zstyle ':zsh-kubectl-prompt:' modified_time_fmt $modified_time_fmt\n    fi\n\n    # KUBECONFIG environment variable can hold a list of kubeconfig files that is colon-delimited.\n    # Therefore, if KUBECONFIG has been held multiple files, each files need to be checked.\n    while read -d \":\" config; do\n        if ! now=\"${now}$(stat -L $modified_time_fmt \"$config\" 2>/dev/null)\"; then\n            ZSH_KUBECTL_PROMPT=\"$config doesn't exist\"\n            return 1\n        fi\n    done <<< \"${kubeconfig}:\"\n\n    zstyle -s ':zsh-kubectl-prompt:' updated_at updated_at\n    if [[ \"$updated_at\" == \"$now\" ]]; then\n        return 0\n    fi\n    zstyle ':zsh-kubectl-prompt:' updated_at \"$now\"\n\n    # Set environment variable if context is not set\n    if ! context=\"$(\"$binary\" config current-context 2>/dev/null)\"; then\n        ZSH_KUBECTL_PROMPT=\"current-context is not set\"\n        return 1\n    fi\n\n    ZSH_KUBECTL_USER=\"$(\"$binary\" config view -o \"jsonpath={.contexts[?(@.name==\\\"$context\\\")].context.user}\")\"\n    ZSH_KUBECTL_CONTEXT=\"${context}\"\n    ns=\"$(\"$binary\" config view -o \"jsonpath={.contexts[?(@.name==\\\"$context\\\")].context.namespace}\")\"\n    [[ -z \"$ns\" ]] && ns=\"default\"\n    ZSH_KUBECTL_NAMESPACE=\"${ns}\"\n\n    # Specify the entry before prompt (default empty)\n    zstyle -s ':zsh-kubectl-prompt:' preprompt preprompt\n    # Specify the entry after prompt (default empty)\n    zstyle -s ':zsh-kubectl-prompt:' postprompt postprompt\n\n    # Set environment variable without namespace\n    zstyle -s ':zsh-kubectl-prompt:' namespace namespace\n    if [[ \"$namespace\" != true ]]; then\n        ZSH_KUBECTL_PROMPT=\"${preprompt}${context}${postprompt}\"\n        return 0\n    fi\n\n    zstyle -s ':zsh-kubectl-prompt:' separator separator\n    ZSH_KUBECTL_PROMPT=\"${preprompt}${context}${separator}${ns}${postprompt}\"\n\n    return 0\n}\n"
  },
  {
    "path": "zsh-kubectl-prompt.plugin.zsh",
    "content": "source ${0:A:h}/kubectl.zsh\n"
  }
]