Skip to content

Commit c6c2f00

Browse files
committed
Refactor homologate data structures
1 parent 519661d commit c6c2f00

File tree

5 files changed

+144
-200
lines changed

5 files changed

+144
-200
lines changed

src/cli.rs

Lines changed: 55 additions & 133 deletions
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 => Err(RunDeployError::NodeNotFound(node_name.to_owned()))?,
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.to_owned())),
283226
};
@@ -306,13 +249,13 @@ async fn run_deploy(
306249

307250
profiles_list
308251
.into_iter()
309-
.map(|x| (deploy_target, data, (node_name.as_str(), node), x))
252+
.map(|x| (target, root, (node_name.as_str(), node), x))
310253
.collect()
311254
}
312255
(None, None) => {
313256
let mut l = Vec::new();
314257

315-
for (node_name, node) in &data.nodes {
258+
for (node_name, node) in &root.nodes {
316259
let mut profiles_list: Vec<(&str, &settings::Profile)> = Vec::new();
317260

318261
for profile_name in [
@@ -337,7 +280,7 @@ async fn run_deploy(
337280

338281
let ll: ToDeploy = profiles_list
339282
.into_iter()
340-
.map(|x| (deploy_target, data, (node_name.as_str(), node), x))
283+
.map(|x| (target, root, (node_name.as_str(), node), x))
341284
.collect();
342285

343286
l.extend(ll);
@@ -360,39 +303,39 @@ async fn run_deploy(
360303
data::DeployDefs,
361304
)> = Vec::new();
362305

363-
for (deploy_target, data, (node_name, node), (profile_name, profile)) in to_deploy {
306+
for (target, root, (node_name, node), (profile_name, profile)) in to_deploy {
364307
let deploy_data = data::make_deploy_data(
365-
&data.generic_settings,
308+
&root.generic_settings,
309+
&cmd_settings,
310+
&cmd_flags,
366311
node,
367312
node_name,
368313
profile,
369314
profile_name,
370-
&cmd_overrides,
371-
debug_logs,
372-
log_dir.as_deref(),
315+
hostname.as_deref(),
373316
);
374317

375318
let deploy_defs = deploy_data.defs()?;
376319

377-
parts.push((deploy_target, deploy_data, deploy_defs));
320+
parts.push((target, deploy_data, deploy_defs));
378321
}
379322

380-
if interactive {
323+
if cmd_flags.interactive {
381324
prompt_deployment(&parts[..])?;
382325
} else {
383326
print_deployment(&parts[..])?;
384327
}
385328

386-
for (deploy_target, deploy_data, deploy_defs) in &parts {
329+
for (target, deploy_data, deploy_defs) in &parts {
387330
deploy::push::push_profile(deploy::push::PushProfileData {
388-
supports_flakes,
389-
check_sigs,
390-
repo: &deploy_target.repo,
331+
supports_flakes: &supports_flakes,
332+
check_sigs: &cmd_flags.checksigs,
333+
repo: &target.repo,
391334
deploy_data: &deploy_data,
392335
deploy_defs: &deploy_defs,
393-
keep_result,
394-
result_path,
395-
extra_build_args,
336+
keep_result: &cmd_flags.keep_result,
337+
result_path: cmd_flags.result_path.as_deref(),
338+
extra_build_args: &cmd_flags.extra_build_args,
396339
})
397340
.await?;
398341
}
@@ -404,14 +347,14 @@ async fn run_deploy(
404347
// Rollbacks adhere to the global seeting to auto_rollback and secondary
405348
// the profile's configuration
406349
for (_, deploy_data, deploy_defs) in &parts {
407-
if let Err(e) = deploy::deploy::deploy_profile(deploy_data, deploy_defs, dry_activate).await
350+
if let Err(e) = deploy::deploy::deploy_profile(deploy_data, deploy_defs, cmd_flags.dry_activate).await
408351
{
409352
error!("{}", e);
410-
if dry_activate {
353+
if cmd_flags.dry_activate {
411354
info!("dry run, not rolling back");
412355
}
413356
info!("Revoking previous deploys");
414-
if rollback_succeeded && cmd_overrides.auto_rollback.unwrap_or(true) {
357+
if cmd_flags.rollback_succeeded && cmd_settings.auto_rollback.unwrap_or(true) {
415358
// revoking all previous deploys
416359
// (adheres to profile configuration if not set explicitely by
417360
// the command line)
@@ -456,8 +399,8 @@ pub async fn run(args: Option<&ArgMatches>) -> Result<(), RunError> {
456399
};
457400

458401
deploy::init_logger(
459-
opts.debug_logs,
460-
opts.log_dir.as_deref(),
402+
opts.flags.debug_logs,
403+
opts.flags.log_dir.as_deref(),
461404
deploy::LoggerType::Deploy,
462405
)?;
463406

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

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

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

493-
if !opts.skip_checks {
494-
for deploy_target in deploy_targets.iter() {
495-
flake::check_deployment(supports_flakes, &deploy_target.repo, &opts.extra_build_args).await?;
418+
let targets: Vec<data::Target> = deploys
419+
.into_iter()
420+
.map(|f| f.parse::<data::Target>())
421+
.collect::<Result<Vec<data::Target>, data::ParseTargetError>>()?;
422+
423+
if !opts.flags.skip_checks {
424+
for target in targets.iter() {
425+
flake::check_deployment(supports_flakes, &target.repo, &opts.flags.extra_build_args).await?;
496426
}
497427
}
498-
let result_path = opts.result_path.as_deref();
499-
let data = flake::get_deployment_data(supports_flakes, &deploy_targets, &opts.extra_build_args).await?;
428+
let settings = flake::get_deployment_data(supports_flakes, &targets, &opts.flags.extra_build_args).await?;
500429
run_deploy(
501-
deploy_targets,
502-
data,
430+
targets,
431+
settings,
503432
supports_flakes,
504-
opts.checksigs,
505-
opts.interactive,
506-
&cmd_overrides,
507-
opts.keep_result,
508-
result_path,
509-
&opts.extra_build_args,
510-
opts.debug_logs,
511-
opts.dry_activate,
512-
&opts.log_dir,
513-
opts.rollback_succeeded.unwrap_or(true),
433+
opts.hostname,
434+
opts.generic_settings,
435+
opts.flags,
514436
)
515437
.await?;
516438

0 commit comments

Comments
 (0)