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