use crate::Terraform;
use crate::command::TerraformCommand;
use crate::error::Result;
use crate::exec::{self, CommandOutput};
#[derive(Debug, Clone, Default)]
pub struct GetCommand {
update: bool,
no_color: bool,
raw_args: Vec<String>,
}
impl GetCommand {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn update(mut self) -> Self {
self.update = true;
self
}
#[must_use]
pub fn no_color(mut self) -> Self {
self.no_color = true;
self
}
#[must_use]
pub fn arg(mut self, arg: impl Into<String>) -> Self {
self.raw_args.push(arg.into());
self
}
}
impl TerraformCommand for GetCommand {
type Output = CommandOutput;
fn args(&self) -> Vec<String> {
let mut args = vec!["get".to_string()];
if self.update {
args.push("-update".to_string());
}
if self.no_color {
args.push("-no-color".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 = GetCommand::new();
assert_eq!(cmd.args(), vec!["get"]);
}
#[test]
fn with_update() {
let cmd = GetCommand::new().update();
assert_eq!(cmd.args(), vec!["get", "-update"]);
}
#[test]
fn with_no_color() {
let cmd = GetCommand::new().no_color();
assert_eq!(cmd.args(), vec!["get", "-no-color"]);
}
#[test]
fn all_options() {
let cmd = GetCommand::new().update().no_color();
let args = cmd.args();
assert_eq!(args[0], "get");
assert!(args.contains(&"-update".to_string()));
assert!(args.contains(&"-no-color".to_string()));
}
}