|
| 1 | +// daemon_api/src/help_generator.rs |
| 2 | + |
| 3 | +// This module provides functions for generating CLI help messages programmatically, |
| 4 | +// leveraging clap's capabilities. |
| 5 | + |
| 6 | +use clap::CommandFactory; |
| 7 | +use crate::cli_schema::CliArgs; |
| 8 | + |
| 9 | +/// Generates the full, auto-generated help message for the CLI. |
| 10 | +pub fn generate_full_help() -> String { |
| 11 | + let mut cmd = CliArgs::command(); // FIX: Use CliArgs::command() directly |
| 12 | + let mut buffer = Vec::new(); |
| 13 | + cmd.write_help(&mut buffer).expect("Failed to write help to buffer"); |
| 14 | + String::from_utf8(buffer).expect("Failed to convert help to UTF-8") |
| 15 | +} |
| 16 | + |
| 17 | +/// Generates a filtered help message based on a command path. |
| 18 | +/// |
| 19 | +/// This function attempts to get the help for a specific subcommand path. |
| 20 | +/// If the path is empty, it returns the full help. |
| 21 | +/// If the path is invalid or no specific help is found, it activates the error case. |
| 22 | +pub fn generate_help_for_path(command_path: &[String]) -> String { |
| 23 | + if command_path.is_empty() { |
| 24 | + return generate_full_help(); |
| 25 | + } |
| 26 | + |
| 27 | + let mut cmd = CliArgs::command(); // FIX: Use CliArgs::command() directly |
| 28 | + let mut current_subcommand = Some(&mut cmd); |
| 29 | + let mut full_path_str = String::new(); |
| 30 | + |
| 31 | + // Traverse the command structure to find the specific subcommand |
| 32 | + for (i, segment) in command_path.iter().enumerate() { |
| 33 | + if i > 0 { |
| 34 | + full_path_str.push(' '); |
| 35 | + } |
| 36 | + full_path_str.push_str(segment); |
| 37 | + |
| 38 | + if let Some(sub) = current_subcommand.take().and_then(|c| c.find_subcommand_mut(segment)) { |
| 39 | + current_subcommand = Some(sub); |
| 40 | + } else { |
| 41 | + // If a segment is not found as a subcommand, return an error message |
| 42 | + return format!( |
| 43 | + "Error: Command path '{}' not found. Displaying general help.\n\n{}", |
| 44 | + full_path_str, |
| 45 | + generate_full_help() |
| 46 | + ); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if let Some(final_cmd) = current_subcommand { |
| 51 | + let mut buffer = Vec::new(); |
| 52 | + final_cmd.write_help(&mut buffer).expect("Failed to write filtered help to buffer"); |
| 53 | + String::from_utf8(buffer).expect("Failed to convert filtered help to UTF-8") |
| 54 | + } else { |
| 55 | + // This case should ideally not be reached if the loop logic is correct, |
| 56 | + // but it's a fallback. |
| 57 | + format!( |
| 58 | + "Error: Could not retrieve help for '{}'. Displaying general help.\n\n{}", |
| 59 | + full_path_str, |
| 60 | + generate_full_help() |
| 61 | + ) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// Generates status information for a given command path. |
| 66 | +/// This is a mock/placeholder for now, as actual status checks involve network calls. |
| 67 | +/// In a real scenario, this would likely interact with daemon_api or other health endpoints. |
| 68 | +pub async fn generate_status_for_path(command_path: &[String]) -> String { |
| 69 | + let mut response_lines = vec![]; |
| 70 | + response_lines.push(format!("--- Status for: {} ---", command_path.join(" "))); |
| 71 | + |
| 72 | + // Corrected matching on &[String] directly |
| 73 | + match command_path { |
| 74 | + [] => { |
| 75 | + // Full status summary |
| 76 | + response_lines.push("Comprehensive system status:".to_string()); |
| 77 | + response_lines.push(" GraphDB Daemon: Checking...".to_string()); |
| 78 | + response_lines.push(" REST API: Checking...".to_string()); |
| 79 | + response_lines.push(" Storage Daemon: Checking...".to_string()); |
| 80 | + response_lines.push("\nNote: For detailed status, use CLI 'status <component>' or REST '/api/v1/status/<component>'".to_string()); |
| 81 | + } |
| 82 | + // FIX: Removed 'ref' binding modifier |
| 83 | + [rest_str] if rest_str == "rest" => { |
| 84 | + response_lines.push("Checking REST API status...".to_string()); |
| 85 | + // In a real implementation, make an HTTP call to the REST API's /health endpoint |
| 86 | + // For now, a placeholder: |
| 87 | + response_lines.push(" REST API: Mock Status - Running".to_string()); |
| 88 | + } |
| 89 | + // FIX: Removed 'ref' binding modifier |
| 90 | + [daemon_str] if daemon_str == "daemon" => { |
| 91 | + response_lines.push("Checking GraphDB Daemon status (all common ports)...".to_string()); |
| 92 | + // Placeholder for checking multiple daemon ports |
| 93 | + response_lines.push(" Daemon (8080): Mock Status - Running".to_string()); |
| 94 | + response_lines.push(" Daemon (9001): Mock Status - Down".to_string()); |
| 95 | + } |
| 96 | + // FIX: Removed 'ref' binding modifier |
| 97 | + [daemon_str, port_str] if daemon_str == "daemon" => { |
| 98 | + if let Ok(port) = port_str.parse::<u16>() { |
| 99 | + response_lines.push(format!("Checking GraphDB Daemon status on port {}...", port)); |
| 100 | + // Placeholder for checking a specific daemon port |
| 101 | + response_lines.push(format!(" Daemon ({}): Mock Status - Running", port)); |
| 102 | + } else { |
| 103 | + response_lines.push(format!("Invalid port specified: {}", port_str)); |
| 104 | + } |
| 105 | + } |
| 106 | + // FIX: Removed 'ref' binding modifier |
| 107 | + [storage_str] if storage_str == "storage" => { |
| 108 | + response_lines.push("Checking Storage Daemon status...".to_string()); |
| 109 | + // Placeholder for checking storage daemon status |
| 110 | + response_lines.push(" Storage Daemon: Mock Status - Running (Sled)".to_string()); |
| 111 | + } |
| 112 | + // FIX: Removed 'ref' binding modifier |
| 113 | + [storage_str, port_str] if storage_str == "storage" => { |
| 114 | + if let Ok(port) = port_str.parse::<u16>() { |
| 115 | + response_lines.push(format!("Checking Storage Daemon status on port {}...", port)); |
| 116 | + // Placeholder for checking a specific storage daemon port |
| 117 | + response_lines.push(format!(" Storage Daemon ({}): Mock Status - Running (RocksDB)", port)); |
| 118 | + } else { |
| 119 | + response_lines.push(format!("Invalid port specified: {}", port_str)); |
| 120 | + } |
| 121 | + } |
| 122 | + _ => { |
| 123 | + response_lines.push(format!("Unknown status request: {:?}. Displaying general status summary.", command_path)); |
| 124 | + response_lines.push(" GraphDB Daemon: Checking...".to_string()); |
| 125 | + response_lines.push(" REST API: Checking...".to_string()); |
| 126 | + response_lines.push(" Storage Daemon: Checking...".to_string()); |
| 127 | + } |
| 128 | + } |
| 129 | + response_lines.join("\n") |
| 130 | +} |
| 131 | + |
0 commit comments