diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 8db1eb72..8f86c7de 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -104,7 +104,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -115,7 +115,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -596,9 +596,9 @@ checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" [[package]] name = "cli-engine" -version = "0.10.4" +version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea2e69f391a1d3c2a4fdf93fda9a84c42a00fd199baf3346d084a40b9e3162d2" +checksum = "0976976d62d9aadfe680bd5a38f9102a7b6b4c6deb5046343768a1e2f7a4ab0f" dependencies = [ "async-trait", "base64", @@ -1024,7 +1024,7 @@ dependencies = [ "libc", "option-ext", "redox_users", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1197,7 +1197,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -2555,7 +2555,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3277,7 +3277,7 @@ dependencies = [ "once_cell", "socket2", "tracing", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3619,7 +3619,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3677,7 +3677,7 @@ dependencies = [ "security-framework 3.7.0", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -4187,7 +4187,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -4338,7 +4338,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -4804,7 +4804,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ "memoffset", "tempfile", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -5086,7 +5086,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 27bf745f..c268e0d7 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -29,7 +29,7 @@ async-trait = "0.1" bytes = "1" chrono = { version = "0.4", default-features = false, features = ["clock", "serde"] } clap = { version = "4.5", features = ["std", "string", "derive", "env"] } -cli-engine = { version = "0.10.4", features = ["pkce-auth"] } +cli-engine = { version = "0.10.6", features = ["pkce-auth"] } dirs = "6" domains-client = { path = "domains-client" } email-client = { path = "email-client" } diff --git a/rust/src/dns/delete.rs b/rust/src/dns/delete.rs index 12dfb948..27876f68 100644 --- a/rust/src/dns/delete.rs +++ b/rust/src/dns/delete.rs @@ -11,7 +11,8 @@ use crate::scopes::DOMAINS_DNS_UPDATE; use domains_client::types; use super::records::{ - ZONE_AND_NAME, fetch_records, parse_write_type_arg, record_value, verified_result, + ZONE_AND_NAME, detail_columns, detail_values, fetch_records, merged_tlsa_data, + parse_write_type_arg, record_value, verified_result, }; output_schema!(DnsDeleteResult { @@ -66,6 +67,19 @@ fn summarize_delete_outcomes( })) } +/// How a record is named in a delete's preview and in its per-record results: its +/// value, except for TLSA, whose value alone is just the certificate — two records +/// that differ only in usage, selector or matching type would otherwise look +/// identical, and a delete by name removes every one of them. TLSA is shown the +/// way `dns list` shows it (`usage selector matching-type certificate`). +fn record_label_value(rec: &types::DnsRecord) -> Option { + if rec.type_.as_str() == "TLSA" { + Some(merged_tlsa_data(rec)).filter(|text| !text.is_empty()) + } else { + record_value(rec).map(str::to_owned) + } +} + /// Builds the `dns delete --dry-run` preview from the same matched records the /// real execution would delete, so the preview can never drift from what /// `delete` would actually do. @@ -88,7 +102,13 @@ fn dry_run_delete_preview( } else { "would fail (missing recordId)" }; - json!({"recordId": rec.record_id, "data": record_value(rec), "status": status}) + // The type-specific fields say which of several records under one + // name (an SRV's services, say) would go. + let mut row = detail_values(rec); + row.insert("recordId".to_owned(), json!(rec.record_id)); + row.insert("data".to_owned(), json!(record_label_value(rec))); + row.insert("status".to_owned(), json!(status)); + Value::Object(row) }) .collect(); @@ -144,11 +164,17 @@ fn view_columns() -> Vec { TableColumn::new("deleted", "Deleted").align(Alignment::Right), TableColumn::new("failed", "Failed").align(Alignment::Right), TableColumn::new("action", "Action"), - TableColumn::new("records", "Records").nested(vec![ - TableColumn::new("recordId", "Record ID"), - TableColumn::new("data", "Data").essential(true), - TableColumn::new("status", "Status").essential(true), - ]), + TableColumn::new("records", "Records") + .hide_when_empty(true) + .nested({ + let mut columns = vec![ + TableColumn::new("data", "Data").essential(true), + TableColumn::new("status", "Status").essential(true), + ]; + columns.extend(detail_columns()); + columns.push(TableColumn::new("recordId", "Record ID")); + columns + }), ] } @@ -220,7 +246,10 @@ pub(super) fn command() -> RuntimeCommandSpec { } }, }; - outcomes.push((record_value(rec).unwrap_or("(no data)").to_string(), err)); + outcomes.push(( + record_label_value(rec).unwrap_or_else(|| "(no data)".to_owned()), + err, + )); } summarize_delete_outcomes(&domain, &record_type, &name, &outcomes) @@ -323,12 +352,48 @@ mod tests { #[test] fn dry_run_delete_preview_shows_tlsa_certificate_data_as_the_value() { let cert = "d2abde240d7cd3ee6b4b28c54df034b97983a1d16e8a410e4561cb106618e971"; + // No numeric fields: it falls back to the certificate alone, not null. let existing = vec![test_tlsa_record("r1", cert)]; let preview = dry_run_delete_preview("example.com", "TLSA", "www", &existing); let records = preview["records"].as_array().expect("records array"); assert_eq!(records[0]["data"], cert); } + /// Two TLSA records with the same certificate, port and protocol that differ + /// only in usage are different records, and `delete --type TLSA --name www` + /// removes both: the preview has to show which is which. + #[test] + fn dry_run_delete_preview_tells_tlsa_records_apart_by_usage_selector_and_matching_type() { + let cert = "d2abde240d7cd3ee6b4b28c54df034b97983a1d16e8a410e4561cb106618e971"; + let tlsa = |id: &str, usage: u8, selector: u8, matching: u8| types::DnsRecord { + usage: Some(types::TlsaUsage(usage)), + selector: Some(types::TlsaSelector(selector)), + matching_type: Some(types::TlsaMatchingType(matching)), + port: Some(443), + protocol: Some("_tcp".to_owned()), + ..test_tlsa_record(id, cert) + }; + let existing = vec![tlsa("r1", 3, 1, 1), tlsa("r2", 1, 0, 2)]; + + let preview = dry_run_delete_preview("example.com", "TLSA", "www", &existing); + + let records = preview["records"].as_array().expect("records array"); + assert_eq!(records[0]["data"], format!("3 1 1 {cert}")); + assert_eq!(records[1]["data"], format!("1 0 2 {cert}")); + assert_ne!(records[0]["data"], records[1]["data"]); + } + + #[test] + fn a_record_that_is_not_tlsa_is_labelled_by_its_plain_value() { + let a = test_record("r1", "1.2.3.4"); + assert_eq!(record_label_value(&a).as_deref(), Some("1.2.3.4")); + let none = types::DnsRecord { + data: None, + ..test_record("r2", "") + }; + assert_eq!(record_label_value(&none), None); + } + /// A record without a recordId can't actually be deleted (the real /// handler reports it as a failure) — the preview must not claim it as /// a delete either. @@ -381,6 +446,64 @@ mod tests { } } + fn test_srv_record(record_id: &str, service: &str, port: u16) -> types::DnsRecord { + types::DnsRecord { + data: Some("chat-server.example.com.".to_owned()), + name: "chat".to_owned(), + port: Some(port), + priority: Some(10), + weight: Some(5), + protocol: Some("_tcp".to_owned()), + service: Some(service.to_owned()), + type_: types::DnsRecordType("SRV".to_owned()), + ..test_record(record_id, "") + } + } + + /// Deleting `--type SRV --name chat` removes every service under it, so the + /// preview has to say which services those are: otherwise the rows are + /// identical and the customer cannot see what is about to go. + #[test] + fn dry_run_delete_preview_tells_srv_records_apart() { + let existing = vec![ + test_srv_record("r1", "_sip", 5060), + test_srv_record("r2", "_xmpp", 5222), + ]; + let preview = dry_run_delete_preview("example.com", "SRV", "chat", &existing); + + let records = preview["records"].as_array().expect("records array"); + assert_eq!(records[0]["service"], "_sip"); + assert_eq!(records[1]["port"], 5222); + + let rendered = cli_engine::preview_human_view(preview, &view_columns()); + assert!( + rendered.contains("SERVICE") && rendered.contains("PORT"), + "{rendered}" + ); + assert!( + rendered.contains("_sip") && rendered.contains("_xmpp"), + "{rendered}" + ); + } + + #[test] + fn dry_run_delete_preview_adds_no_columns_for_plain_records() { + let existing = vec![test_record("r1", "1.2.3.4"), test_record("r2", "5.6.7.8")]; + let preview = dry_run_delete_preview("example.com", "A", "www", &existing); + + let rendered = cli_engine::preview_human_view(preview, &view_columns()); + + for sparse in [ + "SERVICE", "PORT", "PRIORITY", "PROTOCOL", "WEIGHT", "TAG", "FLAG", + ] { + assert!(!rendered.contains(sparse), "{sparse} shown: {rendered}"); + } + assert!( + rendered.contains("1.2.3.4") && rendered.contains("would delete"), + "{rendered}" + ); + } + /// Proves `view_columns()`'s field names actually match what /// `dry_run_delete_preview` emits — a mismatch here would silently drop /// the record breakdown from human output the same way the `api diff --git a/rust/src/dns/guides/dns.md b/rust/src/dns/guides/dns.md index 71735c84..01c6abd4 100644 --- a/rust/src/dns/guides/dns.md +++ b/rust/src/dns/guides/dns.md @@ -83,7 +83,7 @@ Some record types need extra flags: | `TLSA` | `--usage`, `--selector`, `--matching-type`, `--port`, `--protocol` | | | `CAA` | `--tag` (`issue`, `issuewild` or `iodef`); `--data` carries the CA domain or value | `--flag` (defaults to `0`) | -`--service` and `--protocol` take the plain name, e.g. `--service sip --protocol tcp`. GoDaddy stores them with a leading underscore (`_sip._tcp`), which `gddy` adds for you; `--service _sip` works too. Any name made of letters, digits, `-` and `_` (up to 62 characters before the underscore) is accepted, so custom services work. Where they are shown (`--fields service,protocol`, or JSON), they appear in the underscore form, since that is the DNS name. +`--service` and `--protocol` take the plain name, e.g. `--service sip --protocol tcp`. GoDaddy stores them with a leading underscore (`_sip._tcp`), which `gddy` adds for you; `--service _sip` works too. Any name made of letters, digits, `-` and `_` (up to 62 characters before the underscore) is accepted, so custom services work. Listings show the underscore form, since that is the DNS name. Example SRV record: diff --git a/rust/src/dns/list.rs b/rust/src/dns/list.rs index c1e20991..1eb2786d 100644 --- a/rust/src/dns/list.rs +++ b/rust/src/dns/list.rs @@ -12,7 +12,7 @@ use crate::scopes::DOMAINS_READ; use domains_client::types; -use super::records::{fetch_records, merged_tlsa_data, parse_list_type_arg}; +use super::records::{detail_columns, fetch_records, merged_tlsa_data, parse_list_type_arg}; #[derive(Debug, Clone, clap::Args)] struct ListArgs { @@ -33,29 +33,36 @@ struct ListArgs { domain_format: DomainFormArgs, } +/// The columns `dns list` shows when `--fields` is not given, in display order. +/// Everything after `ttl` is specific to some record types and only appears when +/// a listed record has it (see `view_columns`). The TLSA fields are left out +/// because `data` already carries them as `usage selector matching-type cert`. +const DEFAULT_FIELDS: &str = + "type,name,data,ttl,priority,weight,port,service,protocol,flag,tag,parameters"; + /// A record is unreadable without its type/name/data, so those three are /// `essential` — never hidden or shrunk for width, even on a narrow terminal /// — while the rest are nice-to-haves that still yield to width pressure. fn view_columns() -> Vec { - vec![ + // The record-specific columns are only worth a column when some record uses + // them, so a listing of A and TXT records looks as it always did while SRV, + // MX, CAA and TLSA records bring the fields that tell them apart. + let sparse = |column: TableColumn| column.hide_when_empty(true); + let mut columns = vec![ TableColumn::new("type", "Type").essential(true), TableColumn::new("name", "Name").essential(true), TableColumn::new("data", "Data").essential(true), TableColumn::new("ttl", "TTL").align(Alignment::Right), - TableColumn::new("priority", "Priority").align(Alignment::Right), + ]; + columns.extend(detail_columns()); + columns.extend([ TableColumn::new("recordId", "Record ID"), - TableColumn::new("service", "Service"), - TableColumn::new("protocol", "Protocol"), - TableColumn::new("port", "Port").align(Alignment::Right), - TableColumn::new("weight", "Weight").align(Alignment::Right), - TableColumn::new("tag", "Tag"), - TableColumn::new("flag", "Flag").align(Alignment::Right), - TableColumn::new("usage", "Usage"), - TableColumn::new("selector", "Selector"), - TableColumn::new("matchingType", "Matching Type"), - TableColumn::new("certificateData", "Certificate Data").no_truncate(true), - TableColumn::new("parameters", "Parameters"), - ] + sparse(TableColumn::new("usage", "Usage")), + sparse(TableColumn::new("selector", "Selector")), + sparse(TableColumn::new("matchingType", "Matching Type")), + sparse(TableColumn::new("certificateData", "Certificate Data").no_truncate(true)), + ]); + columns } pub(super) fn command() -> RuntimeCommandSpec { @@ -68,7 +75,7 @@ pub(super) fn command() -> RuntimeCommandSpec { ) .with_system("domain") .with_tier(Tier::Read) - .with_default_fields("type,name,data,ttl") + .with_default_fields(DEFAULT_FIELDS) .with_json_schema::() .with_view(view_columns()) .with_scopes(&[DOMAINS_READ]) @@ -137,6 +144,31 @@ mod tests { ); } + fn srv_record(id: &str, service: &str, port: u16) -> types::DnsRecord { + types::DnsRecord { + data: Some("chat-server.example.com.".to_string()), + name: "chat".to_string(), + port: Some(port), + priority: Some(10), + weight: Some(5), + protocol: Some("_tcp".to_string()), + service: Some(service.to_string()), + record_id: Some(id.to_string()), + type_: types::DnsRecordType("SRV".to_string()), + ..a_record() + } + } + + /// The default fields as the engine applies them: the view's columns + /// narrowed to `DEFAULT_FIELDS`, in that order. + fn default_columns() -> Vec { + let all = view_columns(); + DEFAULT_FIELDS + .split(',') + .filter_map(|field| all.iter().find(|c| c.field == field).cloned()) + .collect() + } + /// `view_columns()` declares 17 columns — far more than fit an /// 80-column terminal (the fixed width non-TTY test runs get) once /// their headers are laid out side by side. Type/name/data must survive @@ -144,7 +176,9 @@ mod tests { /// everything else is free to be hidden. #[test] fn essential_fields_survive_width_based_hiding() { - let out = build_list_output(&[a_record()]).expect("serializes"); + // Records that use every optional column, so the table is wider than + // 80 characters and the engine has to decide what to hide. + let out = build_list_output(&[srv_record("r1", "_xmpp", 5222)]).expect("serializes"); let rendered = cli_engine::preview_human_view(out, &view_columns()); @@ -158,6 +192,86 @@ mod tests { ); } + #[test] + fn a_listing_without_srv_or_tlsa_records_has_no_extra_columns() { + let out = build_list_output(&[a_record()]).expect("serializes"); + + let rendered = cli_engine::preview_human_view(out, &default_columns()); + + let header_line = rendered.lines().next().expect("header line"); + for shown in ["TYPE", "NAME", "DATA", "TTL"] { + assert!(header_line.contains(shown), "{rendered}"); + } + for sparse in [ + "PORT", "SERVICE", "PROTOCOL", "PRIORITY", "WEIGHT", "TAG", "FLAG", + ] { + assert!(!header_line.contains(sparse), "{sparse} shown: {rendered}"); + } + } + + #[test] + fn srv_records_under_one_name_are_told_apart_by_service_and_port() { + let out = build_list_output(&[ + srv_record("r1", "_sip", 5060), + srv_record("r2", "_xmpp", 5222), + ]) + .expect("serializes"); + + let rendered = cli_engine::preview_human_view(out, &default_columns()); + + // At 80 columns the trailing protocol column is the one hidden for + // width; the rest are what tells the two rows apart. + for shown in ["PORT", "SERVICE", "PRIORITY", "WEIGHT"] { + assert!(rendered.contains(shown), "{shown} missing: {rendered}"); + } + assert!( + rendered.contains("_sip") && rendered.contains("5060"), + "{rendered}" + ); + assert!( + rendered.contains("_xmpp") && rendered.contains("5222"), + "{rendered}" + ); + } + + #[test] + fn a_mixed_listing_shows_the_extra_columns_with_blanks_on_other_rows() { + let out = + build_list_output(&[a_record(), srv_record("r2", "_xmpp", 5222)]).expect("serializes"); + + let rendered = cli_engine::preview_human_view(out, &default_columns()); + + assert!( + rendered.contains("SERVICE") && rendered.contains("_xmpp"), + "{rendered}" + ); + let a_row = rendered + .lines() + .find(|l| l.contains("1.2.3.4")) + .expect("A row"); + assert!( + !a_row.contains("_xmpp") && !a_row.contains("5222"), + "{a_row}" + ); + } + + #[test] + fn the_default_fields_are_the_core_columns_then_the_shared_detail_fields() { + let expected = format!( + "type,name,data,ttl,{}", + super::super::records::DETAIL_FIELDS.join(",") + ); + assert_eq!(DEFAULT_FIELDS, expected); + } + + #[test] + fn every_default_field_is_a_declared_column() { + let declared: Vec = view_columns().into_iter().map(|c| c.field).collect(); + for field in DEFAULT_FIELDS.split(',') { + assert!(declared.iter().any(|d| d == field), "{field} has no column"); + } + } + fn a_record() -> types::DnsRecord { types::DnsRecord { certificate_data: None, diff --git a/rust/src/dns/records.rs b/rust/src/dns/records.rs index 81d575b8..37c0be19 100644 --- a/rust/src/dns/records.rs +++ b/rust/src/dns/records.rs @@ -2,7 +2,9 @@ //! subcommand: type/name validation, the `RecordOptions` bundle, converting //! CLI input into v3 `DnsRecord`s, and the paginated `list_dns_records` fetch. -use cli_engine::{CliCoreError, CommandResult, NextAction, NextActionParam}; +use cli_engine::{ + Alignment, CliCoreError, CommandResult, NextAction, NextActionParam, TableColumn, +}; use serde_json::Value; use crate::domain::api_error; @@ -463,6 +465,61 @@ pub(super) fn merged_tlsa_data(rec: &types::DnsRecord) -> String { } } +/// The fields that tell apart records of one type at one name (an SRV's service, +/// port and priority; an MX's priority; a CAA's flag and tag; an HTTPS record's +/// parameters). TLSA's four fields are left out because [`merged_tlsa_data`] +/// already folds them into `data`. +pub(super) const DETAIL_FIELDS: &[&str] = &[ + "priority", + "weight", + "port", + "service", + "protocol", + "flag", + "tag", + "parameters", +]; + +/// Table columns for [`DETAIL_FIELDS`], each shown only when some listed record +/// has a value for it, so a table of A and TXT records is unchanged while SRV, +/// MX and CAA records bring the fields that distinguish them. +pub(super) fn detail_columns() -> Vec { + let numeric = ["priority", "weight", "port", "flag"]; + DETAIL_FIELDS + .iter() + .map(|field| { + let header = match *field { + "priority" => "Priority", + "weight" => "Weight", + "port" => "Port", + "service" => "Service", + "protocol" => "Protocol", + "flag" => "Flag", + "tag" => "Tag", + _ => "Parameters", + }; + let column = TableColumn::new(*field, header).hide_when_empty(true); + if numeric.contains(field) { + column.align(Alignment::Right) + } else { + column + } + }) + .collect() +} + +/// The [`DETAIL_FIELDS`] a record actually has, as JSON members to merge into a +/// row of output. +pub(super) fn detail_values(rec: &types::DnsRecord) -> serde_json::Map { + let Ok(Value::Object(all)) = serde_json::to_value(rec) else { + return serde_json::Map::new(); + }; + DETAIL_FIELDS + .iter() + .filter_map(|field| all.get(*field).map(|v| ((*field).to_owned(), v.clone()))) + .collect() +} + /// Whether two v3 records have the same content — every field except `ttl` /// and `recordId`, neither of which makes two records a different *value* /// (a TTL-only change was never treated as a new value, and `recordId` only diff --git a/rust/src/dns/records_tests.rs b/rust/src/dns/records_tests.rs index d2bb2ab2..181478f4 100644 --- a/rust/src/dns/records_tests.rs +++ b/rust/src/dns/records_tests.rs @@ -910,3 +910,35 @@ fn same_content_compares_every_field_except_ttl_and_record_id() { let desired_caa = v3_record("@", "CAA", "letsencrypt.org", &caa_opts_wild); assert!(!same_content(&existing_caa, &desired_caa)); } + +#[test] +fn the_detail_columns_cover_exactly_the_detail_fields_and_all_hide_when_empty() { + let columns = detail_columns(); + let fields: Vec<&str> = columns.iter().map(|c| c.field.as_str()).collect(); + assert_eq!(fields, DETAIL_FIELDS); + assert!( + columns.iter().all(|c| c.hide_when_empty), + "every detail column is sparse" + ); +} + +#[test] +fn detail_values_carry_only_the_fields_a_record_has() { + let a = v3_record("www", "A", "1.2.3.4", &opts()); + assert!( + detail_values(&a).is_empty(), + "an A record has no detail fields" + ); + + let mut srv_opts = opts(); + srv_opts.priority = Some(10); + srv_opts.weight = Some(5); + srv_opts.port = Some(5222); + srv_opts.service = Some("_xmpp".to_owned()); + srv_opts.protocol = Some("_tcp".to_owned()); + let values = detail_values(&v3_record("chat", "SRV", "t.example.com.", &srv_opts)); + assert_eq!(values["service"], "_xmpp"); + assert_eq!(values["port"], 5222); + assert_eq!(values["priority"], 10); + assert!(!values.contains_key("tag") && !values.contains_key("data")); +} diff --git a/rust/src/dns/set/mod.rs b/rust/src/dns/set/mod.rs index ce9f1d2a..1655b45f 100644 --- a/rust/src/dns/set/mod.rs +++ b/rust/src/dns/set/mod.rs @@ -9,7 +9,7 @@ use crate::scopes::DOMAINS_DNS_UPDATE; use super::list::build_list_output; use super::records::{ - RecordOptions, RecordWriteArgs, fetch_records, v3_records, validate_caa_fields, + RecordOptions, RecordWriteArgs, detail_columns, fetch_records, v3_records, validate_caa_fields, validate_record_name, validate_svcb_fields, validate_tlsa_fields, }; use super::records::{ZONE_AND_NAME, verified_result}; @@ -68,18 +68,26 @@ fn view_columns() -> Vec { TableColumn::new("created", "Created").align(Alignment::Right), TableColumn::new("deleted", "Deleted").align(Alignment::Right), TableColumn::new("action", "Action"), - TableColumn::new("records", "Records").nested(vec![ - TableColumn::new("type", "Type").essential(true), - TableColumn::new("name", "Name"), - TableColumn::new("data", "Data").essential(true), - TableColumn::new("ttl", "TTL").align(Alignment::Right), - TableColumn::new("recordId", "Record ID"), - ]), - TableColumn::new("plan", "Plan").nested(vec![ - TableColumn::new("action", "Action").essential(true), - TableColumn::new("recordId", "Record ID"), - TableColumn::new("data", "Data").essential(true), - ]), + TableColumn::new("records", "Records") + .hide_when_empty(true) + .nested({ + let mut columns = vec![ + TableColumn::new("type", "Type").essential(true), + TableColumn::new("name", "Name"), + TableColumn::new("data", "Data").essential(true), + TableColumn::new("ttl", "TTL").align(Alignment::Right), + ]; + columns.extend(detail_columns()); + columns.push(TableColumn::new("recordId", "Record ID")); + columns + }), + TableColumn::new("plan", "Plan") + .hide_when_empty(true) + .nested(vec![ + TableColumn::new("action", "Action").essential(true), + TableColumn::new("recordId", "Record ID"), + TableColumn::new("data", "Data").essential(true), + ]), ] } diff --git a/rust/src/dns/set/outcome.rs b/rust/src/dns/set/outcome.rs index 477397bf..523afc80 100644 --- a/rust/src/dns/set/outcome.rs +++ b/rust/src/dns/set/outcome.rs @@ -257,6 +257,48 @@ mod tests { } } + /// The result of a `set` lists the records now in place; for SRV they have + /// to be told apart by service and port, not just by their identical data. + #[test] + fn the_set_result_shows_srv_service_and_port_and_nothing_extra_for_a_records() { + let columns = super::super::view_columns(); + let srv = |service: &str, port: u64| { + serde_json::json!({ + "type": "SRV", "name": "chat", "data": "t.example.com.", "ttl": 3600, + "recordId": service, "service": service, "protocol": "_tcp", + "port": port, "priority": 10, "weight": 5 + }) + }; + let result = |records: Vec| { + serde_json::json!({ + "domain": "example.com", "type": "SRV", "name": "chat", + "replaced": 0, "unchanged": 2, "created": 0, "deleted": 0, + "action": "set", "records": records + }) + }; + + let rendered = cli_engine::preview_human_view( + result(vec![srv("_sip", 5060), srv("_xmpp", 5222)]), + &columns, + ); + assert!( + rendered.contains("SERVICE") && rendered.contains("PORT"), + "{rendered}" + ); + assert!( + rendered.contains("_sip") && rendered.contains("_xmpp"), + "{rendered}" + ); + + let plain = serde_json::json!({ + "type": "A", "name": "www", "data": "1.2.3.4", "ttl": 3600, "recordId": "r1" + }); + let rendered = cli_engine::preview_human_view(result(vec![plain]), &columns); + for sparse in ["SERVICE", "PORT", "PRIORITY", "PROTOCOL"] { + assert!(!rendered.contains(sparse), "{sparse} shown: {rendered}"); + } + } + /// Proves `view_columns()`'s field names actually match what /// `dry_run_set_preview` emits — a mismatch here would silently drop the /// reconcile plan from human output.