fd8f72b27614d48ec06421fb80aff2eb10674a69

Author
chenzj <tibazq@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(opentofu): add plugin for OpenTofu (#12285)

- Adds aliases
- Sets up autocompletion
- Adds promp functions to show workspace and `tofu` version

Co-authored-by: Marc Cornellà <marc@mcornella.com>

Diff

  1diff --git a/plugins/opentofu/README.md b/plugins/opentofu/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..9c19501aa24d4207633b62273cde9e8095630b2b
  4--- /dev/null
  5+++ b/plugins/opentofu/README.md
  6@@ -0,0 +1,58 @@
  7+# OpenTofu plugin
  8+
  9+Plugin for OpenTofu, a fork of Terraform that is open-source, community-driven, and managed by the Linux Foundation. It adds
 10+completion for `tofu` command, as well as aliases and a prompt function.
 11+
 12+To use it, add `opentofu` to the plugins array of your `~/.zshrc` file:
 13+
 14+```shell
 15+plugins=(... opentofu)
 16+```
 17+
 18+## Requirements
 19+
 20+- [OpenTofu](https://opentofu.org/)
 21+
 22+## Aliases
 23+
 24+| Alias | Command         |
 25+| ----- | --------------- |
 26+| `tt`  | `tofu`          |
 27+| `tta` | `tofu apply`    |
 28+| `ttc` | `tofu console`  |
 29+| `ttd` | `tofu destroy`  |
 30+| `ttf` | `tofu fmt`      |
 31+| `tti` | `tofu init`     |
 32+| `tto` | `tofu output`   |
 33+| `ttp` | `tofu plan`     |
 34+| `ttv` | `tofu validate` |
 35+| `tts` | `tofu state`    |
 36+| `ttsh`| `tofu show`     |
 37+| `ttr` | `tofu refresh`  |
 38+| `ttt` | `tofu test`     |
 39+| `ttws`| `tofu workspace`|
 40+
 41+
 42+## Prompt functions
 43+
 44+- `tofu_prompt_info`: shows the current workspace when in an OpenTofu project directory.
 45+
 46+- `tofu_version_prompt_info`: shows the current version of the `tofu` commmand.
 47+
 48+To use them, add them to a `PROMPT` variable in your theme or `.zshrc` file:
 49+
 50+```sh
 51+PROMPT='$(tofu_prompt_info)'
 52+RPROMPT='$(tofu_version_prompt_info)'
 53+```
 54+
 55+You can also specify the PREFIX and SUFFIX strings for both functions, with the following variables:
 56+
 57+```sh
 58+# for tofu_prompt_info
 59+ZSH_THEME_TOFU_PROMPT_PREFIX="%{$fg[white]%}"
 60+ZSH_THEME_TOFU_PROMPT_SUFFIX="%{$reset_color%}"
 61+# for tofu_version_prompt_info
 62+ZSH_THEME_TOFU_VERSION_PROMPT_PREFIX="%{$fg[white]%}"
 63+ZSH_THEME_TOFU_VERSION_PROMPT_SUFFIX="%{$reset_color%}"
 64+```
 65diff --git a/plugins/opentofu/opentofu.plugin.zsh b/plugins/opentofu/opentofu.plugin.zsh
 66new file mode 100644
 67index 0000000000000000000000000000000000000000..c6844c84a74c032e33a38286a721db0705680d60
 68--- /dev/null
 69+++ b/plugins/opentofu/opentofu.plugin.zsh
 70@@ -0,0 +1,43 @@
 71+# set up the tofu completion (compatible for zsh)
 72+autoload -Uz bashcompinit && bashcompinit
 73+complete -C tofu tofu
 74+
 75+# tofu workspace prompt function
 76+function tofu_prompt_info() {
 77+  # only show the workspace name if we're in an opentofu project
 78+  # i.e. if a .terraform directory exists within the hierarchy
 79+  local dir="$PWD"
 80+  while [[ ! -d "${dir}/.terraform" ]]; do
 81+    [[ "$dir" != / ]] || return 0 # stop at the root directory
 82+    dir="${dir:h}"                # get the parent directory
 83+  done
 84+
 85+  # workspace might be different than the .terraform/environment file
 86+  # for example, if set with the TF_WORKSPACE environment variable
 87+  local workspace="$(tofu workspace show)"
 88+  # make sure to escape % signs in the workspace name to prevent command injection
 89+  echo "${ZSH_THEME_TOFU_PROMPT_PREFIX-[}${workspace:gs/%/%%}${ZSH_THEME_TOFU_PROMPT_SUFFIX-]}"
 90+}
 91+
 92+# tofu version prompt function
 93+function tofu_version_prompt_info() {
 94+  # get the output of `tofu --version` in a single line, and get the second word after splitting by a space
 95+  local tofu_version=${${(s: :)$(tofu --version)}[2]}
 96+  # make sure to escape % signs in the version string to prevent command injection
 97+  echo "${ZSH_THEME_TOFU_VERSION_PROMPT_PREFIX-[}${tofu_version:gs/%/%%}${ZSH_THEME_TOFU_VERSION_PROMPT_SUFFIX-]}"
 98+}
 99+
100+alias tt='tofu'
101+alias tta='tofu apply'
102+alias ttc='tofu console'
103+alias ttd='tofu destroy'
104+alias ttf='tofu fmt'
105+alias tti='tofu init'
106+alias tto='tofu output'
107+alias ttp='tofu plan'
108+alias ttv='tofu validate'
109+alias tts='tofu state'
110+alias ttsh='tofu show'
111+alias ttr='tofu refresh'
112+alias ttt='tofu test'
113+alias ttws='tofu workspace'