use crate::Terraform;
use crate::command::TerraformCommand;
use crate::error::Result;
use crate::exec::{self, CommandOutput};
#[derive(Debug, Clone, Default)]
pub struct ModulesCommand {
json: bool,
raw_args: Vec<String>,
}
impl ModulesCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn json(mut self) -> Self {
self.json = true;
self
}
#[must_use]
pub fn arg(mut self, arg: impl Into<String>) -> Self {
self.raw_args.push(arg.into());
self
}
}
impl TerraformCommand for ModulesCommand {
type Output = CommandOutput;
fn args(&self) -> Vec<String> {
let mut args = vec!["modules".to_string()];
if self.json {
args.push("-json".to_string());
}
args.extend(self.raw_args.clone());
args
}
async fn execute(&self, tf: &Terraform) -> Result<CommandOutput> {
exec::run_terraform(tf, self.args()).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_args() {
let cmd = ModulesCommand::new();
assert_eq!(cmd.args(), vec!["modules"]);
}
#[test]
fn with_json() {
let cmd = ModulesCommand::new().json();
assert_eq!(cmd.args(), vec!["modules", "-json"]);
}
}