-
Notifications
You must be signed in to change notification settings - Fork 14
/
cli_builder.rs
613 lines (533 loc) · 24.2 KB
/
cli_builder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// Copyright 2024 Oxide Computer Company
use std::{collections::BTreeMap, marker::PhantomData, path::PathBuf};
use anyhow::{bail, Result};
use async_trait::async_trait;
use clap::{ArgMatches, Command, CommandFactory, FromArgMatches};
use log::LevelFilter;
use crate::{
generated_cli::{Cli, CliCommand},
OxideOverride, RunnableCmd,
};
use oxide::{
config::{Config, ResolveValue},
context::Context,
};
#[derive(clap::Parser, Debug, Clone)]
#[command(name = "oxide")]
struct OxideCli {
/// Enable debug output
#[clap(long)]
pub debug: bool,
/// Directory to use for configuration
#[clap(long, value_name = "DIR")]
pub config_dir: Option<PathBuf>,
/// Modify name resolution
#[clap(long, value_name = "HOST:PORT:ADDR")]
pub resolve: Option<ResolveValue>,
/// Specify a trusted CA cert
#[clap(long, value_name = "FILE")]
pub cacert: Option<PathBuf>,
/// Disable certificate validation and hostname verification
#[clap(long)]
pub insecure: bool,
/// Timeout value for individual API invocations
#[clap(long, value_name = "SECONDS")]
pub timeout: Option<u64>,
}
#[async_trait]
trait RunIt: Send + Sync {
async fn run_cmd(&self, matches: &ArgMatches, ctx: &Context) -> Result<()>;
fn is_subtree(&self) -> bool;
}
#[derive(Default)]
struct CommandBuilder<'a> {
children: BTreeMap<&'a str, CommandBuilder<'a>>,
cmd: Option<Box<dyn RunIt>>,
terminal: bool,
}
pub struct NewCli<'a> {
parser: Command,
runner: CommandBuilder<'a>,
}
impl<'a> Default for NewCli<'a> {
fn default() -> Self {
let mut parser = OxideCli::command().name("oxide").subcommand_required(true);
let mut runner = CommandBuilder::default();
for op in CliCommand::iter() {
let Some(path) = xxx(op) else { continue };
runner.add_cmd(path, GeneratedCmd(op));
let cmd = Cli::<OxideOverride>::get_command(op);
let cmd = match op {
CliCommand::IpPoolRangeAdd
| CliCommand::IpPoolRangeRemove
| CliCommand::IpPoolServiceRangeAdd
| CliCommand::IpPoolServiceRangeRemove => cmd
.mut_arg("json-body", |arg| arg.required(false))
.arg(
clap::Arg::new("first")
.long("first")
.value_name("ip-addr")
.required(true)
.value_parser(clap::value_parser!(std::net::IpAddr)),
)
.arg(
clap::Arg::new("last")
.long("last")
.value_name("ip-addr")
.required(true)
.value_parser(clap::value_parser!(std::net::IpAddr)),
),
CliCommand::SamlIdentityProviderCreate => cmd
.mut_arg("json-body", |arg| arg.required(false))
.arg(
clap::Arg::new("metadata-url")
.long("metadata-url")
.value_name("url")
.value_parser(clap::value_parser!(String)),
)
.arg(
clap::Arg::new("metadata-value")
.long("metadata-value")
.value_name("xml")
.value_parser(clap::value_parser!(String)),
)
.group(
clap::ArgGroup::new("idp_metadata_source")
.args(["metadata-url", "metadata-value"])
.required(true)
.multiple(false),
),
CliCommand::NetworkingAllowListUpdate => cmd
.mut_arg("json-body", |arg| arg.required(false))
.arg(
clap::Arg::new("any")
.long("any")
.action(clap::ArgAction::SetTrue)
.value_parser(clap::value_parser!(bool)),
)
.arg(
clap::Arg::new("ips")
.long("ip")
.action(clap::ArgAction::Append)
.value_name("IP or IPNET")
.value_parser(clap::value_parser!(crate::IpOrNet)),
)
.group(
clap::ArgGroup::new("allow-list")
.args(["ips", "any"])
.required(true)
.multiple(false),
),
// Command is fine as-is.
_ => cmd,
};
parser = parser.add_subcommand(path, cmd);
// print_cmd(&parser, 0);
}
Self { parser, runner }
}
}
#[async_trait]
impl<C> RunIt for CustomCmd<C>
where
C: Send + Sync + FromArgMatches + RunnableCmd,
{
async fn run_cmd(&self, matches: &ArgMatches, ctx: &Context) -> Result<()> {
let cmd = C::from_arg_matches(matches).unwrap();
cmd.run(ctx).await
}
fn is_subtree(&self) -> bool {
<C as RunnableCmd>::is_subtree()
}
}
impl<'a> NewCli<'a> {
pub fn add_custom<N>(mut self, path: &'a str) -> Self
where
N: Send + Sync + FromArgMatches + RunnableCmd + CommandFactory + 'static,
{
self.runner.add_cmd(path, CustomCmd::<N>::new());
self.parser = self.parser.add_subcommand(path, N::command());
self
}
pub async fn run(self) -> Result<()> {
let Self { parser, runner } = self;
let matches = parser.get_matches();
let OxideCli {
debug,
config_dir,
resolve,
cacert,
insecure,
timeout,
} = OxideCli::from_arg_matches(&matches).unwrap();
if debug {
env_logger::builder().filter_level(LevelFilter::Debug);
}
let mut config = if let Some(dir) = config_dir {
Config::new_with_config_dir(dir)
} else {
Config::default()
};
if let Some(resolve) = resolve {
config = config.with_resolve(resolve);
}
if let Some(cacert_path) = cacert {
enum CertType {
Pem,
Der,
}
let extension = cacert_path
.extension()
.map(std::ffi::OsStr::to_ascii_lowercase);
let ct = match extension.as_ref().and_then(|ex| ex.to_str()) {
Some("pem") => CertType::Pem,
Some("der") => CertType::Der,
_ => bail!("--cacert path must be a 'pem' or 'der' file".to_string()),
};
let contents = std::fs::read(cacert_path)?;
let cert = match ct {
CertType::Pem => reqwest::tls::Certificate::from_pem(&contents),
CertType::Der => reqwest::tls::Certificate::from_der(&contents),
}?;
config = config.with_cert(cert);
}
config = config.with_insecure(insecure);
if let Some(timeout) = timeout {
config = config.with_timeout(timeout);
}
let ctx = Context::new(config)?;
let mut node = &runner;
let mut sm = &matches;
while let Some((sub_name, sub_matches)) = sm.subcommand() {
node = node.children.get(sub_name).unwrap();
sm = sub_matches;
if node.terminal {
break;
}
}
node.cmd.as_ref().unwrap().run_cmd(sm, &ctx).await
}
pub fn command(&self) -> &Command {
&self.parser
}
pub fn command_take(self) -> Command {
self.parser
}
}
impl<'a> CommandBuilder<'a> {
pub fn add_cmd(&mut self, path: &'a str, cmd: impl RunIt + 'static) {
let mut node = self;
for component in path.split(' ') {
node = node.children.entry(component).or_default();
}
node.terminal = cmd.is_subtree();
node.cmd = Some(Box::new(cmd));
}
}
struct GeneratedCmd(CliCommand);
#[async_trait]
impl RunIt for GeneratedCmd {
async fn run_cmd(&self, matches: &ArgMatches, ctx: &Context) -> Result<()> {
let cli = Cli::new(ctx.client()?.clone(), OxideOverride::default());
cli.execute(self.0, matches).await
}
fn is_subtree(&self) -> bool {
false
}
}
struct CustomCmd<C> {
_cmd: PhantomData<C>,
}
impl<C> CustomCmd<C> {
pub fn new() -> Self {
Self { _cmd: PhantomData }
}
}
fn xxx<'a>(command: CliCommand) -> Option<&'a str> {
match command {
CliCommand::InstanceList => Some("instance list"),
CliCommand::InstanceCreate => Some("instance create"),
CliCommand::InstanceView => Some("instance view"),
CliCommand::InstanceDelete => Some("instance delete"),
CliCommand::InstanceMigrate => None, // TODO delete from API?
CliCommand::InstanceReboot => Some("instance reboot"),
CliCommand::InstanceSerialConsole => None, // Special-cased
CliCommand::InstanceSerialConsoleStream => None, // Ditto
CliCommand::InstanceStart => Some("instance start"),
CliCommand::InstanceStop => Some("instance stop"),
CliCommand::InstanceExternalIpList => Some("instance external-ip list"),
CliCommand::InstanceEphemeralIpAttach => Some("instance external-ip attach-ephemeral"),
CliCommand::InstanceEphemeralIpDetach => Some("instance external-ip detach-ephemeral"),
CliCommand::InstanceSshPublicKeyList => Some("instance ssh-key list"),
CliCommand::ProjectList => Some("project list"),
CliCommand::ProjectCreate => Some("project create"),
CliCommand::ProjectView => Some("project view"),
CliCommand::ProjectUpdate => Some("project update"),
CliCommand::ProjectDelete => Some("project delete"),
CliCommand::ProjectPolicyView => Some("project policy view"),
CliCommand::ProjectPolicyUpdate => Some("project policy update"),
CliCommand::ProjectIpPoolList => Some("project ip-pool list"),
CliCommand::ProjectIpPoolView => Some("project ip-pool view"),
CliCommand::ImageList => Some("image list"),
CliCommand::ImageCreate => Some("image create"),
CliCommand::ImageView => Some("image view"),
CliCommand::ImageDelete => Some("image delete"),
CliCommand::ImagePromote => Some("image promote"),
CliCommand::ImageDemote => Some("image demote"),
CliCommand::IpPoolList => Some("ip-pool list"),
CliCommand::IpPoolCreate => Some("ip-pool create"),
CliCommand::IpPoolView => Some("ip-pool view"),
CliCommand::IpPoolUpdate => Some("ip-pool update"),
CliCommand::IpPoolDelete => Some("ip-pool delete"),
CliCommand::IpPoolRangeList => Some("ip-pool range list"),
CliCommand::IpPoolRangeAdd => Some("ip-pool range add"),
CliCommand::IpPoolRangeRemove => Some("ip-pool range remove"),
CliCommand::IpPoolServiceView => Some("ip-pool service view"),
CliCommand::IpPoolServiceRangeList => Some("ip-pool service range list"),
CliCommand::IpPoolServiceRangeAdd => Some("ip-pool service range add"),
CliCommand::IpPoolServiceRangeRemove => Some("ip-pool service remove"),
CliCommand::IpPoolSiloList => Some("ip-pool silo list"),
CliCommand::IpPoolSiloLink => Some("ip-pool silo link"),
CliCommand::IpPoolSiloUpdate => Some("ip-pool silo update"),
CliCommand::IpPoolSiloUnlink => Some("ip-pool silo unlink"),
CliCommand::IpPoolUtilizationView => Some("ip-pool utilization"),
CliCommand::SiloList => Some("silo list"),
CliCommand::SiloCreate => Some("silo create"),
CliCommand::SiloView => Some("silo view"),
CliCommand::SiloDelete => Some("silo delete"),
CliCommand::SiloPolicyView => Some("silo policy view"),
CliCommand::SiloPolicyUpdate => Some("silo policy update"),
CliCommand::SiloUserList => Some("silo user list"),
CliCommand::SiloUserView => Some("silo user view"),
CliCommand::SiloIdentityProviderList => Some("silo idp list"),
CliCommand::LocalIdpUserCreate => Some("silo idp local user create"),
CliCommand::LocalIdpUserDelete => Some("silo idp local user delete"),
CliCommand::LocalIdpUserSetPassword => Some("silo idp local user set-password"),
CliCommand::SamlIdentityProviderCreate => Some("silo idp saml create"),
CliCommand::SamlIdentityProviderView => Some("silo idp saml view"),
CliCommand::SystemQuotasList => Some("silo quotas list"),
CliCommand::SiloQuotasView => Some("silo quotas view"),
CliCommand::SiloQuotasUpdate => Some("silo quotas update"),
CliCommand::SiloUtilizationList => Some("silo utilization list"),
CliCommand::SiloUtilizationView => Some("silo utilization view"),
CliCommand::SiloIpPoolList => Some("silo ip-pool list"),
CliCommand::UtilizationView => Some("utilization"),
CliCommand::UserList => Some("user list"),
// VPCs
CliCommand::VpcList => Some("vpc list"),
CliCommand::VpcCreate => Some("vpc create"),
CliCommand::VpcView => Some("vpc view"),
CliCommand::VpcUpdate => Some("vpc update"),
CliCommand::VpcDelete => Some("vpc delete"),
CliCommand::VpcFirewallRulesView => Some("vpc firewall-rules view"),
CliCommand::VpcFirewallRulesUpdate => Some("vpc firewall-rules update"),
CliCommand::VpcSubnetList => Some("vpc subnet list"),
CliCommand::VpcSubnetCreate => Some("vpc subnet create"),
CliCommand::VpcSubnetView => Some("vpc subnet view"),
CliCommand::VpcSubnetUpdate => Some("vpc subnet update"),
CliCommand::VpcSubnetDelete => Some("vpc subnet delete"),
CliCommand::VpcSubnetListNetworkInterfaces => Some("vpc subnet nic list"),
CliCommand::NetworkingAddressLotList => Some("system networking address-lot list"),
CliCommand::NetworkingAddressLotCreate => Some("system networking address-lot create"),
CliCommand::NetworkingAddressLotDelete => Some("system networking address-lot delete"),
CliCommand::NetworkingAddressLotBlockList => {
Some("system networking address-lot block list")
}
CliCommand::NetworkingLoopbackAddressList => {
Some("system networking loopback-address list")
}
CliCommand::NetworkingLoopbackAddressCreate => {
Some("system networking loopback-address create")
}
CliCommand::NetworkingLoopbackAddressDelete => {
Some("system networking loopback-address delete")
}
CliCommand::NetworkingSwitchPortApplySettings => {
Some("system hardware switch-port apply-settings")
}
CliCommand::NetworkingSwitchPortClearSettings => {
Some("system hardware switch-port clear-settings")
}
CliCommand::NetworkingSwitchPortList => Some("system hardware switch-port list"),
CliCommand::NetworkingSwitchPortStatus => Some("system hardware switch-port status"),
CliCommand::NetworkingSwitchPortSettingsList => {
Some("system networking switch-port-settings list")
}
CliCommand::NetworkingSwitchPortSettingsCreate => {
Some("system networking switch-port-settings create")
}
CliCommand::NetworkingSwitchPortSettingsDelete => {
Some("system networking switch-port-settings delete")
}
CliCommand::NetworkingSwitchPortSettingsView => {
Some("system networking switch-port-settings view")
}
CliCommand::NetworkingBfdStatus => Some("system networking bfd status"),
CliCommand::NetworkingBfdEnable => Some("system networking bfd enable"),
CliCommand::NetworkingBfdDisable => Some("system networking bfd disable"),
CliCommand::NetworkingBgpStatus => Some("system networking bgp status"),
CliCommand::NetworkingBgpMessageHistory => Some("system networking bgp history"),
CliCommand::NetworkingBgpConfigCreate => Some("system networking bgp config create"),
CliCommand::NetworkingBgpConfigDelete => Some("system networking bgp config delete"),
CliCommand::NetworkingBgpConfigList => Some("system networking bgp config list"),
CliCommand::NetworkingBgpAnnounceSetCreate => {
Some("system networking bgp announce-set create")
}
CliCommand::NetworkingBgpAnnounceSetDelete => {
Some("system networking bgp announce-set delete")
}
CliCommand::NetworkingBgpAnnounceSetList => Some("system networking bgp announce-set list"),
CliCommand::NetworkingBgpImportedRoutesIpv4 => Some("system networking bgp imported ipv4"),
// Subcommand: disk
CliCommand::DiskList => Some("disk list"),
CliCommand::DiskCreate => Some("disk create"),
CliCommand::DiskView => Some("disk view"),
CliCommand::DiskDelete => Some("disk delete"),
CliCommand::DiskMetricsList => Some("disk metrics list"),
CliCommand::DiskBulkWriteImportStart => Some("disk import start"),
CliCommand::DiskBulkWriteImport => Some("disk import write"),
CliCommand::DiskBulkWriteImportStop => Some("disk import stop"),
CliCommand::DiskFinalizeImport => Some("disk import finalize"),
CliCommand::GroupList => Some("group list"),
// Subcommand: instance
CliCommand::InstanceDiskList => Some("instance disk list"),
CliCommand::InstanceDiskAttach => Some("instance disk attach"),
CliCommand::InstanceDiskDetach => Some("instance disk detach"),
CliCommand::InstanceNetworkInterfaceList => Some("instance nic list"),
CliCommand::InstanceNetworkInterfaceCreate => Some("instance nic create"),
CliCommand::InstanceNetworkInterfaceView => Some("instance nic view"),
CliCommand::InstanceNetworkInterfaceUpdate => Some("instance nic update"),
CliCommand::InstanceNetworkInterfaceDelete => Some("instance nic delete"),
CliCommand::PolicyView => Some("policy view"),
CliCommand::PolicyUpdate => Some("policy update"),
CliCommand::SnapshotList => Some("snapshot list"),
CliCommand::SnapshotCreate => Some("snapshot create"),
CliCommand::SnapshotView => Some("snapshot view"),
CliCommand::SnapshotDelete => Some("snapshot delete"),
CliCommand::CertificateList => Some("certificate list"),
CliCommand::CertificateCreate => Some("certificate create"),
CliCommand::CertificateView => Some("certificate view"),
CliCommand::CertificateDelete => Some("certificate delete"),
CliCommand::SwitchList => Some("system hardware switch list"),
CliCommand::SwitchView => Some("system hardware switch view"),
CliCommand::RackList => Some("system hardware rack list"),
CliCommand::RackView => Some("system hardware rack view"),
CliCommand::SledList => Some("system hardware sled list"),
CliCommand::SledListUninitialized => Some("system hardware sled list-uninitialized"),
CliCommand::SledView => Some("system hardware sled view"),
CliCommand::SledAdd => Some("system hardware sled add"),
CliCommand::SledSetProvisionPolicy => Some("system hardware sled set-provision-policy"),
CliCommand::SledInstanceList => Some("system hardware sled instance-list"),
CliCommand::PhysicalDiskList => Some("system hardware disk list"),
CliCommand::PhysicalDiskView => Some("system hardware disk view"),
CliCommand::SledPhysicalDiskList => Some("system hardware sled disk-led"),
CliCommand::SystemPolicyView => Some("system policy view"),
CliCommand::SystemPolicyUpdate => Some("system policy update"),
CliCommand::NetworkingAllowListView => Some("system networking allow-list view"),
CliCommand::NetworkingAllowListUpdate => Some("system networking allow-list update"),
CliCommand::CurrentUserView => Some("current-user view"),
CliCommand::CurrentUserGroups => Some("current-user groups"),
CliCommand::CurrentUserSshKeyList => Some("current-user ssh-key list"),
CliCommand::CurrentUserSshKeyCreate => Some("current-user ssh-key create"),
CliCommand::CurrentUserSshKeyView => Some("current-user ssh-key view"),
CliCommand::CurrentUserSshKeyDelete => Some("current-user ssh-key delete"),
CliCommand::FloatingIpAttach => Some("floating-ip attach"),
CliCommand::FloatingIpCreate => Some("floating-ip create"),
CliCommand::FloatingIpDelete => Some("floating-ip delete"),
CliCommand::FloatingIpDetach => Some("floating-ip detach"),
CliCommand::FloatingIpList => Some("floating-ip list"),
CliCommand::FloatingIpUpdate => Some("floating-ip update"),
CliCommand::FloatingIpView => Some("floating-ip view"),
CliCommand::Ping => Some("ping"),
CliCommand::ProbeCreate => Some("experimental probe create"),
CliCommand::ProbeDelete => Some("experimental probe delete"),
CliCommand::ProbeList => Some("experimental probe list"),
CliCommand::ProbeView => Some("experimental probe view"),
// Metrics-related subcommands
CliCommand::TimeseriesQuery => Some("experimental timeseries query"),
CliCommand::TimeseriesSchemaList => Some("experimental timeseries schema list"),
// Commands not yet implemented
CliCommand::DeviceAccessToken
| CliCommand::DeviceAuthConfirm
| CliCommand::DeviceAuthRequest
| CliCommand::GroupView
| CliCommand::LoginLocal
| CliCommand::LoginSaml
| CliCommand::Logout
| CliCommand::RoleList
| CliCommand::RoleView
| CliCommand::SiloMetric
| CliCommand::SystemMetric
| CliCommand::UserBuiltinList
| CliCommand::UserBuiltinView => None,
}
}
trait CommandExt {
fn add_subcommand(self, path: &str, subcmd: impl Into<Command>) -> Self;
}
impl CommandExt for Command {
fn add_subcommand(self, path: &str, subcmd: impl Into<Command>) -> Self {
if let Some(index) = path.find(' ') {
let first = &path[..index];
let rest = &path[index + 1..];
let has_subcommand = self.find_subcommand(first).is_some();
if has_subcommand {
self.mut_subcommand(first, |cmd| cmd.add_subcommand(rest, subcmd))
} else {
self.subcommand(
Command::new(first.to_owned())
.subcommand_required(true)
.add_subcommand(rest, subcmd),
)
}
} else {
let new_subcmd = subcmd.into().name(path.to_owned());
let has_subcommand = self.find_subcommand(path).is_some();
if has_subcommand {
self.mut_subcommand(path, |cmd| {
// Replace the subcommand, but retain its subcommands.
new_subcmd.subcommands(cmd.get_subcommands())
})
} else {
self.subcommand(new_subcmd)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_value_parses_ipv4_addr() {
let addr = "192.168.1.1";
let host = "oxide.computer";
let port = 12345;
let parsed: ResolveValue = format!("{host}:{port}:{addr}").parse().unwrap();
assert_eq!(
parsed,
ResolveValue {
host: host.to_string(),
port,
addr: addr.parse().unwrap(),
}
);
}
#[test]
fn resolve_value_parses_ipv6_addr() {
let addr = "fdb2:a840:2504:355::1";
let host = "oxide.computer";
let port = 12345;
let parsed: ResolveValue = format!("{host}:{port}:[{addr}]").parse().unwrap();
assert_eq!(
parsed,
ResolveValue {
host: host.to_string(),
port,
addr: addr.parse().unwrap(),
}
);
}
}