Skip to content

Commit 77e28b7

Browse files
committed
Refactor homologate data structures
1 parent aff6f1f commit 77e28b7

File tree

5 files changed

+145
-201
lines changed

5 files changed

+145
-201
lines changed

src/cli.rs

+57-135
Original file line numberDiff line numberDiff line change
@@ -28,66 +28,16 @@ pub struct Opts {
2828
/// A list of flakes to deploy alternatively
2929
#[clap(long, group = "deploy")]
3030
targets: Option<Vec<String>>,
31-
/// Check signatures when using `nix copy`
32-
#[clap(short, long)]
33-
checksigs: bool,
34-
/// Use the interactive prompt before deployment
35-
#[clap(short, long)]
36-
interactive: bool,
37-
/// Extra arguments to be passed to nix build
38-
extra_build_args: Vec<String>,
39-
40-
/// Print debug logs to output
41-
#[clap(short, long)]
42-
debug_logs: bool,
43-
/// Directory to print logs to (including the background activation process)
44-
#[clap(long)]
45-
log_dir: Option<String>,
46-
47-
/// Keep the build outputs of each built profile
48-
#[clap(short, long)]
49-
keep_result: bool,
50-
/// Location to keep outputs from built profiles in
51-
#[clap(short, long)]
52-
result_path: Option<String>,
5331

54-
/// Skip the automatic pre-build checks
55-
#[clap(short, long)]
56-
skip_checks: bool,
57-
58-
/// Override the SSH user with the given value
59-
#[clap(long)]
60-
ssh_user: Option<String>,
61-
/// Override the profile user with the given value
62-
#[clap(long)]
63-
profile_user: Option<String>,
64-
/// Override the SSH options used
65-
#[clap(long)]
66-
ssh_opts: Option<String>,
67-
/// Override if the connecting to the target node should be considered fast
68-
#[clap(long)]
69-
fast_connection: Option<bool>,
70-
/// Override if a rollback should be attempted if activation fails
71-
#[clap(long)]
72-
auto_rollback: Option<bool>,
7332
/// Override hostname used for the node
7433
#[clap(long)]
7534
hostname: Option<String>,
76-
/// Make activation wait for confirmation, or roll back after a period of time
77-
#[clap(long)]
78-
magic_rollback: Option<bool>,
79-
/// How long activation should wait for confirmation (if using magic-rollback)
80-
#[clap(long)]
81-
confirm_timeout: Option<u16>,
82-
/// Where to store temporary files (only used by magic-rollback)
83-
#[clap(long)]
84-
temp_path: Option<String>,
85-
/// Show what will be activated on the machines
86-
#[clap(long)]
87-
dry_activate: bool,
88-
/// Revoke all previously succeeded deploys when deploying multiple profiles
89-
#[clap(long)]
90-
rollback_succeeded: Option<bool>,
35+
36+
#[clap(flatten)]
37+
flags: data::Flags,
38+
39+
#[clap(flatten)]
40+
generic_settings: settings::GenericSettings,
9141
}
9242

9343
/// Returns if the available Nix installation supports flakes
@@ -240,27 +190,20 @@ type ToDeploy<'a> = Vec<(
240190
)>;
241191

242192
async fn run_deploy(
243-
deploy_targets: Vec<data::Target>,
244-
data: Vec<settings::Root>,
193+
targets: Vec<data::Target>,
194+
settings: Vec<settings::Root>,
245195
supports_flakes: bool,
246-
check_sigs: bool,
247-
interactive: bool,
248-
cmd_overrides: &data::CmdOverrides,
249-
keep_result: bool,
250-
result_path: Option<&str>,
251-
extra_build_args: &[String],
252-
debug_logs: bool,
253-
dry_activate: bool,
254-
log_dir: &Option<String>,
255-
rollback_succeeded: bool,
196+
hostname: Option<String>,
197+
cmd_settings: settings::GenericSettings,
198+
cmd_flags: data::Flags,
256199
) -> Result<(), RunDeployError> {
257-
let to_deploy: ToDeploy = deploy_targets
200+
let to_deploy: ToDeploy = targets
258201
.iter()
259-
.zip(&data)
260-
.map(|(deploy_target, data)| {
261-
let to_deploys: ToDeploy = match (&deploy_target.node, &deploy_target.profile) {
202+
.zip(&settings)
203+
.map(|(target, root)| {
204+
let to_deploys: ToDeploy = match (&target.node, &target.profile) {
262205
(Some(node_name), Some(profile_name)) => {
263-
let node = match data.nodes.get(node_name) {
206+
let node = match root.nodes.get(node_name) {
264207
Some(x) => x,
265208
None => return Err(RunDeployError::NodeNotFound(node_name.clone())),
266209
};
@@ -270,14 +213,14 @@ async fn run_deploy(
270213
};
271214

272215
vec![(
273-
deploy_target,
274-
data,
216+
&target,
217+
&root,
275218
(node_name.as_str(), node),
276219
(profile_name.as_str(), profile),
277220
)]
278221
}
279222
(Some(node_name), None) => {
280-
let node = match data.nodes.get(node_name) {
223+
let node = match root.nodes.get(node_name) {
281224
Some(x) => x,
282225
None => return Err(RunDeployError::NodeNotFound(node_name.clone())),
283226
};
@@ -304,13 +247,13 @@ async fn run_deploy(
304247

305248
profiles_list
306249
.into_iter()
307-
.map(|x| (deploy_target, data, (node_name.as_str(), node), x))
250+
.map(|x| (target, root, (node_name.as_str(), node), x))
308251
.collect()
309252
}
310253
(None, None) => {
311254
let mut l = Vec::new();
312255

313-
for (node_name, node) in &data.nodes {
256+
for (node_name, node) in &root.nodes {
314257
let mut profiles_list: Vec<(&str, &settings::Profile)> = Vec::new();
315258

316259
for profile_name in [
@@ -335,7 +278,7 @@ async fn run_deploy(
335278

336279
let ll: ToDeploy = profiles_list
337280
.into_iter()
338-
.map(|x| (deploy_target, data, (node_name.as_str(), node), x))
281+
.map(|x| (target, root, (node_name.as_str(), node), x))
339282
.collect();
340283

341284
l.extend(ll);
@@ -358,39 +301,39 @@ async fn run_deploy(
358301
data::DeployDefs,
359302
)> = Vec::new();
360303

361-
for (deploy_target, data, (node_name, node), (profile_name, profile)) in to_deploy {
304+
for (target, root, (node_name, node), (profile_name, profile)) in to_deploy {
362305
let deploy_data = data::make_deploy_data(
363-
&data.generic_settings,
306+
&root.generic_settings,
307+
&cmd_settings,
308+
&cmd_flags,
364309
node,
365310
node_name,
366311
profile,
367312
profile_name,
368-
cmd_overrides,
369-
debug_logs,
370-
log_dir.as_deref(),
313+
hostname.as_deref(),
371314
);
372315

373316
let deploy_defs = deploy_data.defs()?;
374317

375-
parts.push((deploy_target, deploy_data, deploy_defs));
318+
parts.push((target, deploy_data, deploy_defs));
376319
}
377320

378-
if interactive {
321+
if cmd_flags.interactive {
379322
prompt_deployment(&parts[..])?;
380323
} else {
381324
print_deployment(&parts[..])?;
382325
}
383326

384-
for (deploy_target, deploy_data, deploy_defs) in &parts {
327+
for (target, deploy_data, deploy_defs) in &parts {
385328
deploy::push::push_profile(deploy::push::PushProfileData {
386-
supports_flakes,
387-
check_sigs,
388-
repo: &deploy_target.repo,
389-
deploy_data,
390-
deploy_defs,
391-
keep_result,
392-
result_path,
393-
extra_build_args,
329+
supports_flakes: &supports_flakes,
330+
check_sigs: &cmd_flags.checksigs,
331+
repo: &target.repo,
332+
deploy_data: &deploy_data,
333+
deploy_defs: &deploy_defs,
334+
keep_result: &cmd_flags.keep_result,
335+
result_path: cmd_flags.result_path.as_deref(),
336+
extra_build_args: &cmd_flags.extra_build_args,
394337
})
395338
.await?;
396339
}
@@ -402,14 +345,14 @@ async fn run_deploy(
402345
// Rollbacks adhere to the global seeting to auto_rollback and secondary
403346
// the profile's configuration
404347
for (_, deploy_data, deploy_defs) in &parts {
405-
if let Err(e) = deploy::deploy::deploy_profile(deploy_data, deploy_defs, dry_activate).await
348+
if let Err(e) = deploy::deploy::deploy_profile(deploy_data, deploy_defs, cmd_flags.dry_activate).await
406349
{
407350
error!("{}", e);
408-
if dry_activate {
351+
if cmd_flags.dry_activate {
409352
info!("dry run, not rolling back");
410353
}
411354
info!("Revoking previous deploys");
412-
if rollback_succeeded && cmd_overrides.auto_rollback.unwrap_or(true) {
355+
if cmd_flags.rollback_succeeded && cmd_settings.auto_rollback.unwrap_or(true) {
413356
// revoking all previous deploys
414357
// (adheres to profile configuration if not set explicitely by
415358
// the command line)
@@ -454,8 +397,8 @@ pub async fn run(args: Option<&ArgMatches>) -> Result<(), RunError> {
454397
};
455398

456399
deploy::init_logger(
457-
opts.debug_logs,
458-
opts.log_dir.as_deref(),
400+
opts.flags.debug_logs,
401+
opts.flags.log_dir.as_deref(),
459402
&deploy::LoggerType::Deploy,
460403
)?;
461404

@@ -464,51 +407,30 @@ pub async fn run(args: Option<&ArgMatches>) -> Result<(), RunError> {
464407
.targets
465408
.unwrap_or_else(|| vec![opts.clone().target.unwrap_or_else(|| ".".to_string())]);
466409

467-
let deploy_targets: Vec<data::Target> = deploys
468-
.iter()
469-
.map(|f| f.parse::<data::Target>())
470-
.collect::<Result<Vec<data::Target>, data::ParseTargetError>>()?;
471-
472-
let cmd_overrides = data::CmdOverrides {
473-
ssh_user: opts.ssh_user,
474-
profile_user: opts.profile_user,
475-
ssh_opts: opts.ssh_opts,
476-
fast_connection: opts.fast_connection,
477-
auto_rollback: opts.auto_rollback,
478-
hostname: opts.hostname,
479-
magic_rollback: opts.magic_rollback,
480-
temp_path: opts.temp_path,
481-
confirm_timeout: opts.confirm_timeout,
482-
dry_activate: opts.dry_activate,
483-
};
484-
485410
let supports_flakes = test_flake_support().await.map_err(RunError::FlakeTest)?;
486411

487412
if !supports_flakes {
488413
warn!("A Nix version without flakes support was detected, support for this is work in progress");
489414
}
490415

491-
if !opts.skip_checks {
492-
for deploy_target in deploy_targets.iter() {
493-
flake::check_deployment(supports_flakes, &deploy_target.repo, &opts.extra_build_args).await?;
416+
let targets: Vec<data::Target> = deploys
417+
.into_iter()
418+
.map(|f| f.parse::<data::Target>())
419+
.collect::<Result<Vec<data::Target>, data::ParseTargetError>>()?;
420+
421+
if !opts.flags.skip_checks {
422+
for target in targets.iter() {
423+
flake::check_deployment(supports_flakes, &target.repo, &opts.flags.extra_build_args).await?;
494424
}
495425
}
496-
let result_path = opts.result_path.as_deref();
497-
let data = flake::get_deployment_data(supports_flakes, &deploy_targets, &opts.extra_build_args).await?;
426+
let settings = flake::get_deployment_data(supports_flakes, &targets, &opts.flags.extra_build_args).await?;
498427
run_deploy(
499-
deploy_targets,
500-
data,
428+
targets,
429+
settings,
501430
supports_flakes,
502-
opts.checksigs,
503-
opts.interactive,
504-
&cmd_overrides,
505-
opts.keep_result,
506-
result_path,
507-
&opts.extra_build_args,
508-
opts.debug_logs,
509-
opts.dry_activate,
510-
&opts.log_dir,
511-
opts.rollback_succeeded.unwrap_or(true),
431+
opts.hostname,
432+
opts.generic_settings,
433+
opts.flags,
512434
)
513435
.await?;
514436

0 commit comments

Comments
 (0)