|
| 1 | +use crate::cli::OutputFormat; |
| 2 | +use crate::connection::ConnectionManager; |
| 3 | +use crate::error::{RedisCtlError, Result as CliResult}; |
| 4 | +use clap::Subcommand; |
| 5 | + |
| 6 | +#[derive(Debug, Clone, Subcommand)] |
| 7 | +pub enum LocalCommands { |
| 8 | + /// Get local node master healthcheck |
| 9 | + #[command(name = "healthcheck")] |
| 10 | + MasterHealthcheck, |
| 11 | + |
| 12 | + /// List local services |
| 13 | + Services, |
| 14 | + |
| 15 | + /// Update local services configuration |
| 16 | + #[command(name = "services-update")] |
| 17 | + ServicesUpdate { |
| 18 | + /// Service configuration as JSON string or @file.json |
| 19 | + #[arg(long)] |
| 20 | + data: String, |
| 21 | + }, |
| 22 | +} |
| 23 | + |
| 24 | +impl LocalCommands { |
| 25 | + #[allow(dead_code)] |
| 26 | + pub async fn execute( |
| 27 | + &self, |
| 28 | + conn_mgr: &ConnectionManager, |
| 29 | + profile_name: Option<&str>, |
| 30 | + output_format: OutputFormat, |
| 31 | + query: Option<&str>, |
| 32 | + ) -> CliResult<()> { |
| 33 | + let client = conn_mgr.create_enterprise_client(profile_name).await?; |
| 34 | + |
| 35 | + match self { |
| 36 | + LocalCommands::MasterHealthcheck => { |
| 37 | + let response: serde_json::Value = client |
| 38 | + .get("/v1/local/node/master_healthcheck") |
| 39 | + .await |
| 40 | + .map_err(RedisCtlError::from)?; |
| 41 | + |
| 42 | + let output_data = if let Some(q) = query { |
| 43 | + super::utils::apply_jmespath(&response, q)? |
| 44 | + } else { |
| 45 | + response |
| 46 | + }; |
| 47 | + super::utils::print_formatted_output(output_data, output_format)?; |
| 48 | + } |
| 49 | + |
| 50 | + LocalCommands::Services => { |
| 51 | + let response: serde_json::Value = client |
| 52 | + .get("/v1/local/services") |
| 53 | + .await |
| 54 | + .map_err(RedisCtlError::from)?; |
| 55 | + |
| 56 | + let output_data = if let Some(q) = query { |
| 57 | + super::utils::apply_jmespath(&response, q)? |
| 58 | + } else { |
| 59 | + response |
| 60 | + }; |
| 61 | + super::utils::print_formatted_output(output_data, output_format)?; |
| 62 | + } |
| 63 | + |
| 64 | + LocalCommands::ServicesUpdate { data } => { |
| 65 | + let json_data = super::utils::read_json_data(data)?; |
| 66 | + |
| 67 | + let response: serde_json::Value = client |
| 68 | + .post("/v1/local/services", &json_data) |
| 69 | + .await |
| 70 | + .map_err(RedisCtlError::from)?; |
| 71 | + |
| 72 | + let output_data = if let Some(q) = query { |
| 73 | + super::utils::apply_jmespath(&response, q)? |
| 74 | + } else { |
| 75 | + response |
| 76 | + }; |
| 77 | + super::utils::print_formatted_output(output_data, output_format)?; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + Ok(()) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[allow(dead_code)] |
| 86 | +pub async fn handle_local_command( |
| 87 | + conn_mgr: &ConnectionManager, |
| 88 | + profile_name: Option<&str>, |
| 89 | + local_cmd: LocalCommands, |
| 90 | + output_format: OutputFormat, |
| 91 | + query: Option<&str>, |
| 92 | +) -> CliResult<()> { |
| 93 | + local_cmd |
| 94 | + .execute(conn_mgr, profile_name, output_format, query) |
| 95 | + .await |
| 96 | +} |
0 commit comments