iggy_cli/commands/binary_context/
delete_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::ContextManager;
26
27pub struct DeleteContextCmd {
28 context_name: String,
29}
30
31impl DeleteContextCmd {
32 pub fn new(context_name: String) -> Self {
33 Self { context_name }
34 }
35}
36
37#[async_trait]
38impl CliCommand for DeleteContextCmd {
39 fn explain(&self) -> String {
40 let context_name = &self.context_name;
41 format!("delete context {context_name}")
42 }
43
44 fn login_required(&self) -> bool {
45 false
46 }
47
48 fn connection_required(&self) -> bool {
49 false
50 }
51
52 async fn execute_cmd(&mut self, _client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
53 let mut context_mgr = ContextManager::default();
54
55 context_mgr.delete_context(&self.context_name).await?;
56
57 event!(target: PRINT_TARGET, Level::INFO, "context '{}' deleted successfully", self.context_name);
58
59 Ok(())
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 #[test]
68 fn should_return_explain_message() {
69 let cmd = DeleteContextCmd::new("production".to_string());
70 assert_eq!(cmd.explain(), "delete context production");
71 }
72
73 #[test]
74 fn should_not_require_login() {
75 let cmd = DeleteContextCmd::new("test".to_string());
76 assert!(!cmd.login_required());
77 }
78
79 #[test]
80 fn should_not_require_connection() {
81 let cmd = DeleteContextCmd::new("test".to_string());
82 assert!(!cmd.connection_required());
83 }
84}