use crate::Terraform;
use crate::command::TerraformCommand;
use crate::error::Result;
use crate::exec;
#[cfg(feature = "json")]
use crate::types::version::VersionInfo;
#[derive(Debug, Clone)]
pub struct VersionCommand {
json: bool,
}
impl Default for VersionCommand {
fn default() -> Self {
Self { json: true }
}
}
impl VersionCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn no_json(mut self) -> Self {
self.json = false;
self
}
}
#[cfg(feature = "json")]
impl TerraformCommand for VersionCommand {
type Output = VersionInfo;
fn args(&self) -> Vec<String> {
let mut args = vec!["version".to_string()];
if self.json {
args.push("-json".to_string());
}
args
}
async fn execute(&self, tf: &Terraform) -> Result<VersionInfo> {
let output = exec::run_terraform(tf, self.args()).await?;
serde_json::from_str(&output.stdout).map_err(|e| crate::error::Error::Json {
message: "failed to parse version json".to_string(),
source: e,
})
}
}
#[cfg(not(feature = "json"))]
impl TerraformCommand for VersionCommand {
type Output = exec::CommandOutput;
fn args(&self) -> Vec<String> {
let mut args = vec!["version".to_string()];
if self.json {
args.push("-json".to_string());
}
args
}
async fn execute(&self, tf: &Terraform) -> Result<exec::CommandOutput> {
exec::run_terraform(tf, self.args()).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_args_include_json() {
let cmd = VersionCommand::new();
assert_eq!(cmd.args(), vec!["version", "-json"]);
}
#[test]
fn no_json_args() {
let cmd = VersionCommand::new().no_json();
assert_eq!(cmd.args(), vec!["version"]);
}
}