From fd8f72b27614d48ec06421fb80aff2eb10674a69 Mon Sep 17 00:00:00 2001 From: chenzj Date: Sun, 14 Jul 2024 01:31:32 +0800 Subject: [PATCH] feat(opentofu): add plugin for OpenTofu (#12285) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Adds aliases - Sets up autocompletion - Adds promp functions to show workspace and `tofu` version Co-authored-by: Marc Cornellà --- plugins/opentofu/README.md | 58 ++++++++++++++++++++++++++++ plugins/opentofu/opentofu.plugin.zsh | 43 +++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 plugins/opentofu/README.md create mode 100644 plugins/opentofu/opentofu.plugin.zsh diff --git a/plugins/opentofu/README.md b/plugins/opentofu/README.md new file mode 100644 index 000000000..9c19501aa --- /dev/null +++ b/plugins/opentofu/README.md @@ -0,0 +1,58 @@ +# OpenTofu plugin + +Plugin for OpenTofu, a fork of Terraform that is open-source, community-driven, and managed by the Linux Foundation. It adds +completion for `tofu` command, as well as aliases and a prompt function. + +To use it, add `opentofu` to the plugins array of your `~/.zshrc` file: + +```shell +plugins=(... opentofu) +``` + +## Requirements + +- [OpenTofu](https://opentofu.org/) + +## Aliases + +| Alias | Command | +| ----- | --------------- | +| `tt` | `tofu` | +| `tta` | `tofu apply` | +| `ttc` | `tofu console` | +| `ttd` | `tofu destroy` | +| `ttf` | `tofu fmt` | +| `tti` | `tofu init` | +| `tto` | `tofu output` | +| `ttp` | `tofu plan` | +| `ttv` | `tofu validate` | +| `tts` | `tofu state` | +| `ttsh`| `tofu show` | +| `ttr` | `tofu refresh` | +| `ttt` | `tofu test` | +| `ttws`| `tofu workspace`| + + +## Prompt functions + +- `tofu_prompt_info`: shows the current workspace when in an OpenTofu project directory. + +- `tofu_version_prompt_info`: shows the current version of the `tofu` commmand. + +To use them, add them to a `PROMPT` variable in your theme or `.zshrc` file: + +```sh +PROMPT='$(tofu_prompt_info)' +RPROMPT='$(tofu_version_prompt_info)' +``` + +You can also specify the PREFIX and SUFFIX strings for both functions, with the following variables: + +```sh +# for tofu_prompt_info +ZSH_THEME_TOFU_PROMPT_PREFIX="%{$fg[white]%}" +ZSH_THEME_TOFU_PROMPT_SUFFIX="%{$reset_color%}" +# for tofu_version_prompt_info +ZSH_THEME_TOFU_VERSION_PROMPT_PREFIX="%{$fg[white]%}" +ZSH_THEME_TOFU_VERSION_PROMPT_SUFFIX="%{$reset_color%}" +``` diff --git a/plugins/opentofu/opentofu.plugin.zsh b/plugins/opentofu/opentofu.plugin.zsh new file mode 100644 index 000000000..c6844c84a --- /dev/null +++ b/plugins/opentofu/opentofu.plugin.zsh @@ -0,0 +1,43 @@ +# set up the tofu completion (compatible for zsh) +autoload -Uz bashcompinit && bashcompinit +complete -C tofu tofu + +# tofu workspace prompt function +function tofu_prompt_info() { + # only show the workspace name if we're in an opentofu project + # i.e. if a .terraform directory exists within the hierarchy + local dir="$PWD" + while [[ ! -d "${dir}/.terraform" ]]; do + [[ "$dir" != / ]] || return 0 # stop at the root directory + dir="${dir:h}" # get the parent directory + done + + # workspace might be different than the .terraform/environment file + # for example, if set with the TF_WORKSPACE environment variable + local workspace="$(tofu workspace show)" + # make sure to escape % signs in the workspace name to prevent command injection + echo "${ZSH_THEME_TOFU_PROMPT_PREFIX-[}${workspace:gs/%/%%}${ZSH_THEME_TOFU_PROMPT_SUFFIX-]}" +} + +# tofu version prompt function +function tofu_version_prompt_info() { + # get the output of `tofu --version` in a single line, and get the second word after splitting by a space + local tofu_version=${${(s: :)$(tofu --version)}[2]} + # make sure to escape % signs in the version string to prevent command injection + echo "${ZSH_THEME_TOFU_VERSION_PROMPT_PREFIX-[}${tofu_version:gs/%/%%}${ZSH_THEME_TOFU_VERSION_PROMPT_SUFFIX-]}" +} + +alias tt='tofu' +alias tta='tofu apply' +alias ttc='tofu console' +alias ttd='tofu destroy' +alias ttf='tofu fmt' +alias tti='tofu init' +alias tto='tofu output' +alias ttp='tofu plan' +alias ttv='tofu validate' +alias tts='tofu state' +alias ttsh='tofu show' +alias ttr='tofu refresh' +alias ttt='tofu test' +alias ttws='tofu workspace'