use crate::command::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
#[derive(Debug, Clone)]
pub struct ContextUseCommand {
name: String,
pub executor: CommandExecutor,
}
impl ContextUseCommand {
#[must_use]
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
executor: CommandExecutor::new(),
}
}
}
#[async_trait]
impl DockerCommand for ContextUseCommand {
type Output = CommandOutput;
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["context".to_string(), "use".to_string(), self.name.clone()];
args.extend(self.executor.raw_args.clone());
args
}
async fn execute(&self) -> Result<Self::Output> {
let args = self.build_command_args();
self.execute_command(args).await
}
}
impl CommandOutput {
#[must_use]
pub fn context_switched(&self) -> bool {
self.exit_code == 0 && self.stderr.is_empty()
}
pub fn switched_to_context(&self) -> Option<String> {
if self.context_switched() {
self.stdout.split_whitespace().last().map(String::from)
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_context_use_basic() {
let cmd = ContextUseCommand::new("production");
let args = cmd.build_command_args();
assert_eq!(args[0], "context");
assert_eq!(args[1], "use");
assert_eq!(args[2], "production");
}
#[test]
fn test_context_switched() {
let output = CommandOutput {
stdout: "Current context is now \"production\"".to_string(),
stderr: String::new(),
exit_code: 0,
success: true,
};
assert!(output.context_switched());
assert_eq!(
output.switched_to_context(),
Some("\"production\"".to_string())
);
}
}