terraform_wrapper/commands/
version.rs1use crate::Terraform;
2use crate::command::TerraformCommand;
3use crate::error::Result;
4use crate::exec;
5
6#[cfg(feature = "json")]
7use crate::types::version::VersionInfo;
8
9#[derive(Debug, Clone)]
24pub struct VersionCommand {
25 json: bool,
26}
27
28impl Default for VersionCommand {
29 fn default() -> Self {
30 Self { json: true }
31 }
32}
33
34impl VersionCommand {
35 #[must_use]
37 pub fn new() -> Self {
38 Self::default()
39 }
40
41 #[must_use]
43 pub fn no_json(mut self) -> Self {
44 self.json = false;
45 self
46 }
47}
48
49#[cfg(feature = "json")]
50impl TerraformCommand for VersionCommand {
51 type Output = VersionInfo;
52
53 fn args(&self) -> Vec<String> {
54 let mut args = vec!["version".to_string()];
55 if self.json {
56 args.push("-json".to_string());
57 }
58 args
59 }
60
61 async fn execute(&self, tf: &Terraform) -> Result<VersionInfo> {
62 let output = exec::run_terraform(tf, self.args()).await?;
63 serde_json::from_str(&output.stdout).map_err(|e| crate::error::Error::Json {
64 message: "failed to parse version json".to_string(),
65 source: e,
66 })
67 }
68}
69
70#[cfg(not(feature = "json"))]
71impl TerraformCommand for VersionCommand {
72 type Output = exec::CommandOutput;
73
74 fn args(&self) -> Vec<String> {
75 let mut args = vec!["version".to_string()];
76 if self.json {
77 args.push("-json".to_string());
78 }
79 args
80 }
81
82 async fn execute(&self, tf: &Terraform) -> Result<exec::CommandOutput> {
83 exec::run_terraform(tf, self.args()).await
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn default_args_include_json() {
93 let cmd = VersionCommand::new();
94 assert_eq!(cmd.args(), vec!["version", "-json"]);
95 }
96
97 #[test]
98 fn no_json_args() {
99 let cmd = VersionCommand::new().no_json();
100 assert_eq!(cmd.args(), vec!["version"]);
101 }
102}