_terraform 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #compdef terraform
  2. compdef _terraform terraform
  3. (( ${+functions[_terraform_commands]} )) || _terraform_commands() {
  4. local -a _terraform_cmds
  5. _terraform_cmds=(
  6. 'apply:Create or update infrastructure'
  7. 'console:Try Terraform expressions at an interactive command prompt'
  8. 'destroy:Destroy previously-created infrastructure'
  9. 'fmt:Reformat your configuration in the standard style'
  10. 'force-unlock:Release a stuck lock on the current workspace'
  11. 'get:Install or upgrade remote Terraform modules'
  12. 'graph:Generate a Graphviz graph of the steps in an operation'
  13. 'import:Associate existing infrastructure with a Terraform resource'
  14. 'init:Prepare your working directory for other commands'
  15. 'login:Obtain and save credentials for a remote host'
  16. 'logout:Remove locally-stored credentials for a remote host'
  17. 'metadata:Metadata related commands'
  18. 'output:Show output values from your root module'
  19. 'plan:Show changes required by the current configuration'
  20. 'providers:Show the providers required for this configuration'
  21. 'refresh:Update the state to match remote systems'
  22. 'show:Show the current state or a saved plan'
  23. 'state:Advanced state management'
  24. 'taint:Mark a resource instance as not fully functional'
  25. 'test:Execute integration tests for Terraform modules'
  26. 'untaint:Remove the '\''tainted'\'' state from a resource instance'
  27. 'validate:Check whether the configuration is valid'
  28. 'version:Show the current Terraform version'
  29. 'workspace:Workspace management'
  30. )
  31. if (( CURRENT == 1 )); then
  32. _describe -t commands 'terraform commands' _terraform_cmds
  33. return
  34. fi
  35. local curcontext="${curcontext}"
  36. cmd="${${_terraform_cmds[(r)$words[1]:*]%%:*}}"
  37. curcontext="${curcontext%:*:*}:terraform-${cmd}:"
  38. local __chdir="${opt_args[-chdir]:-.}"
  39. if (( ${+functions[_terraform_$cmd]} )); then
  40. "_terraform_${cmd}"
  41. else
  42. _message "no more options"
  43. fi
  44. }
  45. (( ${+functions[_terraform_apply]} )) || _terraform_apply() {
  46. _arguments \
  47. '-auto-approve[Skip interactive approval of plan before applying.]' \
  48. '-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 -W __chdir -g "*.backup"' \
  49. '-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.]' \
  50. '-destroy[Destroy Terraform-managed infrastructure. The command "terraform destroy" is a convenience alias for this option.]' \
  51. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  52. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  53. '-input=[(true) Ask for input for variables if not directly set.]:input:(true false)' \
  54. '-no-color[If specified, output won'\''t contain any color.]' \
  55. '-parallelism=[(10) Limit the number of parallel resource operations.]:parallelism:' \
  56. '-refresh=[(true) Skip checking for external changes to remote objects while creating the plan. This can potentially make planning faster, but at the expense of possibly planning against a stale record of the remote system state.]:refresh:(true false)' \
  57. '*-replace=[(resource) Force replacement of a particular resource instance using its resource address. If applying would'\''ve normally produced an update or no-op action for this instance, Terraform will replace it instead. You can use this option multiple times to replace more than one object.]:resource:__terraform_state_resources' \
  58. '-state=[(terraform.tfstate) Path to read and save state (unless state-out is specified).]:statefile:_files -W __chdir -g "*.tfstate"' \
  59. '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]:statefile:_files -W __chdir -g "*.tfstate"' \
  60. '*-target=[(resource) Limit the operation to only the given module, resource, or resource instance and all of its dependencies. You can use this option multiple times to include more than one object. This is for exceptional use only.]:target:__terraform_state_resources' \
  61. '*-var=[(for=bar) Set a value for one of the input variables in the root module of the configuration. Use this option more than once to set more than one variable.]:var:' \
  62. '*-var-file=[(foo) Load variable values from the given file, in addition to the default files terraform.tfvars and *.auto.tfvars. Use this option more than once to include more than one variables file.]:file:_files -W __chdir -g "*.tfvars{,.json}"' \
  63. ':plan:_files -W __chdir -'
  64. }
  65. (( ${+functions[_terraform_console]} )) || _terraform_console() {
  66. _arguments \
  67. '-state=[(terraform.tfstate) Legacy option for the local backend only. See the local backend'\''s documentation for more information.]:statefile:_files -W __chdir -g "*.tfstate"' \
  68. '-plan[Create a new plan (as if running "terraform plan") and then evaluate expressions against its planned state, instead of evaluating against the current state. You can use this to inspect the effects of configuration changes that haven'\''t been applied yet.]' \
  69. '*-var=[(for=bar) Set a variable in the Terraform configuration. This flag can be set multiple times.]:var:' \
  70. '*-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 -W __chdir -g "*.tfvars{,.json}"'
  71. }
  72. (( ${+functions[_terraform_destroy]} )) || _terraform_destroy() {
  73. _arguments \
  74. '-auto-approve[Skip interactive approval of plan before applying.]' \
  75. '-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 -W __chdir -g "*.backup"' \
  76. '-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.]' \
  77. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  78. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  79. '-input=[(true) Ask for input for variables if not directly set.]:input:(true false)' \
  80. '-no-color[If specified, output won'\''t contain any color.]' \
  81. '-parallelism=[(10) Limit the number of parallel resource operations.]:parallelism:' \
  82. '-refresh=[(true) Update state prior to checking for differences. This has no effect if a plan file is given to apply.]:refresh:(true false)' \
  83. '-state=[(terraform.tfstate) Path to read and save state (unless state-out is specified).]:statefile:_files -W __chdir -g "*.tfstate"' \
  84. '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]:statefile:_files -W __chdir -g "*.tfstate"' \
  85. '*-target=[(resource) Limit the operation to only the given module, resource, or resource instance and all of its dependencies. You can use this option multiple times to include more than one object. This is for exceptional use only.]:target:__terraform_state_resources' \
  86. '*-var=[(for=bar) Set a value for one of the input variables in the root module of the configuration. Use this option more than once to set more than one variable.]:var:' \
  87. '*-var-file=[(foo) Load variable values from the given file, in addition to the default files terraform.tfvars and *.auto.tfvars. Use this option more than once to include more than one variables file.]:file:_files -W __chdir -g "*.tfvars{,.json}"'
  88. }
  89. (( ${+functions[_terraform_fmt]} )) || _terraform_fmt() {
  90. _arguments \
  91. '-list=[(true) Don'\''t list files whose formatting differs (always disabled if using STDIN)]:list:(true false)' \
  92. '-write=[(true) Don'\''t write to source files (always disabled if using STDIN or -check)]:write:(true false)' \
  93. '-diff[Display diffs of formatting changes]' \
  94. '-check[Check if the input is formatted. Exit status will be 0 if all input is properly formatted and non-zero otherwise.]' \
  95. '-no-color[If specified, output won'\''t contain any color.]' \
  96. '-recursive[Also process files in subdirectories. By default, only the given directory (or current directory) is processed.]' \
  97. '*:targets:_files -W __chdir -'
  98. }
  99. (( ${+functions[_terraform_force-unlock]} )) || _terraform_force-unlock() {
  100. _arguments \
  101. '-force[Don'\''t ask for input for unlock confirmation.]' \
  102. ':lock_id:'
  103. }
  104. (( ${+functions[_terraform_get]} )) || _terraform_get() {
  105. _arguments \
  106. '-update[Check already-downloaded modules for available updates and install the newest versions available.]' \
  107. '-no-color[Disable text coloring in the output.]' \
  108. '-test-directory=[(tests) Set the Terraform test directory, defaults to "tests".]:test_directory:_files -W __chdir -/'
  109. }
  110. (( ${+functions[_terraform_graph]} )) || _terraform_graph() {
  111. _arguments \
  112. '-draw-cycles[Highlight any cycles in the graph with colored edges. This helps when diagnosing cycle errors. This option is supported only when illustrating a real evaluation graph, selected using the -type=TYPE option.]' \
  113. '-module-depth=[(-1) (deprecated) In prior versions of Terraform, specified the depth of modules to show in the output.]:module_depth:' \
  114. '-plan=[Render graph using the specified plan file instead of the configuration in the current directory. Implies -type=apply.]:plan:_files -W __chdir -' \
  115. '-type=[(plan) Type of operation graph to output. Can be: plan, plan-refresh-only, plan-destroy, or apply. By default Terraform just summarizes the relationships between the resources in your configuration, without any particular operation in mind. Full operation graphs are more detailed but therefore often harder to read.]:type:(plan plan-refresh-only plan-destroy apply)'
  116. }
  117. (( ${+functions[_terraform_import]} )) || _terraform_import() {
  118. _arguments \
  119. '-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 -W __chdir -g "*.backup"' \
  120. '-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.]:config:_files -W __chdir -/' \
  121. '-input=[(true) Disable interactive input prompts.]:input:(true false)' \
  122. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  123. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  124. '-no-color[If specified, output will contain no color.]' \
  125. '-state=[(PATH) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:statefile:_files -W __chdir -g "*.tfstate"' \
  126. '-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 -W __chdir -g "*.tfstate"' \
  127. '*-var=[(for=bar) Set a variable in the Terraform configuration. This flag can be set multiple times. This is only useful with the "-config" flag.]:var:' \
  128. '*-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 -W __chdir -g "*.tfvars{,.json}"' \
  129. ':addr:' \
  130. ':id:'
  131. }
  132. (( ${+functions[_terraform_init]} )) || _terraform_init() {
  133. _arguments \
  134. '-backend=[(true) Disable backend or Terraform Cloud initialization for this configuration and use what was previously initialized instead.]:backend:(true false)' \
  135. '-backend-config=[Configuration to be merged with what is in the configuration file'\''s '\''backend'\'' block. This can be either a path to an HCL file with key/value assignments (same format as terraform.tfvars) or a '\''key=value'\'' format, and can be specified multiple times. The backend type must be in the configuration itself.]:backend_config:_files -W __chdir -' \
  136. '-force-copy[Suppress prompts about copying state data. This is equivalent to providing a "yes" to all confirmation prompts.]' \
  137. '-from-module=[Copy the contents of the given module into the target directory before initialization.]:from_module:_files -W __chdir -/' \
  138. '-get=[(true) Disable downloading modules for this configuration.]:get:(true false)' \
  139. '-input=[(true) Disable interactive prompts. Note that some actions may require interactive prompts and will error if input is disabled.]:input:(true false)' \
  140. '-lock=[(true) Don'\''t hold a state lock during backend migration. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  141. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  142. '-no-color[If specified, output will contain no color.]' \
  143. '-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 -W __chdir -/' \
  144. '-reconfigure[Reconfigure the backend, ignoring any saved configuration.]' \
  145. '-migrate-state[Reconfigure a backend, and attempt to migrate any existing state.]' \
  146. '-upgrade[Install the latest module and provider versions allowed within configured constraints, overriding the default behavior of selecting exactly the version recorded in the dependency lockfile.]' \
  147. '-lockfile=[Set a dependency lockfile mode. Currently only "readonly" is valid.]:lockfile:( readonly )' \
  148. '-ignore-remote-version[A rare option used for Terraform Cloud and the remote backend only. Set this to ignore checking that the local and remote Terraform versions use compatible state representations, making an operation proceed even when there is a potential mismatch. See the documentation on configuring Terraform with Terraform Cloud for more information.]' \
  149. '-test-directory=[(tests) Set the Terraform test directory, defaults to "tests".]:test_directory:_files -W __chdir -/'
  150. }
  151. (( ${+functions[_terraform_login]} )) || _terraform_login() {
  152. _arguments \
  153. ':hostname:'
  154. }
  155. (( ${+functions[_terraform_logout]} )) || _terraform_logout() {
  156. _arguments \
  157. ':hostname:'
  158. }
  159. (( ${+functions[_terraform_metadata]} )) || _terraform_metadata() {
  160. _arguments \
  161. '*::terraform metadata command:_terraform_metadata_commands'
  162. }
  163. (( ${+functions[_terraform_metadata_commands]} )) || _terraform_metadata_commands() {
  164. local -a _metadata_cmds
  165. _metadata_cmds=(
  166. 'functions:Show signatures and descriptions for the available functions'
  167. )
  168. if (( CURRENT == 1 )); then
  169. _describe -t commands "terraform metadata commands" _metadata_cmds
  170. return
  171. fi
  172. local curcontext="${curcontext}"
  173. cmd="${${_metadata_cmds[(r)$words[1]:*]%%:*}}"
  174. curcontext="${curcontext%:*:*}:terraform-metadata-${cmd}:"
  175. if (( ${+functions[_terraform_metadata_$cmd]} )); then
  176. "_terraform_metadata_${cmd}"
  177. else
  178. _message "no more options"
  179. fi
  180. }
  181. (( ${+functions[_terraform_metadata_functions]} )) || _terraform_metadata_functions() {
  182. _arguments \
  183. '-json[]'
  184. }
  185. (( ${+functions[_terraform_output]} )) || _terraform_output() {
  186. _arguments \
  187. '-state=[(path) Path to the state file to read. Defaults to "terraform.tfstate". Ignored when remote state is used.]:statefile:_files -W __chdir -g "*.tfstate"' \
  188. '-no-color[If specified, output will contain no color.]' \
  189. '-json[If specified, machine readable output will be printed in JSON format]' \
  190. '-raw[For value types that can be automatically converted to a string, will print the raw string directly, rather than a human-oriented representation of the value.]' \
  191. ':name:'
  192. }
  193. (( ${+functions[_terraform_plan]} )) || _terraform_plan() {
  194. _arguments \
  195. '-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.]' \
  196. '-destroy[Select the "destroy" planning mode, which creates a plan to destroy all objects currently managed by this Terraform configuration instead of the usual behavior.]' \
  197. '-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]' \
  198. '-input=[(true) Ask for input for variables if not directly set.]:input:(true false)' \
  199. '-generate-config-out=[(path) (Experimental) If import blocks are present in configuration, instructs Terraform to generate HCL for any imported resources not already present. The configuration is written to a new file at PATH, which must not already exist. Terraform may still attempt to write configuration if the plan errors.]:generate_config_out:' \
  200. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  201. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  202. '-no-color[If specified, output will contain no color.]' \
  203. '-out=[(path) Write a plan file to the given path. This can be used as input to the "apply" command.]:out:' \
  204. '-parallelism=[(10) Limit the number of concurrent operations.]:parallelism:' \
  205. '-refresh=[(true) Skip checking for external changes to remote objects while creating the plan. This can potentially make planning faster, but at the expense of possibly planning against a stale record of the remote system state.]:refresh:(true false)' \
  206. '-refresh-only[Select the "refresh only" planning mode, which checks whether remote objects still match the outcome of the most recent Terraform apply but does not propose any actions to undo any changes made outside of Terraform.]' \
  207. '*-replace=[(resource) Force replacement of a particular resource instance using its resource address. If the plan would'\''ve normally produced an update or no-op action for this instance, Terraform will plan to replace it instead. You can use this option multiple times to replace more than one object.]:replace:__terraform_state_resources' \
  208. '-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 -W __chdir -g "*.tfstate"' \
  209. '*-target=[(resource) Limit the planning operation to only the given module, resource, or resource instance and all of its dependencies. You can use this option multiple times to include more than one object. This is for exceptional use only.]:target:__terraform_state_resources' \
  210. '*-var=[(for=bar) Set a value for one of the input variables in the root module of the configuration. Use this option more than once to set more than one variable.]:var:' \
  211. '*-var-file=[(foo) Load variable values from the given file, in addition to the default files terraform.tfvars and *.auto.tfvars. Use this option more than once to include more than one variables file.]:file:_files -W __chdir -g "*.tfvars{,.json}"'
  212. }
  213. (( ${+functions[_terraform_providers]} )) || _terraform_providers() {
  214. _arguments \
  215. '-test-directory=[(path) Set the Terraform test directory, defaults to "tests".]:test_directory:_files -W __chdir -/' \
  216. '*::terraform providers command:_terraform_providers_commands'
  217. }
  218. (( ${+functions[_terraform_providers_commands]} )) || _terraform_providers_commands() {
  219. local -a _providers_cmds
  220. _providers_cmds=(
  221. 'lock:Write out dependency locks for the configured providers'
  222. 'mirror:Save local copies of all required provider plugins'
  223. 'schema:Show schemas for the providers used in the configuration'
  224. )
  225. if (( CURRENT == 1 )); then
  226. _describe -t commands "terraform providers commands" _providers_cmds
  227. return
  228. fi
  229. local curcontext="${curcontext}"
  230. cmd="${${_providers_cmds[(r)$words[1]:*]%%:*}}"
  231. curcontext="${curcontext%:*:*}:terraform-providers-${cmd}:"
  232. if (( ${+functions[_terraform_providers_$cmd]} )); then
  233. "_terraform_providers_${cmd}"
  234. else
  235. _message "no more options"
  236. fi
  237. }
  238. (( ${+functions[_terraform_providers_lock]} )) || _terraform_providers_lock() {
  239. _arguments \
  240. '-fs-mirror=[(dir) Consult the given filesystem mirror directory instead of the origin registry for each of the given providers.]:fs_mirror:_files -W __chdir -/' \
  241. '-net-mirror=[(url) Consult the given network mirror (given as a base URL) instead of the origin registry for each of the given providers.]:net_mirror:' \
  242. '*-platform=[(os_arch) Choose a target platform to request package checksums for.]:platform:' \
  243. '*:provider:'
  244. }
  245. (( ${+functions[_terraform_providers_mirror]} )) || _terraform_providers_mirror() {
  246. _arguments \
  247. '*-platform=[(os_arch) Choose which target platform to build a mirror for.]:platform:' \
  248. '::' \
  249. ':target_dir:_files -W __chdir -/'
  250. }
  251. (( ${+functions[_terraform_providers_schema]} )) || _terraform_providers_schema() {
  252. _arguments \
  253. '-json[]'
  254. }
  255. (( ${+functions[_terraform_refresh]} )) || _terraform_refresh() {
  256. _arguments \
  257. '-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 -W __chdir -g "*.backup"' \
  258. '-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.]' \
  259. '-input=[(true) Ask for input for variables if not directly set.]:input:(true false)' \
  260. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  261. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  262. '-no-color[If specified, output will not contain any color.]' \
  263. '-parallelism=[(10) Limit the number of parallel resource operations.]:parallelism:' \
  264. '-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]:statefile:_files -W __chdir -g "*.tfstate"' \
  265. '-state-out=[(path) Path to write state to that is different than "-state". This can be used to preserve the old state.]:statefile:_files -W __chdir -g "*.tfstate"' \
  266. '*-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:__terraform_state_resources' \
  267. '*-var=[(for=bar) Set a variable in the Terraform configuration. This flag can be set multiple times.]:var:' \
  268. '*-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 -W __chdir -g "*.tfvars{,.json}"'
  269. }
  270. (( ${+functions[_terraform_show]} )) || _terraform_show() {
  271. _arguments \
  272. '-json[If specified, output the Terraform plan or state in a machine-readable form.]' \
  273. '-no-color[If specified, output will not contain any color.]' \
  274. ':path:_files -W __chdir -g "*.tfstate"'
  275. }
  276. (( ${+functions[_terraform_state]} )) || _terraform_state() {
  277. _arguments \
  278. '*::terraform state command:_terraform_state_commands'
  279. }
  280. (( ${+functions[_terraform_state_commands]} )) || _terraform_state_commands() {
  281. local -a _state_cmds
  282. _state_cmds=(
  283. 'list:List resources in the state'
  284. 'mv:Move an item in the state'
  285. 'pull:Pull current state and output to stdout'
  286. 'push:Update remote state from a local state file'
  287. 'replace-provider:Replace provider in the state'
  288. 'rm:Remove instances from the state'
  289. 'show:Show a resource in the state'
  290. )
  291. if (( CURRENT == 1 )); then
  292. _describe -t commands "terraform state commands" _state_cmds
  293. return
  294. fi
  295. local curcontext="${curcontext}"
  296. cmd="${${_state_cmds[(r)$words[1]:*]%%:*}}"
  297. curcontext="${curcontext%:*:*}:terraform-state-${cmd}:"
  298. if (( ${+functions[_terraform_state_$cmd]} )); then
  299. "_terraform_state_${cmd}"
  300. else
  301. _message "no more options"
  302. fi
  303. }
  304. (( ${+functions[_terraform_state_list]} )) || _terraform_state_list() {
  305. _arguments \
  306. '-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.]:statefile:_files -W __chdir -g "*.tfstate"' \
  307. '-id=[(id) Filters the results to include only instances whose resource types have an attribute named id whose value equals the given id string.]:id:' \
  308. '*:address:__terraform_state_resources'
  309. }
  310. (( ${+functions[_terraform_state_mv]} )) || _terraform_state_mv() {
  311. _arguments \
  312. '-dry-run[If set, prints out what would'\''ve been moved but doesn'\''t actually move anything.]' \
  313. '-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 -W __chdir -g "*.backup"' \
  314. '-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 -W __chdir -g "*.backup"' \
  315. '-ignore-remote-version[A rare option used for the remote backend only. See the remote backend documentation for more information.]' \
  316. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  317. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  318. '-state=[(path) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:statefile:_files -W __chdir -g "*.tfstate"' \
  319. '-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 -W __chdir -g "*.tfstate"' \
  320. '::' \
  321. ':source:__terraform_state_resources' \
  322. ':destination: '
  323. }
  324. (( ${+functions[_terraform_state_push]} )) || _terraform_state_push() {
  325. _arguments \
  326. '-force[Write the state even if lineages don'\''t match or the remote serial is higher.]' \
  327. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  328. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  329. '::' \
  330. ':destination:_files'
  331. }
  332. (( ${+functions[_terraform_state_replace-provider]} )) || _terraform_state_replace-provider() {
  333. _arguments \
  334. '-auto-approve[Skip interactive approval.]' \
  335. '-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 -W __chdir -g "*.backup"' \
  336. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  337. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  338. '-state=[(PATH) Path to the source state file. Defaults to the configured backend, or "terraform.tfstate"]:statefile:_files -W __chdir -g "*.tfstate"' \
  339. '::' \
  340. ':from_provider_fqn:' \
  341. ':to_provider_fqn:'
  342. }
  343. (( ${+functions[_terraform_state_rm]} )) || _terraform_state_rm() {
  344. _arguments \
  345. '-dry-run[If set, prints out what would'\''ve been removed but doesn'\''t actually remove anything.]' \
  346. '-backup=[(PATH) Path where Terraform should write the backup for the original state.]::backupfile:_files -W __chdir -g "*.backup"' \
  347. '-ignore-remote-version[Continue even if remote and local Terraform versions are incompatible. This may result in an unusable workspace, and should be used with extreme caution.]' \
  348. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  349. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  350. '-state=[(PATH) Path to the state file to update. Defaults to the current workspace state.]:statefile:_files -W __chdir -g "*.tfstate"' \
  351. '*:address:__terraform_state_resources'
  352. }
  353. (( ${+functions[_terraform_state_show]} )) || _terraform_state_show() {
  354. _arguments \
  355. '-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 -W __chdir -g "*.tfstate"' \
  356. "*:address:__terraform_state_resources"
  357. }
  358. (( ${+functions[__terraform_state_resources]} )) || __terraform_state_resources() {
  359. local resource
  360. local -a resources
  361. terraform -chdir="${__chdir}" state list -state="${opt_args[-state]}" 2>/dev/null | while read -r resource; do
  362. resources+=( "${resource}" )
  363. done
  364. compadd "${@}" - "${resources[@]}"
  365. }
  366. (( ${+functions[_terraform_taint]} )) || _terraform_taint() {
  367. _arguments \
  368. '-allow-missing[If specified, the command will succeed (exit code 0) even if the resource is missing.]' \
  369. '-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 -W __chdir -g "*.backup"' \
  370. '-ignore-remote-version[A rare option used for the remote backend only. See the remote backend documentation for more information.]' \
  371. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  372. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  373. '-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]:statefile:_files -W __chdir -g "*.tfstate"' \
  374. '-state-out=[(path) Path to write updated state file. By default, the "-state" path will be used.]:statefile:_files -W __chdir -g "*.tfstate"' \
  375. '*:address:__terraform_state_resources'
  376. }
  377. (( ${+functions[_terraform_test]} )) || _terraform_test() {
  378. _arguments \
  379. '-cloud-run=[(source) If specified, Terraform will execute this test run remotely using Terraform Cloud. You must specify the source of a module registered in a private module registry as the argument to this flag. This allows Terraform to associate the cloud run with the correct Terraform Cloud module and organization.]:cloud_run:' \
  380. '*-filter=[(testfile) If specified, Terraform will only execute the test files specified by this flag. You can use this option multiple times to execute more than one test file.]:testfile:_files -W __chdir -g "*.tftest.hcl"' \
  381. '-json[If specified, machine readable output will be printed in JSON format]' \
  382. '-no-color[If specified, machine readable output will be printed in JSON format]' \
  383. '-test-directory=[(path) Set the Terraform test directory, defaults to "tests".]:test_directory:_files -W __chdir -/' \
  384. '*-var=[(for=bar) Set a value for one of the input variables in the root module of the configuration. Use this option more than once to set more than one variable.]:var:' \
  385. '*-var-file=[(foo) Load variable values from the given file, in addition to the default files terraform.tfvars and *.auto.tfvars. Use this option more than once to include more than one variables file.]:file:_files -W __chdir -g "*.tfvars{,.json}"' \
  386. '-verbose[Print the plan or state for each test run block as it executes.]' \
  387. }
  388. (( ${+functions[_terraform_untaint]} )) || _terraform_untaint() {
  389. _arguments \
  390. '-allow-missing[If specified, the command will succeed (exit code 0) even if the resource is missing.]' \
  391. '-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 -W __chdir -g "*.backup"' \
  392. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  393. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  394. '-state=[(path) Path to read and save state (unless state-out is specified). Defaults to "terraform.tfstate".]:statefile:_files -W __chdir -g "*.tfstate"' \
  395. '-state-out=[(path) Path to write updated state file. By default, the "-state" path will be used.]:statefile:_files -W __chdir -g "*.tfstate"' \
  396. ':name:__terraform_state_resources'
  397. }
  398. (( ${+functions[_terraform_validate]} )) || _terraform_validate() {
  399. _arguments \
  400. '-json[Produce output in a machine-readable JSON format, suitable for use in text editor integrations and other automated systems.]' \
  401. '-no-color[If specified, output will not contain any color.]' \
  402. '-no-tests[If specified, Terraform will not validate test files.]' \
  403. '-test-directory=[(path) Set the Terraform test directory, defaults to "tests".]:test_directory:_files -W __chdir -/' \
  404. ':dir:_files -W __chdir -/'
  405. }
  406. (( ${+functions[_terraform_version]} )) || _terraform_version() {
  407. _arguments \
  408. '-json[Output the version information as a JSON object.]' \
  409. '::'
  410. }
  411. (( ${+functions[_terraform_workspace]} )) || _terraform_workspace() {
  412. _arguments \
  413. '*::terraform workspace command:_terraform_workspace_commands'
  414. }
  415. (( ${+functions[_terraform_workspace_commands]} )) || _terraform_workspace_commands() {
  416. local -a _workspace_cmds
  417. _workspace_cmds=(
  418. 'delete:Delete a workspace'
  419. 'list:List Workspaces'
  420. 'new:Create a new workspace'
  421. 'select:Select a workspace'
  422. 'show:Show the name of the current workspace'
  423. )
  424. if (( CURRENT == 1 )); then
  425. _describe -t commands "terraform workspace commands" _workspace_cmds
  426. return
  427. fi
  428. local curcontext="${curcontext}"
  429. cmd="${${_workspace_cmds[(r)$words[1]:*]%%:*}}"
  430. curcontext="${curcontext%:*:*}:terraform-workspace-${cmd}:"
  431. if (( ${+functions[_terraform_workspace_$cmd]} )); then
  432. "_terraform_workspace_${cmd}"
  433. else
  434. _message "no more options"
  435. fi
  436. }
  437. (( ${+functions[_terraform_workspace_delete]} )) || _terraform_workspace_delete() {
  438. _arguments \
  439. '-force[Remove a workspace even if it is managing resources. Terraform can no longer track or manage the workspace'\''s infrastructure.]' \
  440. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  441. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  442. '::' \
  443. ':name:__terraform_workspaces'
  444. }
  445. (( ${+functions[_terraform_workspace_list]} )) || _terraform_workspace_list() {
  446. _arguments
  447. }
  448. (( ${+functions[_terraform_workspace_new]} )) || _terraform_workspace_new() {
  449. _arguments \
  450. '-lock=[(true) Don'\''t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.]:lock:(true false)' \
  451. '-lock-timeout=[(0s) Duration to retry a state lock.]:lock_timeout:' \
  452. '-state=[(path) Copy an existing state file into the new workspace.]:statefile:_files -W __chdir -g "*.tfstate"' \
  453. '::' \
  454. ':name:'
  455. }
  456. (( ${+functions[_terraform_workspace_select]} )) || _terraform_workspace_select() {
  457. _arguments \
  458. '-or-create=[(false) Create the Terraform workspace if it doesn'\''t exist.]:or_create:(true false)' \
  459. '::' \
  460. ':name:__terraform_workspaces'
  461. }
  462. (( ${+functions[_terraform_workspace_show]} )) || _terraform_workspace_show() {
  463. _arguments
  464. }
  465. (( ${+functions[__terraform_workspaces]} )) || __terraform_workspaces() {
  466. local workspace
  467. local -a workspaces
  468. terraform -chdir="${__chdir}" workspace list | while read -r workspace; do
  469. if [[ -z "${workspace}" ]]; then
  470. continue
  471. fi
  472. workspaces+=( "${workspace#[ *] }" )
  473. done
  474. compadd "${@}" - "${workspaces[@]}"
  475. }
  476. _terraform() {
  477. _arguments \
  478. '-chdir=[(DIR) Switch to a different working directory before executing the given subcommand.]:chdir:_files -W __chdir -/' \
  479. '-help[Show this help output, or the help for a specified subcommand.]' \
  480. '-version[An alias for the "version" subcommand.]' \
  481. '*::terraform command:_terraform_commands'
  482. }
  483. # don't run the completion function when being source-ed or eval-ed
  484. if [ "${funcstack[1]}" = '_terraform' ]; then
  485. _terraform
  486. fi