Better error model
This commit is contained in:
46
src/linux.rs
46
src/linux.rs
@@ -1,8 +1,8 @@
|
||||
use super::error::{Error, ErrorKind};
|
||||
use super::executor::Executor;
|
||||
use super::terminal::{print_separator, print_warning};
|
||||
use super::utils::{which, Check};
|
||||
use failure;
|
||||
use failure_derive::Fail;
|
||||
use failure::ResultExt;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use walkdir::WalkDir;
|
||||
@@ -19,17 +19,9 @@ pub enum Distribution {
|
||||
Void,
|
||||
}
|
||||
|
||||
#[derive(Debug, Fail)]
|
||||
#[fail(display = "Unknown Linux Distribution")]
|
||||
struct UnknownLinuxDistribution;
|
||||
|
||||
#[derive(Debug, Fail)]
|
||||
#[fail(display = "Detected Python is not the system Python")]
|
||||
struct NotSystemPython;
|
||||
|
||||
impl Distribution {
|
||||
pub fn detect() -> Result<Self, failure::Error> {
|
||||
let content = fs::read_to_string("/etc/os-release")?;
|
||||
pub fn detect() -> Result<Self, Error> {
|
||||
let content = fs::read_to_string("/etc/os-release").context(ErrorKind::UnknownLinuxDistribution)?;
|
||||
|
||||
if content.contains("Arch") | content.contains("Manjaro") | content.contains("Antergos") {
|
||||
return Ok(Distribution::Arch);
|
||||
@@ -63,7 +55,7 @@ impl Distribution {
|
||||
return Ok(Distribution::Gentoo);
|
||||
}
|
||||
|
||||
Err(UnknownLinuxDistribution.into())
|
||||
Err(ErrorKind::UnknownLinuxDistribution)?
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
@@ -111,7 +103,7 @@ pub fn show_pacnew() {
|
||||
}
|
||||
}
|
||||
|
||||
fn upgrade_arch_linux(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::Error> {
|
||||
fn upgrade_arch_linux(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), Error> {
|
||||
if let Some(yay) = which("yay") {
|
||||
if let Some(python) = which("python") {
|
||||
if python != PathBuf::from("/usr/bin/python") {
|
||||
@@ -120,7 +112,7 @@ fn upgrade_arch_linux(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failu
|
||||
It's dangerous to run yay since Python based AUR packages will be installed in the wrong location",
|
||||
python
|
||||
));
|
||||
return Err(NotSystemPython.into());
|
||||
return Err(ErrorKind::NotSystemPython)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +130,7 @@ It's dangerous to run yay since Python based AUR packages will be installed in t
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn upgrade_redhat(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::Error> {
|
||||
fn upgrade_redhat(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), Error> {
|
||||
if let Some(sudo) = &sudo {
|
||||
Executor::new(&sudo, dry_run)
|
||||
.args(&["/usr/bin/yum", "upgrade"])
|
||||
@@ -152,7 +144,7 @@ fn upgrade_redhat(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn upgrade_opensuse(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::Error> {
|
||||
fn upgrade_opensuse(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), Error> {
|
||||
if let Some(sudo) = &sudo {
|
||||
Executor::new(&sudo, dry_run)
|
||||
.args(&["/usr/bin/zypper", "refresh"])
|
||||
@@ -172,7 +164,7 @@ fn upgrade_opensuse(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn upgrade_void(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::Error> {
|
||||
fn upgrade_void(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), Error> {
|
||||
if let Some(sudo) = &sudo {
|
||||
Executor::new(&sudo, dry_run)
|
||||
.args(&["/usr/bin/xbps-install", "-Su"])
|
||||
@@ -186,7 +178,7 @@ fn upgrade_void(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::Er
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn upgrade_fedora(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::Error> {
|
||||
fn upgrade_fedora(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), Error> {
|
||||
if let Some(sudo) = &sudo {
|
||||
Executor::new(&sudo, dry_run)
|
||||
.args(&["/usr/bin/dnf", "upgrade"])
|
||||
@@ -200,7 +192,7 @@ fn upgrade_fedora(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn upgrade_gentoo(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::Error> {
|
||||
fn upgrade_gentoo(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), Error> {
|
||||
if let Some(sudo) = &sudo {
|
||||
if let Some(layman) = which("layman") {
|
||||
Executor::new(&sudo, dry_run)
|
||||
@@ -236,7 +228,7 @@ fn upgrade_gentoo(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn upgrade_debian(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), failure::Error> {
|
||||
fn upgrade_debian(sudo: &Option<PathBuf>, dry_run: bool) -> Result<(), Error> {
|
||||
if let Some(sudo) = &sudo {
|
||||
Executor::new(&sudo, dry_run)
|
||||
.args(&["/usr/bin/apt", "update"])
|
||||
@@ -262,7 +254,7 @@ pub fn run_needrestart(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&'stati
|
||||
if let Some(needrestart) = which("needrestart") {
|
||||
print_separator("Check for needed restarts");
|
||||
|
||||
let success = || -> Result<(), failure::Error> {
|
||||
let success = || -> Result<(), Error> {
|
||||
Executor::new(&sudo, dry_run)
|
||||
.arg(needrestart)
|
||||
.spawn()?
|
||||
@@ -285,7 +277,7 @@ pub fn run_fwupdmgr(dry_run: bool) -> Option<(&'static str, bool)> {
|
||||
if let Some(fwupdmgr) = which("fwupdmgr") {
|
||||
print_separator("Firmware upgrades");
|
||||
|
||||
let success = || -> Result<(), failure::Error> {
|
||||
let success = || -> Result<(), Error> {
|
||||
Executor::new(&fwupdmgr, dry_run)
|
||||
.arg("refresh")
|
||||
.spawn()?
|
||||
@@ -311,7 +303,7 @@ pub fn flatpak_user_update(dry_run: bool) -> Option<(&'static str, bool)> {
|
||||
if let Some(flatpak) = which("flatpak") {
|
||||
print_separator("Flatpak User Packages");
|
||||
|
||||
let success = || -> Result<(), failure::Error> {
|
||||
let success = || -> Result<(), Error> {
|
||||
Executor::new(&flatpak, dry_run)
|
||||
.args(&["update", "--user", "-y"])
|
||||
.spawn()?
|
||||
@@ -333,7 +325,7 @@ pub fn flatpak_global_update(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&
|
||||
if let Some(flatpak) = which("flatpak") {
|
||||
print_separator("Flatpak Global Packages");
|
||||
|
||||
let success = || -> Result<(), failure::Error> {
|
||||
let success = || -> Result<(), Error> {
|
||||
Executor::new(&sudo, dry_run)
|
||||
.args(&[flatpak.to_str().unwrap(), "update", "-y"])
|
||||
.spawn()?
|
||||
@@ -357,7 +349,7 @@ pub fn run_snap(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&'static str,
|
||||
if PathBuf::from("/var/snapd.socket").exists() {
|
||||
print_separator("snap");
|
||||
|
||||
let success = || -> Result<(), failure::Error> {
|
||||
let success = || -> Result<(), Error> {
|
||||
Executor::new(&sudo, dry_run)
|
||||
.args(&[snap.to_str().unwrap(), "refresh"])
|
||||
.spawn()?
|
||||
@@ -382,7 +374,7 @@ pub fn run_etc_update(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&'static
|
||||
if let Some(etc_update) = which("etc-update") {
|
||||
print_separator("etc-update");
|
||||
|
||||
let success = || -> Result<(), failure::Error> {
|
||||
let success = || -> Result<(), Error> {
|
||||
Executor::new(&sudo, dry_run)
|
||||
.arg(&etc_update.to_str().unwrap())
|
||||
.spawn()?
|
||||
|
||||
Reference in New Issue
Block a user