iggy_cli/commands/binary_context/
show_context.rs1use anyhow::bail;
20use async_trait::async_trait;
21use comfy_table::Table;
22use tracing::{Level, event};
23
24use crate::commands::cli_command::{CliCommand, PRINT_TARGET};
25use iggy_common::Client;
26
27use super::common::{ContextConfig, ContextManager};
28
29const MASKED_VALUE: &str = "********";
30
31pub struct ShowContextCmd {
32 context_name: String,
33}
34
35impl ShowContextCmd {
36 pub fn new(context_name: String) -> Self {
37 Self { context_name }
38 }
39
40 fn add_opt_str(table: &mut Table, label: &str, value: &Option<String>) {
41 if let Some(ref v) = *value {
42 table.add_row(vec![label, v]);
43 }
44 }
45
46 fn add_opt_bool(table: &mut Table, label: &str, value: Option<bool>) {
47 if let Some(v) = value {
48 table.add_row(vec![label, &v.to_string()]);
49 }
50 }
51
52 fn add_opt_u16(table: &mut Table, label: &str, value: Option<u16>) {
53 if let Some(v) = value {
54 table.add_row(vec![label, &v.to_string()]);
55 }
56 }
57
58 fn add_opt_u32(table: &mut Table, label: &str, value: Option<u32>) {
59 if let Some(v) = value {
60 table.add_row(vec![label, &v.to_string()]);
61 }
62 }
63
64 fn add_opt_u64(table: &mut Table, label: &str, value: Option<u64>) {
65 if let Some(v) = value {
66 table.add_row(vec![label, &v.to_string()]);
67 }
68 }
69
70 fn add_masked(table: &mut Table, label: &str, value: &Option<String>) {
71 if value.is_some() {
72 table.add_row(vec![label, MASKED_VALUE]);
73 }
74 }
75
76 fn build_table(name: &str, is_active: bool, config: &ContextConfig) -> Table {
77 let mut table = Table::new();
78 table.set_header(vec!["Property", "Value"]);
79
80 let display_name = if is_active {
81 format!("{name}*")
82 } else {
83 name.to_string()
84 };
85 table.add_row(vec!["Name", &display_name]);
86
87 let iggy = &config.iggy;
88
89 Self::add_opt_str(&mut table, "Transport", &iggy.transport);
90 Self::add_masked(&mut table, "Encryption Key", &iggy.encryption_key);
91 Self::add_opt_str(
92 &mut table,
93 "Credentials Username",
94 &iggy.credentials_username,
95 );
96 Self::add_masked(
97 &mut table,
98 "Credentials Password",
99 &iggy.credentials_password,
100 );
101
102 Self::add_opt_str(&mut table, "HTTP API URL", &iggy.http_api_url);
103 Self::add_opt_u32(&mut table, "HTTP Retries", iggy.http_retries);
104
105 Self::add_opt_str(&mut table, "TCP Server Address", &iggy.tcp_server_address);
106 Self::add_opt_u32(
107 &mut table,
108 "TCP Reconnection Max Retries",
109 iggy.tcp_reconnection_max_retries,
110 );
111 Self::add_opt_str(
112 &mut table,
113 "TCP Reconnection Interval",
114 &iggy.tcp_reconnection_interval,
115 );
116 Self::add_opt_bool(&mut table, "TCP TLS Enabled", iggy.tcp_tls_enabled);
117 Self::add_opt_str(&mut table, "TCP TLS Domain", &iggy.tcp_tls_domain);
118
119 Self::add_opt_str(&mut table, "QUIC Client Address", &iggy.quic_client_address);
120 Self::add_opt_str(&mut table, "QUIC Server Address", &iggy.quic_server_address);
121 Self::add_opt_str(&mut table, "QUIC Server Name", &iggy.quic_server_name);
122 Self::add_opt_u32(
123 &mut table,
124 "QUIC Reconnection Max Retries",
125 iggy.quic_reconnection_max_retries,
126 );
127 Self::add_opt_str(
128 &mut table,
129 "QUIC Reconnection Interval",
130 &iggy.quic_reconnection_interval,
131 );
132 Self::add_opt_u64(
133 &mut table,
134 "QUIC Max Concurrent Bidi Streams",
135 iggy.quic_max_concurrent_bidi_streams,
136 );
137 Self::add_opt_u64(
138 &mut table,
139 "QUIC Datagram Send Buffer Size",
140 iggy.quic_datagram_send_buffer_size,
141 );
142 Self::add_opt_u16(&mut table, "QUIC Initial MTU", iggy.quic_initial_mtu);
143 Self::add_opt_u64(&mut table, "QUIC Send Window", iggy.quic_send_window);
144 Self::add_opt_u64(&mut table, "QUIC Receive Window", iggy.quic_receive_window);
145 Self::add_opt_u64(
146 &mut table,
147 "QUIC Response Buffer Size",
148 iggy.quic_response_buffer_size,
149 );
150 Self::add_opt_u64(
151 &mut table,
152 "QUIC Keep Alive Interval",
153 iggy.quic_keep_alive_interval,
154 );
155 Self::add_opt_u64(
156 &mut table,
157 "QUIC Max Idle Timeout",
158 iggy.quic_max_idle_timeout,
159 );
160 Self::add_opt_bool(
161 &mut table,
162 "QUIC Validate Certificate",
163 iggy.quic_validate_certificate,
164 );
165
166 Self::add_opt_str(
167 &mut table,
168 "WebSocket Server Address",
169 &iggy.websocket_server_address,
170 );
171 Self::add_opt_u32(
172 &mut table,
173 "WebSocket Reconnection Max Retries",
174 iggy.websocket_reconnection_max_retries,
175 );
176 Self::add_opt_str(
177 &mut table,
178 "WebSocket Reconnection Interval",
179 &iggy.websocket_reconnection_interval,
180 );
181
182 if let Some(ref username) = config.username {
183 table.add_row(vec!["Username", username]);
184 }
185 Self::add_masked(&mut table, "Password", &config.password);
186 Self::add_masked(&mut table, "Token", &config.token);
187 Self::add_opt_str(&mut table, "Token Name", &config.token_name);
188
189 for (key, value) in &config.extra {
190 table.add_row(vec![key.as_str(), &value.to_string()]);
191 }
192
193 table
194 }
195}
196
197#[async_trait]
198impl CliCommand for ShowContextCmd {
199 fn explain(&self) -> String {
200 let context_name = &self.context_name;
201 format!("show context {context_name}")
202 }
203
204 fn login_required(&self) -> bool {
205 false
206 }
207
208 fn connection_required(&self) -> bool {
209 false
210 }
211
212 async fn execute_cmd(&mut self, _client: &dyn Client) -> anyhow::Result<(), anyhow::Error> {
213 let mut context_mgr = ContextManager::default();
214 let contexts_map = context_mgr.get_contexts().await?;
215 let active_context_key = context_mgr.get_active_context_key().await?;
216
217 let config = match contexts_map.get(&self.context_name) {
218 Some(config) => config,
219 None => bail!("context '{}' not found", self.context_name),
220 };
221
222 let is_active = self.context_name == active_context_key;
223 let table = Self::build_table(&self.context_name, is_active, config);
224
225 event!(target: PRINT_TARGET, Level::INFO, "{table}");
226
227 Ok(())
228 }
229}
230
231#[cfg(test)]
232mod tests {
233 use super::*;
234
235 #[test]
236 fn should_return_explain_message() {
237 let cmd = ShowContextCmd::new("production".to_string());
238 assert_eq!(cmd.explain(), "show context production");
239 }
240
241 #[test]
242 fn should_not_require_login() {
243 let cmd = ShowContextCmd::new("test".to_string());
244 assert!(!cmd.login_required());
245 }
246
247 #[test]
248 fn should_not_require_connection() {
249 let cmd = ShowContextCmd::new("test".to_string());
250 assert!(!cmd.connection_required());
251 }
252}