|
| 1 | +use anyhow::Context; |
| 2 | +use clap::Subcommand; |
| 3 | + |
| 4 | +use crate::{cli::OutputFormat, connection::ConnectionManager, error::Result as CliResult}; |
| 5 | + |
| 6 | +#[allow(dead_code)] |
| 7 | +pub async fn handle_suffix_command( |
| 8 | + conn_mgr: &ConnectionManager, |
| 9 | + profile_name: Option<&str>, |
| 10 | + suffix_cmd: SuffixCommands, |
| 11 | + output_format: OutputFormat, |
| 12 | + query: Option<&str>, |
| 13 | +) -> CliResult<()> { |
| 14 | + suffix_cmd |
| 15 | + .execute(conn_mgr, profile_name, output_format, query) |
| 16 | + .await |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, Clone, Subcommand)] |
| 20 | +pub enum SuffixCommands { |
| 21 | + /// List all DNS suffixes |
| 22 | + List, |
| 23 | + |
| 24 | + /// Get a specific DNS suffix by name |
| 25 | + Get { |
| 26 | + /// DNS suffix name |
| 27 | + name: String, |
| 28 | + }, |
| 29 | +} |
| 30 | + |
| 31 | +impl SuffixCommands { |
| 32 | + #[allow(dead_code)] |
| 33 | + pub async fn execute( |
| 34 | + &self, |
| 35 | + conn_mgr: &ConnectionManager, |
| 36 | + profile_name: Option<&str>, |
| 37 | + output_format: OutputFormat, |
| 38 | + query: Option<&str>, |
| 39 | + ) -> CliResult<()> { |
| 40 | + handle_suffix_command_impl(conn_mgr, profile_name, self, output_format, query).await |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[allow(dead_code)] |
| 45 | +async fn handle_suffix_command_impl( |
| 46 | + conn_mgr: &ConnectionManager, |
| 47 | + profile_name: Option<&str>, |
| 48 | + command: &SuffixCommands, |
| 49 | + output_format: OutputFormat, |
| 50 | + query: Option<&str>, |
| 51 | +) -> CliResult<()> { |
| 52 | + let client = conn_mgr.create_enterprise_client(profile_name).await?; |
| 53 | + |
| 54 | + match command { |
| 55 | + SuffixCommands::List => { |
| 56 | + let response: serde_json::Value = client |
| 57 | + .get("/v1/suffixes") |
| 58 | + .await |
| 59 | + .context("Failed to list DNS suffixes")?; |
| 60 | + |
| 61 | + let output_data = if let Some(q) = query { |
| 62 | + super::utils::apply_jmespath(&response, q)? |
| 63 | + } else { |
| 64 | + response |
| 65 | + }; |
| 66 | + |
| 67 | + super::utils::print_formatted_output(output_data, output_format)?; |
| 68 | + } |
| 69 | + SuffixCommands::Get { name } => { |
| 70 | + let response: serde_json::Value = client |
| 71 | + .get(&format!("/v1/suffix/{}", name)) |
| 72 | + .await |
| 73 | + .context(format!("Failed to get DNS suffix '{}'", name))?; |
| 74 | + |
| 75 | + let output_data = if let Some(q) = query { |
| 76 | + super::utils::apply_jmespath(&response, q)? |
| 77 | + } else { |
| 78 | + response |
| 79 | + }; |
| 80 | + |
| 81 | + super::utils::print_formatted_output(output_data, output_format)?; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + Ok(()) |
| 86 | +} |
| 87 | + |
| 88 | +#[cfg(test)] |
| 89 | +mod tests { |
| 90 | + use super::*; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn test_suffix_command_parsing() { |
| 94 | + use clap::Parser; |
| 95 | + |
| 96 | + #[derive(Parser)] |
| 97 | + struct TestCli { |
| 98 | + #[command(subcommand)] |
| 99 | + cmd: SuffixCommands, |
| 100 | + } |
| 101 | + |
| 102 | + // Test list command |
| 103 | + let cli = TestCli::parse_from(["test", "list"]); |
| 104 | + assert!(matches!(cli.cmd, SuffixCommands::List)); |
| 105 | + |
| 106 | + // Test get command |
| 107 | + let cli = TestCli::parse_from(["test", "get", "example.redis.local"]); |
| 108 | + if let SuffixCommands::Get { name } = cli.cmd { |
| 109 | + assert_eq!(name, "example.redis.local"); |
| 110 | + } else { |
| 111 | + panic!("Expected Get command"); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments