Skip to content

Commit e5546f9

Browse files
committed
General code cleanup
1 parent d721743 commit e5546f9

File tree

5 files changed

+117
-130
lines changed

5 files changed

+117
-130
lines changed

src/bin/activate.rs

Lines changed: 40 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -95,23 +95,23 @@ struct RevokeOpts {
9595
#[derive(Error, Debug)]
9696
pub enum DeactivateError {
9797
#[error("Failed to execute the rollback command: {0}")]
98-
RollbackError(std::io::Error),
98+
Rollback(std::io::Error),
9999
#[error("The rollback resulted in a bad exit code: {0:?}")]
100-
RollbackExitError(Option<i32>),
100+
RollbackExit(Option<i32>),
101101
#[error("Failed to run command for listing generations: {0}")]
102-
ListGenError(std::io::Error),
102+
ListGen(std::io::Error),
103103
#[error("Command for listing generations resulted in a bad exit code: {0:?}")]
104-
ListGenExitError(Option<i32>),
104+
ListGenExit(Option<i32>),
105105
#[error("Error converting generation list output to utf8: {0}")]
106-
DecodeListGenUtf8Error(#[from] std::string::FromUtf8Error),
106+
DecodeListGenUtf8(std::string::FromUtf8Error),
107107
#[error("Failed to run command for deleting generation: {0}")]
108-
DeleteGenError(std::io::Error),
108+
DeleteGen(std::io::Error),
109109
#[error("Command for deleting generations resulted in a bad exit code: {0:?}")]
110-
DeleteGenExitError(Option<i32>),
110+
DeleteGenExit(Option<i32>),
111111
#[error("Failed to run command for re-activating the last generation: {0}")]
112-
ReactivateError(std::io::Error),
112+
Reactivate(std::io::Error),
113113
#[error("Command for re-activating the last generation resulted in a bad exit code: {0:?}")]
114-
ReactivateExitError(Option<i32>),
114+
ReactivateExit(Option<i32>),
115115
}
116116

117117
pub async fn deactivate(profile_path: &str) -> Result<(), DeactivateError> {
@@ -123,11 +123,11 @@ pub async fn deactivate(profile_path: &str) -> Result<(), DeactivateError> {
123123
.arg("--rollback")
124124
.status()
125125
.await
126-
.map_err(DeactivateError::RollbackError)?;
126+
.map_err(DeactivateError::Rollback)?;
127127

128128
match nix_env_rollback_exit_status.code() {
129129
Some(0) => (),
130-
a => return Err(DeactivateError::RollbackExitError(a)),
130+
a => return Err(DeactivateError::RollbackExit(a)),
131131
};
132132

133133
debug!("Listing generations");
@@ -138,14 +138,15 @@ pub async fn deactivate(profile_path: &str) -> Result<(), DeactivateError> {
138138
.arg("--list-generations")
139139
.output()
140140
.await
141-
.map_err(DeactivateError::ListGenError)?;
141+
.map_err(DeactivateError::ListGen)?;
142142

143143
match nix_env_list_generations_out.status.code() {
144144
Some(0) => (),
145-
a => return Err(DeactivateError::ListGenExitError(a)),
145+
a => return Err(DeactivateError::ListGenExit(a)),
146146
};
147147

148-
let generations_list = String::from_utf8(nix_env_list_generations_out.stdout)?;
148+
let generations_list = String::from_utf8(nix_env_list_generations_out.stdout)
149+
.map_err(DeactivateError::DecodeListGenUtf8)?;
149150

150151
let last_generation_line = generations_list
151152
.lines()
@@ -167,11 +168,11 @@ pub async fn deactivate(profile_path: &str) -> Result<(), DeactivateError> {
167168
.arg(last_generation_id)
168169
.status()
169170
.await
170-
.map_err(DeactivateError::DeleteGenError)?;
171+
.map_err(DeactivateError::DeleteGen)?;
171172

172173
match nix_env_delete_generation_exit_status.code() {
173174
Some(0) => (),
174-
a => return Err(DeactivateError::DeleteGenExitError(a)),
175+
a => return Err(DeactivateError::DeleteGenExit(a)),
175176
};
176177

177178
info!("Attempting to re-activate the last generation");
@@ -181,11 +182,11 @@ pub async fn deactivate(profile_path: &str) -> Result<(), DeactivateError> {
181182
.current_dir(&profile_path)
182183
.status()
183184
.await
184-
.map_err(DeactivateError::ReactivateError)?;
185+
.map_err(DeactivateError::Reactivate)?;
185186

186187
match re_activate_exit_status.code() {
187188
Some(0) => (),
188-
a => return Err(DeactivateError::ReactivateExitError(a)),
189+
a => return Err(DeactivateError::ReactivateExit(a)),
189190
};
190191

191192
Ok(())
@@ -194,15 +195,11 @@ pub async fn deactivate(profile_path: &str) -> Result<(), DeactivateError> {
194195
#[derive(Error, Debug)]
195196
pub enum ActivationConfirmationError {
196197
#[error("Failed to create activation confirmation directory: {0}")]
197-
CreateConfirmDirError(std::io::Error),
198+
CreateConfirmDir(std::io::Error),
198199
#[error("Failed to create activation confirmation file: {0}")]
199-
CreateConfirmFileError(std::io::Error),
200-
#[error("Failed to create file system watcher instance: {0}")]
201-
CreateWatcherError(notify::Error),
202-
#[error("Error forking process: {0}")]
203-
ForkError(i32),
200+
CreateConfirmFile(std::io::Error),
204201
#[error("Could not watch for activation sentinel: {0}")]
205-
WatcherError(#[from] notify::Error),
202+
Watcher(#[from] notify::Error),
206203
}
207204

208205
#[derive(Error, Debug)]
@@ -212,7 +209,7 @@ pub enum DangerZoneError {
212209
#[error("inotify stream ended without activation confirmation")]
213210
NoConfirmation,
214211
#[error("inotify encountered an error: {0}")]
215-
WatchError(notify::Error),
212+
Watch(notify::Error),
216213
}
217214

218215
async fn danger_zone(
@@ -223,7 +220,7 @@ async fn danger_zone(
223220

224221
match timeout(Duration::from_secs(confirm_timeout as u64), events.recv()).await {
225222
Ok(Some(Ok(()))) => Ok(()),
226-
Ok(Some(Err(e))) => Err(DangerZoneError::WatchError(e)),
223+
Ok(Some(Err(e))) => Err(DangerZoneError::Watch(e)),
227224
Ok(None) => Err(DangerZoneError::NoConfirmation),
228225
Err(_) => Err(DangerZoneError::TimesUp),
229226
}
@@ -242,14 +239,14 @@ pub async fn activation_confirmation(
242239
if let Some(parent) = Path::new(&lock_path).parent() {
243240
fs::create_dir_all(parent)
244241
.await
245-
.map_err(ActivationConfirmationError::CreateConfirmDirError)?;
242+
.map_err(ActivationConfirmationError::CreateConfirmDir)?;
246243
}
247244

248245
debug!("Creating canary file");
249246

250247
fs::File::create(&lock_path)
251248
.await
252-
.map_err(ActivationConfirmationError::CreateConfirmFileError)?;
249+
.map_err(ActivationConfirmationError::CreateConfirmFile)?;
253250

254251
debug!("Creating notify watcher");
255252

@@ -342,20 +339,20 @@ pub async fn wait(temp_path: String, closure: String) -> Result<(), WaitError> {
342339
#[derive(Error, Debug)]
343340
pub enum ActivateError {
344341
#[error("Failed to execute the command for setting profile: {0}")]
345-
SetProfileError(std::io::Error),
342+
SetProfile(std::io::Error),
346343
#[error("The command for setting profile resulted in a bad exit code: {0:?}")]
347-
SetProfileExitError(Option<i32>),
344+
SetProfileExit(Option<i32>),
348345

349346
#[error("Failed to execute the activation script: {0}")]
350-
RunActivateError(std::io::Error),
347+
RunActivate(std::io::Error),
351348
#[error("The activation script resulted in a bad exit code: {0:?}")]
352-
RunActivateExitError(Option<i32>),
349+
RunActivateExit(Option<i32>),
353350

354351
#[error("There was an error de-activating after an error was encountered: {0}")]
355-
DeactivateError(#[from] DeactivateError),
352+
Deactivate(#[from] DeactivateError),
356353

357354
#[error("Failed to get activation confirmation: {0}")]
358-
ActivationConfirmationError(#[from] ActivationConfirmationError),
355+
ActivationConfirmation(#[from] ActivationConfirmationError),
359356
}
360357

361358
pub async fn activate(
@@ -376,14 +373,14 @@ pub async fn activate(
376373
.arg(&closure)
377374
.status()
378375
.await
379-
.map_err(ActivateError::SetProfileError)?;
376+
.map_err(ActivateError::SetProfile)?;
380377
match nix_env_set_exit_status.code() {
381378
Some(0) => (),
382379
a => {
383380
if auto_rollback && !dry_activate {
384381
deactivate(&profile_path).await?;
385382
}
386-
return Err(ActivateError::SetProfileExitError(a));
383+
return Err(ActivateError::SetProfileExit(a));
387384
}
388385
};
389386
}
@@ -402,7 +399,7 @@ pub async fn activate(
402399
.current_dir(activation_location)
403400
.status()
404401
.await
405-
.map_err(ActivateError::RunActivateError)
402+
.map_err(ActivateError::RunActivate)
406403
{
407404
Ok(x) => x,
408405
Err(e) => {
@@ -420,7 +417,7 @@ pub async fn activate(
420417
if auto_rollback {
421418
deactivate(&profile_path).await?;
422419
}
423-
return Err(ActivateError::RunActivateExitError(a));
420+
return Err(ActivateError::RunActivateExit(a));
424421
}
425422
};
426423

@@ -437,7 +434,7 @@ pub async fn activate(
437434
Ok(()) => {}
438435
Err(err) => {
439436
deactivate(&profile_path).await?;
440-
return Err(ActivateError::ActivationConfirmationError(err));
437+
return Err(ActivateError::ActivationConfirmation(err));
441438
}
442439
};
443440
}
@@ -446,12 +443,7 @@ pub async fn activate(
446443
Ok(())
447444
}
448445

449-
#[derive(Error, Debug)]
450-
pub enum RevokeError {
451-
#[error("There was an error de-activating after an error was encountered: {0}")]
452-
DeactivateError(#[from] DeactivateError),
453-
}
454-
async fn revoke(profile_path: String) -> Result<(), RevokeError> {
446+
async fn revoke(profile_path: String) -> Result<(), DeactivateError> {
455447
deactivate(profile_path.as_str()).await?;
456448
Ok(())
457449
}
@@ -462,7 +454,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
462454
let mut signals = Signals::new(&[SIGHUP])?;
463455
std::thread::spawn(move || {
464456
for _ in signals.forever() {
465-
println!("Received NOHUP - ignoring...");
457+
println!("Received SIGHUP - ignoring...");
466458
}
467459
});
468460

@@ -471,7 +463,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
471463
deploy::init_logger(
472464
opts.debug_logs,
473465
opts.log_dir.as_deref(),
474-
match opts.subcmd {
466+
&match opts.subcmd {
475467
SubCommand::Activate(_) => deploy::LoggerType::Activate,
476468
SubCommand::Wait(_) => deploy::LoggerType::Wait,
477469
SubCommand::Revoke(_) => deploy::LoggerType::Revoke,

src/cli.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use std::collections::HashMap;
77
use std::io::{stdin, stdout, Write};
88

9-
use clap::{Clap, ArgMatches, FromArgMatches};
9+
use clap::{ArgMatches, Clap, FromArgMatches};
1010

1111
use crate as deploy;
1212

@@ -127,16 +127,13 @@ async fn check_deployment(
127127
false => Command::new("nix-build"),
128128
};
129129

130-
match supports_flakes {
131-
true => {
132-
check_command.arg("flake").arg("check").arg(repo);
133-
}
134-
false => {
135-
check_command.arg("-E")
130+
if supports_flakes {
131+
check_command.arg("flake").arg("check").arg(repo);
132+
} else {
133+
check_command.arg("-E")
136134
.arg("--no-out-link")
137135
.arg(format!("let r = import {}/.; x = (if builtins.isFunction r then (r {{}}) else r); in if x ? checks then x.checks.${{builtins.currentSystem}} else {{}}", repo));
138-
}
139-
};
136+
}
140137

141138
for extra_arg in extra_build_args {
142139
check_command.arg(extra_arg);
@@ -420,24 +417,24 @@ async fn run_deploy(
420417
(Some(node_name), Some(profile_name)) => {
421418
let node = match data.nodes.get(node_name) {
422419
Some(x) => x,
423-
None => Err(RunDeployError::NodeNotFound(node_name.to_owned()))?,
420+
None => return Err(RunDeployError::NodeNotFound(node_name.clone())),
424421
};
425422
let profile = match node.node_settings.profiles.get(profile_name) {
426423
Some(x) => x,
427-
None => Err(RunDeployError::ProfileNotFound(profile_name.to_owned()))?,
424+
None => return Err(RunDeployError::ProfileNotFound(profile_name.clone())),
428425
};
429426

430427
vec![(
431-
&deploy_flake,
432-
&data,
428+
deploy_flake,
429+
data,
433430
(node_name.as_str(), node),
434431
(profile_name.as_str(), profile),
435432
)]
436433
}
437434
(Some(node_name), None) => {
438435
let node = match data.nodes.get(node_name) {
439436
Some(x) => x,
440-
None => return Err(RunDeployError::NodeNotFound(node_name.to_owned())),
437+
None => return Err(RunDeployError::NodeNotFound(node_name.clone())),
441438
};
442439

443440
let mut profiles_list: Vec<(&str, &deploy::data::Profile)> = Vec::new();
@@ -451,14 +448,12 @@ async fn run_deploy(
451448
let profile = match node.node_settings.profiles.get(profile_name) {
452449
Some(x) => x,
453450
None => {
454-
return Err(RunDeployError::ProfileNotFound(
455-
profile_name.to_owned(),
456-
))
451+
return Err(RunDeployError::ProfileNotFound(profile_name.clone()))
457452
}
458453
};
459454

460455
if !profiles_list.iter().any(|(n, _)| n == profile_name) {
461-
profiles_list.push((&profile_name, profile));
456+
profiles_list.push((profile_name, profile));
462457
}
463458
}
464459

@@ -483,13 +478,13 @@ async fn run_deploy(
483478
Some(x) => x,
484479
None => {
485480
return Err(RunDeployError::ProfileNotFound(
486-
profile_name.to_owned(),
481+
profile_name.clone(),
487482
))
488483
}
489484
};
490485

491486
if !profiles_list.iter().any(|(n, _)| n == profile_name) {
492-
profiles_list.push((&profile_name, profile));
487+
profiles_list.push((profile_name, profile));
493488
}
494489
}
495490

@@ -525,7 +520,7 @@ async fn run_deploy(
525520
node_name,
526521
profile,
527522
profile_name,
528-
&cmd_overrides,
523+
cmd_overrides,
529524
debug_logs,
530525
log_dir.as_deref(),
531526
);
@@ -546,8 +541,8 @@ async fn run_deploy(
546541
supports_flakes,
547542
check_sigs,
548543
repo: deploy_flake.repo,
549-
deploy_data: &deploy_data,
550-
deploy_defs: &deploy_defs,
544+
deploy_data,
545+
deploy_defs,
551546
keep_result,
552547
result_path,
553548
extra_build_args,
@@ -616,13 +611,13 @@ pub async fn run(args: Option<&ArgMatches>) -> Result<(), RunError> {
616611
deploy::init_logger(
617612
opts.debug_logs,
618613
opts.log_dir.as_deref(),
619-
deploy::LoggerType::Deploy,
614+
&deploy::LoggerType::Deploy,
620615
)?;
621616

622617
let deploys = opts
623618
.clone()
624619
.targets
625-
.unwrap_or_else(|| vec![opts.clone().target.unwrap_or(".".to_string())]);
620+
.unwrap_or_else(|| vec![opts.clone().target.unwrap_or_else(|| ".".to_string())]);
626621

627622
let deploy_flakes: Vec<DeployFlake> = deploys
628623
.iter()
@@ -649,7 +644,7 @@ pub async fn run(args: Option<&ArgMatches>) -> Result<(), RunError> {
649644
}
650645

651646
if !opts.skip_checks {
652-
for deploy_flake in deploy_flakes.iter() {
647+
for deploy_flake in &deploy_flakes {
653648
check_deployment(supports_flakes, deploy_flake.repo, &opts.extra_build_args).await?;
654649
}
655650
}

0 commit comments

Comments
 (0)