From 53ad1e3bacb424991275c83b2a67b9e4eb2f9981 Mon Sep 17 00:00:00 2001 From: Andrew Babichev Date: Thu, 3 Oct 2019 22:53:23 +0300 Subject: [PATCH] feat(terragrunt): add tg alias & autocompletion --- plugins/terragrunt/README.md | 19 ++ plugins/terragrunt/_terragrunt | 411 +++++++++++++++++++++++ plugins/terragrunt/terragrunt.plugin.zsh | 11 + 3 files changed, 441 insertions(+) create mode 100644 plugins/terragrunt/README.md create mode 100644 plugins/terragrunt/_terragrunt create mode 100644 plugins/terragrunt/terragrunt.plugin.zsh diff --git a/plugins/terragrunt/README.md b/plugins/terragrunt/README.md new file mode 100644 index 000000000..a84dddc4a --- /dev/null +++ b/plugins/terragrunt/README.md @@ -0,0 +1,19 @@ +## Terragrunt oh-my-zsh plugin + +Plugin for Terragrunt, a thin wrapper over Terraform tool from Hashicorp for managing infrastructure safely and efficiently. + +Current as of Terragrunt v0.19.16 + +### Requirements + + * [Terragrunt](https://github.com/gruntwork-io/terragrunt) + +### Usage + +To use it, add `terragrunt` to the plugins array of your `~/.zshrc` file: + +```shell +plugins=(... terragrunt) +``` + + * Type `terragrunt` into your prompt and hit `TAB` to see available completion options diff --git a/plugins/terragrunt/_terragrunt b/plugins/terragrunt/_terragrunt new file mode 100644 index 000000000..7e2fe2625 --- /dev/null +++ b/plugins/terragrunt/_terragrunt @@ -0,0 +1,411 @@ +#compdef terragrunt + +local -a _terragrunt_cmds +_terragrunt_cmds=( + 'apply:Builds or changes infrastructure' + 'console:Interactive console for Terraform interpolations' + 'destroy:Destroy Terraform-managed infrastructure' + 'fmt:Rewrites config files to canonical format' + 'force-unlock:Manually unlock the terraform state' + 'get:Download and install modules for the configuration' + 'graph:Create a visual graph of Terraform resources' + 'import:Import existing infrastructure into Terraform' + 'init:Initialize a Terraform working directory' + 'login:Obtain and save credentials for a remote host' + 'logout:Remove locally-stored credentials for a remote host' + 'output:Read an output from a state file' + 'plan:Generate and show an execution plan' + 'providers:Prints a tree of the providers used in the configuration' + 'refresh:Update local state file against real resources' + 'show:Inspect Terraform state or plan' + 'state:Advanced state management' + 'taint:Manually mark a resource for recreation' + 'untaint:Manually unmark a resource as tainted' + 'validate:Validates the Terraform files' + 'version:Prints the Terraform version' + 'workspace:Workspace management' + 'aws-provider-patch:(Terragrunt) Overwrite settings on nested AWS providers to work around several Terraform bugs' + 'graph-dependencies:(Terragrunt) Prints the terragrunt dependency graph in DOT format to stdout' + 'hclfmt:(Terragrunt) Recursively find hcl files and rewrite them into a canonical format' + 'render-json:(Terragrunt) Render out the final interpreted terragrunt.hcl file as json' + 'run-all:(Terragrunt) Run the provided Terraform command against a "stack".' + 'terragrunt-info:(Terragrunt) Emits limited terragrunt state on stdout in a JSON format and exits' + 'validate-inputs:(Terragrunt) Emits information about the input variables that are configured with the given terragrunt configuration' + 'plan-all:(Terragrunt) DEPRECATED: use run-all. Display the plans of a "stack" by running "terragrunt plan" in each subfolder' + 'apply-all:(Terragrunt) DEPRECATED: use run-all. Apply a "stack" by running "terragrunt apply" in each subfolder' + 'output-all:(Terragrunt) DEPRECATED: use run-all. Display the outputs of a "stack" by running "terragrunt output" in each subfolder' + 'destroy-all:(Terragrunt) DEPRECATED: use run-all. Destroy a "stack" by running "terragrunt destroy" in each subfolder' + 'validate-all:(Terragrunt) DEPRECATED: use run-all. Validate "stack" by running "terragrunt validate" in each subfolder' +) + +__apply() { + _arguments \ + '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]' \ + '-auto-approve[Skip interactive approval of plan before applying.]' \ + '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]:backupfile:_files -g "*.backup"' \ + '-compact-warnings[If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages.]' \ + '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \ + '-lock-timeout=[(0s) Duration to retry a state lock.]' \ + '-input=[(true) Ask for input for variables if not directly set.]' \ + '-no-color[If specified, output wil be colorless.]' \ + '-parallelism=[(10) Limit the number of parallel resource operations.]' \ + '-refresh=[(true) Update state prior to checking for differences. This has no effect if a plan file is given to apply.]' \ + '-state=[(terraform.tfstate) Path to read and save state (unless state-out is specified).]:statefile:_files -g "*.tfstate"' \ + '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]:statefile:_files -g "*.tfstate"' \ + '*-target=[(resource) Resource to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \ + '*-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \ + '*-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]:file:_files -g "*.tfvars{,.json}"' +} + +__console() { + _arguments \ + '-state=[(terraform.tfstate) Path to read state.]' \ + '*-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \ + '*-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]:file:_files -g "*.tfvars{,.json}"' +} + +__destroy() { + _arguments \ + '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]' \ + '-auto-approve[Skip interactive approval before destroying.]' \ + '-force[Deprecated: same as auto-approve.]' \ + '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \ + '-lock-timeout=[(0s) Duration to retry a state lock.]' \ + '-no-color[If specified, output will contain no color.]' \ + '-parallelism=[(10) Limit the number of concurrent operations.]' \ + '-refresh=[(true) Update state prior to checking for differences. This has no effect if a plan file is given to apply.]' \ + '-state=[(terraform.tfstate) Path to read and save state (unless state-out is specified).]:statefile:_files -g "*.tfstate"' \ + '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]:statefile:_files -g "*.tfstate"' \ + '*-target=[(resource) Resource to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \ + '*-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \ + '*-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]:file:_files -g "*.tfvars{,.json}"' +} + +__fmt() { + _arguments \ + '-list=[(true) List files whose formatting differs (always false if using STDIN)]' \ + '-write=[(true) Write result to source file instead of STDOUT (always false if using STDIN or -check)]' \ + '-diff=[(false) Display diffs of formatting changes]' \ + '-check=[(false) Check if the input is formatted. Exit status will be 0 if all input is properly formatted and non-zero otherwise.]' \ + '-recursive=[(false) Also process files in subdirectories. By default, only the given directory (or current directory) is processed.]' +} + +__force_unlock() { + _arguments \ + "-force[Don't ask for input for unlock confirmation.]" +} + +__get() { + _arguments \ + '-update=[(false) If true, modules already downloaded will be checked for updates and updated if necessary.]' \ + '-no-color[Disable text coloring in the output.]' +} + +__graph() { + _arguments \ + '-draw-cycles[Highlight any cycles in the graph with colored edges. This helps when diagnosing cycle errors.]' \ + '-type=[(plan) Type of graph to output. Can be: plan, plan-destroy, apply, validate, input, refresh.]' +} + +__import() { + _arguments \ + '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]' \ + '-config=[(path) Path to a directory of Terraform configuration files to use to configure the provider. Defaults to pwd. If no config files are present, they must be provided via the input prompts or env vars.]' \ + '-allow-missing-config[Allow import when no resource configuration block exists.]' \ + '-input=[(true) Ask for input for variables if not directly set.]' \ + '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \ + '-lock-timeout=[(0s) Duration to retry a state lock.]' \ + '-no-color[If specified, output will contain no color.]' \ + '-state=[(PATH) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:statefile:_files -g "*.tfstate"' \ + '-state-out=[(PATH) Path to the destination state file to write to. If this is not specified, the source state file will be used. This can be a new or existing path.]:statefile:_files -g "*.tfstate"' \ + '*-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times. This is only useful with the "-config" flag.]' \ + '*-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]:file:_files -g "*.tfvars{,.json}"' +} + +__init() { + _arguments \ + '-backend=[(true) Configure the backend for this configuration.]' \ + '-backend-config=[This can be either a path to an HCL file with key/value assignments (same format as terraform.tfvars) or a 'key=value' format. This is merged with what is in the configuration file. This can be specified multiple times. The backend type must be in the configuration itself.]' \ + '-force-copy[Suppress prompts about copying state data. This is equivalent to providing a "yes" to all confirmation prompts.]' \ + '-from-module=[(SOURCE) Copy the contents of the given module into the target directory before initialization.]' \ + '-get=[(true) Download any modules for this configuration.]' \ + '-get-plugins=[(true) Download any missing plugins for this configuration.]' \ + '-input=[(true) Ask for input if necessary. If false, will error if input was required.]' \ + '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \ + '-lock-timeout=[(0s) Duration to retry a state lock.]' \ + '-no-color[If specified, output will contain no color.]' \ + '-plugin-dir[Directory containing plugin binaries. This overrides all default search paths for plugins, and prevents the automatic installation of plugins. This flag can be used multiple times.]:plugin_dir:_files -/' \ + '-reconfigure[Reconfigure the backend, ignoring any saved configuration.]' \ + '-upgrade=[(false) If installing modules (-get) or plugins (-get-plugins), ignore previously-downloaded objects and install the latest version allowed within configured constraints.]' \ + '-verify-plugins=[(true) Verify the authenticity and integrity of automatically downloaded plugins.]' +} + +__login() { + _arguments \ + +} + +__logout() { + _arguments \ + +} + +__output() { + _arguments \ + '-state=[(path) Path to the state file to read. Defaults to "terraform.tfstate".]:statefile:_files -g "*.tfstate"' \ + '-no-color[If specified, output will contain no color.]' \ + '-json[If specified, machine readable output will be printed in JSON format]' +} + +__plan() { + _arguments \ + '-compact-warnings[If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages.]' \ + '-destroy[If set, a plan will be generated to destroy all resources managed by the given configuration and state.]' \ + '-detailed-exitcode[() Return detailed exit codes when the command exits. This will change the meaning of exit codes to: 0 - Succeeded, diff is empty (no changes); 1 - Errored, 2 - Succeeded; there is a diff]' \ + '-input=[(true) Ask for input for variables if not directly set.]' \ + '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \ + '-lock-timeout=[(0s) Duration to retry a state lock.]' \ + '-no-color[() If specified, output will contain no color.]' \ + '-out=[(path) Write a plan file to the given path. This can be used as input to the "apply" command.]' \ + '-parallelism=[(10) Limit the number of concurrent operations.]' \ + '-refresh=[(true) Update state prior to checking for differences.]' \ + '-state=[(statefile) Path to a Terraform state file to use to look up Terraform-managed resources. By default it will use the state "terraform.tfstate" if it exists.]:statefile:_files -g "*.tfstate"' \ + '*-target=[(resource) Resource to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \ + '*-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \ + '*-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]:file:_files -g "*.tfvars{,.json}"' +} + +__providers() { + local -a __providers_cmds + __providers_cmds=( + 'mirror:Mirrors the provider plugins needed for the current configuration' + 'schema:Prints the schemas of the providers used in the configuration' + ) + _describe -t providers "providers commands" __providers_cmds + +} + +__providers_mirror() { + _arguments \ + '-platform=[(os_arch) Choose which target platform to build a mirror for.]' \ + "*:target_dir:_files -/" +} + +__providers_schema() { + _arguments \ + '-json[]' \ + '::' +} + +__refresh() { + _arguments \ + '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]::backupfile:_files -g "*.backup"' \ + '-compact-warnings[If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages.]' \ + '-input=[(true) Ask for input for variables if not directly set.]' \ + '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \ + '-lock-timeout=[(0s) Duration to retry a state lock.]' \ + '-no-color[If specified, output will not contain any color.]' \ + '-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]:statefile:_files -g "*.tfstate"' \ + '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]:statefile:_files -g "*.tfstate"' \ + '*-target=[(resource) A Resource Address to target. Operation will be limited to this resource and its dependencies. This flag can be used multiple times.]:target:__statelist' \ + '*-var[("foo=bar") Set a variable in the Terraform configuration. This flag can be set multiple times.]' \ + '*-var-file=[(foo) Set variables in the Terraform configuration from a file. If "terraform.tfvars" or any ".auto.tfvars" files are present, they will be automatically loaded.]:file:_files -g "*.tfvars{,.json}"' +} + +__show() { + _arguments \ + '-json[If specified, output the Terraform plan or state in a machine-readable form.]' \ + '-no-color[If specified, output will not contain any color.]' +} + +__state() { + local -a __state_cmds + __state_cmds=( + 'list:List resources in the state' + 'mv:Move an item in the state' + 'pull:Pull current state and output to stdout' + 'push:Update remote state from a local state file' + 'replace-provider:Replace provider for resources in the Terraform state' + 'rm:Remove instances from the state' + 'show:Show a resource in the state' + ) + _describe -t state "state commands" __state_cmds +} + +__state_list() { + _arguments \ + '-state=[(statefile) Path to a Terraform state file to use to look up Terraform-managed resources. By default, Terraform will consult the state of the currently-selected workspace.]' \ + '-id=[(id) Filters the results to include only instances whose resource types have an attribute named id whose value equals the given id string.]' \ + "*:address:__statelist" +} + +__state_mv() { + _arguments \ + "-dry-run[If set, prints out what would've been moved but doesn't actually move anything.]" \ + '-backup=[(PATH) Path where Terraform should write the backup for the original state. This can"t be disabled. If not set, Terraform will write it to the same path as the statefile with a ".backup" extension.]:backupfile:_files -g "*.backup"' \ + '-backup-out=[(PATH) Path where Terraform should write the backup for the destination state. This can"t be disabled. If not set, Terraform will write it to the same path as the destination state file with a backup extension. This only needs to be specified if -state-out is set to a different path than -state.]:backupfile:_files -g "*.backup"' \ + "-lock=[(true) Lock the state files when locking is supported.]:lock:(true false)" \ + "-lock-timeout=[(0s) Duration to retry a state lock.]" \ + '-state=[(path) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:statefile:_files -g "*.tfstate"' \ + '-state-out=[(path) Path to the destination state file to write to. If this isn"t specified, the source state file will be used. This can be a new or existing path.]:statefile:_files -g "*.tfstate"' \ + "::" \ + ":source:__statelist" \ + ":destination: " +} + +__state_push() { + _arguments \ + "-force[Write the state even if lineages don't match or the remote serial is higher.]" \ + '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \ + "-lock-timeout=[(0s) Duration to retry a state lock.]" \ + "::" \ + ":destination:_files" +} + +__state_replace_provider() { + _arguments \ + '-auto-approve[Skip interactive approval.]' \ + '-backup=[(PATH) Path where Terraform should write the backup for the state file. This can"t be disabled. If not set, Terraform will write it to the same path as the state file with a ".backup" extension.]:backupfile:_files -g "*.backup"' \ + "-lock=[(true) Lock the state files when locking is supported.]:lock:(true false)" \ + "-lock-timeout=[(0s) Duration to retry a state lock.]" \ + '-state=[(PATH) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:statefile:_files -g "*.tfstate"' \ + ":from_provider_fqn:" \ + ":to_provider_fqn:" +} + +__state_rm() { + _arguments \ + "-dry-run[If set, prints out what would've been removed but doesn't actually remove anything.]" \ + '-backup=[(PATH) Path where Terraform should write the backup for the original state.]::backupfile:_files -g "*.backup"' \ + "-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)" \ + "-lock-timeout=[(0s) Duration to retry a state lock.]" \ + '-state=[(PATH) Path to the state file to update. Defaults to the current workspace state.]:statefile:_files -g "*.tfstate"' \ + "*:address:__statelist" +} + + +__state_show() { + _arguments \ + '-state=[(statefile) Path to a Terraform state file to use to look up Terraform-managed resources. By default it will use the state "terraform.tfstate" if it exists.]:statefile:_files -g "*.tfstate"' \ + "*:address:__statelist" +} + +__statelist() { + # Get the Terraform working directory and move there + tf_working_dir="$(terragrunt terragrunt-info 2> /dev/null | grep WorkingDir | sed -r 's/.*: \"(.*)\"/\1/')" + cd $tf_working_dir + # Output the list of resources and use them for completion + compadd $(terraform state list $opt_args[-state] 2> /dev/null) +} + +__taint() { + _arguments \ + '-allow-missing[If specified, the command will succeed (exit code 0) even if the resource is missing.]' \ + '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]:backupfile:_files -g "*.backup"' \ + '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \ + '-lock-timeout=[(0s) Duration to retry a state lock.]' \ + '-module=[(path) The module path where the resource lives. By default this will be root. Child modules can be specified by names. Ex. "consul" or "consul.vpc" (nested modules).]' \ + '-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]:statefile:_files -g "*.tfstate"' \ + '-state-out=[(path) Path to write updated state file. By default, the "-state" path will be used.]:statefile:_files -g "*.tfstate"' \ + "*:address:__statelist" +} + +__untaint() { + _arguments \ + '-allow-missing[If specified, the command will succeed (exit code 0) even if the resource is missing.]' \ + '-backup=[(path) Path to backup the existing state file before modifying. Defaults to the "-state-out" path with ".backup" extension. Set to "-" to disable backup.]:backupfile:_files -g "*.backup"' \ + '-lock=[(true) Lock the state file when locking is supported.]:lock:(true false)' \ + '-lock-timeout=[(0s) Duration to retry a state lock.]' \ + '-module=[(path) The module path where the resource lives. By default this will be root. Child modules can be specified by names. Ex. "consul" or "consul.vpc" (nested modules).]' \ + '-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]:statefile:_files -g "*.tfstate"' \ + '-state-out=[(path) Path to write updated state file. By default, the "-state" path will be used.]:statefile:_files -g "*.tfstate"' +} + +__validate() { + _arguments \ + '-no-color[If specified, output will not contain any color.]' \ + '-json[Produce output in a machine-readable JSON format, suitable for use in text editor integrations and other automated systems.]' \ + ':dir:_files -/' +} + +__version() { + _arguments \ + '-json[Output the version information as a JSON object.]' +} + +__workspace() { + local -a __workspace_cmds + __workspace_cmds=( + 'delete:Delete a workspace' + 'list:List Workspaces' + 'new:Create a new workspace' + 'select:Select a workspace' + 'show:Show the name of the current workspace' + ) + _describe -t workspace "workspace commands" __workspace_cmds +} + +_arguments '*:: :->command' + +if (( CURRENT == 1 )); then + _describe -t commands "terragrunt command" _terragrunt_cmds + return +fi + +local -a _command_args +case "$words[1]" in + apply) + __apply ;; + console) + __console;; + destroy) + __destroy ;; + fmt) + __fmt;; + force-unlock) + __force_unlock;; + get) + __get ;; + graph) + __graph ;; + import) + __import;; + init) + __init ;; + login) + __login ;; + logout) + __logout ;; + output) + __output ;; + plan) + __plan ;; + providers) + test $CURRENT -lt 3 && __providers + [[ $words[2] = "mirror" ]] && __providers_mirror + [[ $words[2] = "schema" ]] && __providers_schema + ;; + refresh) + __refresh ;; + show) + __show ;; + state) + test $CURRENT -lt 3 && __state + [[ $words[2] = "list" ]] && __state_list + [[ $words[2] = "mv" ]] && __state_mv + [[ $words[2] = "push" ]] && __state_push + [[ $words[2] = "replace-provider" ]] && __state_replace_provider + [[ $words[2] = "rm" ]] && __state_rm + [[ $words[2] = "show" ]] && __state_show + ;; + taint) + __taint ;; + untaint) + __untaint ;; + validate) + __validate ;; + version) + __version ;; + workspace) + test $CURRENT -lt 3 && __workspace ;; +esac diff --git a/plugins/terragrunt/terragrunt.plugin.zsh b/plugins/terragrunt/terragrunt.plugin.zsh new file mode 100644 index 000000000..bbbf056e0 --- /dev/null +++ b/plugins/terragrunt/terragrunt.plugin.zsh @@ -0,0 +1,11 @@ +alias tg='terragrunt' +alias tga='terragrunt apply' +alias tgd='terragrunt destroy' +alias tgf='terragrunt fmt' +alias tgi='terragrunt init' +alias tgo='terragrunt output' +alias tgp='terragrunt plan' +alias tgv='terragrunt validate' +alias tgt='terragrunt taint' +alias tgc='terragrunt console' +alias tgfu='terragrunt force-unlock'