terraform_wrapper/commands/
get.rs1use crate::Terraform;
2use crate::command::TerraformCommand;
3use crate::error::Result;
4use crate::exec::{self, CommandOutput};
5
6#[derive(Debug, Clone, Default)]
26pub struct GetCommand {
27 update: bool,
28 no_color: bool,
29 raw_args: Vec<String>,
30}
31
32impl GetCommand {
33 #[must_use]
35 pub fn new() -> Self {
36 Self::default()
37 }
38
39 #[must_use]
41 pub fn update(mut self) -> Self {
42 self.update = true;
43 self
44 }
45
46 #[must_use]
48 pub fn no_color(mut self) -> Self {
49 self.no_color = true;
50 self
51 }
52
53 #[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}