opentofu.plugin.zsh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # set up the tofu completion (compatible for zsh)
  2. autoload -Uz bashcompinit && bashcompinit
  3. complete -C tofu tofu
  4. # tofu workspace prompt function
  5. function tofu_prompt_info() {
  6. # only show the workspace name if we're in an opentofu project
  7. # i.e. if a .terraform directory exists within the hierarchy
  8. local dir="$PWD"
  9. while [[ ! -d "${dir}/.terraform" ]]; do
  10. [[ "$dir" != / ]] || return 0 # stop at the root directory
  11. dir="${dir:h}" # get the parent directory
  12. done
  13. # workspace might be different than the .terraform/environment file
  14. # for example, if set with the TF_WORKSPACE environment variable
  15. local workspace="$(tofu workspace show)"
  16. # make sure to escape % signs in the workspace name to prevent command injection
  17. echo "${ZSH_THEME_TOFU_PROMPT_PREFIX-[}${workspace:gs/%/%%}${ZSH_THEME_TOFU_PROMPT_SUFFIX-]}"
  18. }
  19. # tofu version prompt function
  20. function tofu_version_prompt_info() {
  21. # get the output of `tofu --version` in a single line, and get the second word after splitting by a space
  22. local tofu_version=${${(s: :)$(tofu --version)}[2]}
  23. # make sure to escape % signs in the version string to prevent command injection
  24. echo "${ZSH_THEME_TOFU_VERSION_PROMPT_PREFIX-[}${tofu_version:gs/%/%%}${ZSH_THEME_TOFU_VERSION_PROMPT_SUFFIX-]}"
  25. }
  26. alias tt='tofu'
  27. alias tta='tofu apply'
  28. alias ttaa='tofu apply -auto-approve'
  29. alias ttc='tofu console'
  30. alias ttd='tofu destroy'
  31. alias ttf='tofu fmt'
  32. alias ttfr='tofu fmt -recursive'
  33. alias tti='tofu init'
  34. alias tto='tofu output'
  35. alias ttp='tofu plan'
  36. alias ttv='tofu validate'
  37. alias tts='tofu state'
  38. alias ttsh='tofu show'
  39. alias ttr='tofu refresh'
  40. alias ttt='tofu test'
  41. alias ttws='tofu workspace'