From 3fbc6b94c7e4cad06902d01d5b14694d23fa71c9 Mon Sep 17 00:00:00 2001 From: Owen Jacobson Date: Wed, 24 Jul 2024 19:47:07 -0400 Subject: Legibility improvements to main. The explicit panic is a conscious decision; clap doesn't have a better way to represent mutually-exclusive options, so we have to use two boolean fields, but it will actually prevent the user from passing both options and triggering that code path. Since it's one of the rare situations where design choices converge on a truly unreachable case, I thought I'd make it easier to discover when I'm wrong and it is actually reachable. The old logic treated (true, true) like (false, false). --- src/cli.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 96758be..0362b5e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -42,20 +42,18 @@ pub struct Args { impl Args { pub async fn run(self) -> Result<()> { let args = Args::parse(); - let name = args.autoscaling_group; let aws_context = AwsContext::from_env().await; - let apply_mode = if args.dry_run { - ApplyMode::DryRun - } else if args.apply { - ApplyMode::Apply - } else { - ApplyMode::DryRun - }; + let changes = named_asg_changes( + &aws_context, + &args.autoscaling_group, + &args.dns_name, + args.dns_ttl, + ) + .await?; - let changes = named_asg_changes(&aws_context, &name, &args.dns_name, args.dns_ttl).await?; - apply_mode + self.apply_mode() .apply( &aws_context, &changes.zone_id, @@ -66,4 +64,14 @@ impl Args { Ok(()) } + + fn apply_mode(&self) -> ApplyMode { + use ApplyMode::*; + match (self.dry_run, self.apply) { + (true, false) => DryRun, + (false, true) => Apply, + (false, false) => DryRun, + (true, true) => unreachable!("Cannot set --apply and --dry-run together"), + } + } } -- cgit v1.2.3