Skip to main content

terraform_wrapper/commands/
get.rs

1use crate::Terraform;
2use crate::command::TerraformCommand;
3use crate::error::Result;
4use crate::exec::{self, CommandOutput};
5
6/// Command for downloading and updating remote modules.
7///
8/// Downloads modules referenced in the configuration. This is also done
9/// automatically by `terraform init`, but `get` is useful for updating
10/// modules independently.
11///
12/// ```no_run
13/// # async fn example() -> terraform_wrapper::error::Result<()> {
14/// use terraform_wrapper::{Terraform, TerraformCommand};
15/// use terraform_wrapper::commands::get::GetCommand;
16///
17/// let tf = Terraform::builder().working_dir("/tmp/infra").build()?;
18/// GetCommand::new()
19///     .update()
20///     .execute(&tf)
21///     .await?;
22/// # Ok(())
23/// # }
24/// ```
25#[derive(Debug, Clone, Default)]
26pub struct GetCommand {
27    update: bool,
28    no_color: bool,
29    raw_args: Vec<String>,
30}
31
32impl GetCommand {
33    /// Create a new get command with default options.
34    #[must_use]
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    /// Check for and download updates to already-installed modules (`-update`).
40    #[must_use]
41    pub fn update(mut self) -> Self {
42        self.update = true;
43        self
44    }
45
46    /// Disable color output (`-no-color`).
47    #[must_use]
48    pub fn no_color(mut self) -> Self {
49        self.no_color = true;
50        self
51    }
52
53    /// Add a raw argument (escape hatch for unsupported options).
54    #[must_use]
55    pub fn arg(mut self, arg: impl Into<String>) -> Self {
56        self.raw_args.push(arg.into());
57        self
58    }
59}
60
61impl TerraformCommand for GetCommand {
62    type Output = CommandOutput;
63
64    fn args(&self) -> Vec<String> {
65        let mut args = vec!["get".to_string()];
66        if self.update {
67            args.push("-update".to_string());
68        }
69        if self.no_color {
70            args.push("-no-color".to_string());
71        }
72        args.extend(self.raw_args.clone());
73        args
74    }
75
76    async fn execute(&self, tf: &Terraform) -> Result<CommandOutput> {
77        exec::run_terraform(tf, self.args()).await
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn default_args() {
87        let cmd = GetCommand::new();
88        assert_eq!(cmd.args(), vec!["get"]);
89    }
90
91    #[test]
92    fn with_update() {
93        let cmd = GetCommand::new().update();
94        assert_eq!(cmd.args(), vec!["get", "-update"]);
95    }
96
97    #[test]
98    fn with_no_color() {
99        let cmd = GetCommand::new().no_color();
100        assert_eq!(cmd.args(), vec!["get", "-no-color"]);
101    }
102
103    #[test]
104    fn all_options() {
105        let cmd = GetCommand::new().update().no_color();
106        let args = cmd.args();
107        assert_eq!(args[0], "get");
108        assert!(args.contains(&"-update".to_string()));
109        assert!(args.contains(&"-no-color".to_string()));
110    }
111}