refactor(apt): extract detect_apt() function
This commit is contained in:
@@ -505,29 +505,34 @@ fn upgrade_gentoo(ctx: &ExecutionContext) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn upgrade_debian(ctx: &ExecutionContext) -> Result<()> {
|
enum AptKind {
|
||||||
let apt = which("apt-fast")
|
AptFast,
|
||||||
.or_else(|| {
|
Mist,
|
||||||
if which("mist").is_some() {
|
Nala,
|
||||||
Some(PathBuf::from("mist"))
|
AptGet,
|
||||||
} else {
|
}
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.or_else(|| {
|
|
||||||
if Path::new("/usr/bin/nala").exists() {
|
|
||||||
Some(Path::new("/usr/bin/nala").to_path_buf())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| PathBuf::from("apt-get"));
|
|
||||||
|
|
||||||
let is_mist = apt.ends_with("mist");
|
fn detect_apt() -> Result<(AptKind, PathBuf)> {
|
||||||
let is_nala = apt.ends_with("nala");
|
use AptKind::*;
|
||||||
|
|
||||||
|
if let Some(apt_fast) = which("apt-fast") {
|
||||||
|
Ok((AptFast, apt_fast))
|
||||||
|
} else if let Some(mist) = which("mist") {
|
||||||
|
Ok((Mist, mist))
|
||||||
|
} else if Path::new("/usr/bin/nala").exists() {
|
||||||
|
Ok((Nala, Path::new("/usr/bin/nala").to_path_buf()))
|
||||||
|
} else {
|
||||||
|
Ok((AptGet, require("apt-get")?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn upgrade_debian(ctx: &ExecutionContext) -> Result<()> {
|
||||||
|
use AptKind::*;
|
||||||
|
|
||||||
|
let (kind, apt) = detect_apt()?;
|
||||||
|
|
||||||
// MIST does not require `sudo`
|
// MIST does not require `sudo`
|
||||||
if is_mist {
|
if matches!(kind, Mist) {
|
||||||
ctx.execute(&apt).arg("update").status_checked()?;
|
ctx.execute(&apt).arg("update").status_checked()?;
|
||||||
ctx.execute(&apt).arg("upgrade").status_checked()?;
|
ctx.execute(&apt).arg("upgrade").status_checked()?;
|
||||||
|
|
||||||
@@ -537,14 +542,14 @@ fn upgrade_debian(ctx: &ExecutionContext) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let sudo = ctx.require_sudo()?;
|
let sudo = ctx.require_sudo()?;
|
||||||
if !is_nala {
|
if !matches!(kind, Nala) {
|
||||||
sudo.execute(ctx, &apt)?
|
sudo.execute(ctx, &apt)?
|
||||||
.arg("update")
|
.arg("update")
|
||||||
.status_checked_with_codes(&[0, 100])?;
|
.status_checked_with_codes(&[0, 100])?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut command = sudo.execute(ctx, &apt)?;
|
let mut command = sudo.execute(ctx, &apt)?;
|
||||||
if is_nala {
|
if matches!(kind, Nala) {
|
||||||
command.arg("upgrade");
|
command.arg("upgrade");
|
||||||
} else {
|
} else {
|
||||||
command.arg("dist-upgrade");
|
command.arg("dist-upgrade");
|
||||||
@@ -797,26 +802,8 @@ fn should_skip_needrestart() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if matches!(distribution, Distribution::Debian) {
|
if matches!(distribution, Distribution::Debian) {
|
||||||
let apt = which("apt-fast")
|
let (apt_kind, _) = detect_apt()?;
|
||||||
.or_else(|| {
|
if matches!(apt_kind, AptKind::Nala) {
|
||||||
if which("mist").is_some() {
|
|
||||||
Some(PathBuf::from("mist"))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.or_else(|| {
|
|
||||||
if Path::new("/usr/bin/nala").exists() {
|
|
||||||
Some(Path::new("/usr/bin/nala").to_path_buf())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.unwrap_or_else(|| PathBuf::from("apt-get"));
|
|
||||||
|
|
||||||
let is_nala = apt.ends_with("nala");
|
|
||||||
|
|
||||||
if is_nala {
|
|
||||||
return Err(SkipStep(String::from(msg)).into());
|
return Err(SkipStep(String::from(msg)).into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user