use crate::command::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
use serde_json::Value;
#[derive(Debug, Clone)]
pub struct ContextInspectCommand {
contexts: Vec<String>,
format: Option<String>,
pub executor: CommandExecutor,
}
impl ContextInspectCommand {
#[must_use]
pub fn new(context: impl Into<String>) -> Self {
Self {
contexts: vec![context.into()],
format: None,
executor: CommandExecutor::new(),
}
}
#[must_use]
pub fn add_context(mut self, context: impl Into<String>) -> Self {
self.contexts.push(context.into());
self
}
#[must_use]
pub fn format(mut self, template: impl Into<String>) -> Self {
self.format = Some(template.into());
self
}
}
#[async_trait]
impl DockerCommand for ContextInspectCommand {
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(), "inspect".to_string()];
if let Some(format) = &self.format {
args.push("--format".to_string());
args.push(format.clone());
}
args.extend(self.contexts.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 {
pub fn parse_context_info(&self) -> Result<Vec<Value>> {
if self.stdout.trim().is_empty() {
return Ok(Vec::new());
}
let contexts: Vec<Value> = serde_json::from_str(&self.stdout)?;
Ok(contexts)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_context_inspect_single() {
let cmd = ContextInspectCommand::new("production");
let args = cmd.build_command_args();
assert_eq!(args[0], "context");
assert_eq!(args[1], "inspect");
assert!(args.contains(&"production".to_string()));
}
#[test]
fn test_context_inspect_multiple() {
let cmd = ContextInspectCommand::new("context1").add_context("context2");
let args = cmd.build_command_args();
assert!(args.contains(&"context1".to_string()));
assert!(args.contains(&"context2".to_string()));
}
#[test]
fn test_context_inspect_with_format() {
let cmd = ContextInspectCommand::new("production").format("{{.Name}}");
let args = cmd.build_command_args();
assert!(args.contains(&"--format".to_string()));
assert!(args.contains(&"{{.Name}}".to_string()));
}
}