Skip to main content

terraform_wrapper/
command.rs

1use crate::Terraform;
2use crate::error::Result;
3
4/// Trait implemented by all Terraform command builders.
5///
6/// Each command implements [`args`](TerraformCommand::args) to produce its CLI
7/// arguments and [`execute`](TerraformCommand::execute) to run against a
8/// [`Terraform`] client.
9pub trait TerraformCommand: Send + Sync {
10    /// The output type produced by this command.
11    type Output: Send;
12
13    /// Build the argument list for this command.
14    ///
15    /// Returns the subcommand name followed by all flags and options.
16    /// For example: `["init", "-upgrade", "-backend-config=key=value"]`.
17    fn args(&self) -> Vec<String>;
18
19    /// Whether this command supports the `-input=false` flag.
20    ///
21    /// Defaults to `false`. Commands that accept `-input` (init, plan, apply,
22    /// destroy, import, refresh) should override this to return `true`.
23    fn supports_input(&self) -> bool {
24        false
25    }
26
27    /// Build the argument list with `-input=false` injected when appropriate.
28    ///
29    /// Calls [`args`](TerraformCommand::args) and, if `tf.no_input` is set
30    /// and [`supports_input`](TerraformCommand::supports_input) returns `true`,
31    /// inserts `-input=false` after the subcommand name.
32    fn prepare_args(&self, tf: &Terraform) -> Vec<String> {
33        let mut args = self.args();
34        if tf.no_input && self.supports_input() {
35            args.insert(1, "-input=false".to_string());
36        }
37        args
38    }
39
40    /// Execute this command against the given Terraform client.
41    fn execute(
42        &self,
43        tf: &Terraform,
44    ) -> impl std::future::Future<Output = Result<Self::Output>> + Send;
45}