iggy_cli/commands/binary_context/
create_context.rs1use async_trait::async_trait;
20use tracing::{Level, event};
21
22use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
23use iggy_common::Client;
24
25use super::common::{ContextConfig, ContextManager, validate_transport};
26
27pub struct CreateContextCmd {
28 context_name: String,
29 context_config: ContextConfig,
30}
31
32impl CreateContextCmd {
33 pub fn new(context_name: String, context_config: ContextConfig) -> Self {
34 Self {
35 context_name,
36 context_config,
37 }
38 }
39}
40
41#[async_trait]
42impl CliCommand for CreateContextCmd {
43 fn explain(&self) -> String {
44 let context_name = &self.context_name;
45 format!("create context {context_name}")
46 }
47
48 fn login_required(&self) -> bool {
49 false
50 }
51
52 fn connection_required(&self) -> bool {
53 false
54 }
55
56 async fn execute_cmd(&mut self, _client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
57 if let Some(ref transport) = self.context_config.iggy.transport {
58 validate_transport(transport)?;
59 }
60
61 let mut context_mgr = ContextManager::default();
62
63 context_mgr
64 .create_context(&self.context_name, self.context_config.clone())
65 .await?;
66
67 event!(target: PRINT_TARGET, Level::INFO, "context '{}' created successfully", self.context_name);
68
69 Ok(())
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn should_return_explain_message() {
79 let cmd = CreateContextCmd::new("production".to_string(), ContextConfig::default());
80 assert_eq!(cmd.explain(), "create context production");
81 }
82
83 #[test]
84 fn should_not_require_login() {
85 let cmd = CreateContextCmd::new("test".to_string(), ContextConfig::default());
86 assert!(!cmd.login_required());
87 }
88
89 #[test]
90 fn should_not_require_connection() {
91 let cmd = CreateContextCmd::new("test".to_string(), ContextConfig::default());
92 assert!(!cmd.connection_required());
93 }
94}