Modules refactor

This commit is contained in:
Roey Darwish Dror
2018-12-15 21:52:21 +02:00
parent 66c6338b42
commit 47a271af47
14 changed files with 63 additions and 61 deletions

64
src/steps/os/freebsd.rs Normal file
View File

@@ -0,0 +1,64 @@
use crate::error::{Error, ErrorKind};
use crate::executor::Executor;
use crate::terminal::{print_separator, print_warning};
use crate::utils::Check;
use failure::ResultExt;
use std::path::PathBuf;
use std::process::Command;
#[must_use]
pub fn upgrade_freebsd(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&'static str, bool)> {
print_separator("FreeBSD Update");
if let Some(sudo) = sudo {
let success = || -> Result<(), Error> {
Executor::new(sudo, dry_run)
.args(&["/usr/sbin/freebsd-update", "fetch", "install"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
Some(("FreeBSD Update", success))
} else {
print_warning("No sudo or yay detected. Skipping system upgrade");
None
}
}
#[must_use]
pub fn upgrade_packages(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&'static str, bool)> {
print_separator("FreeBSD Packages");
if let Some(sudo) = sudo {
let success = || -> Result<(), Error> {
Executor::new(sudo, dry_run)
.args(&["/usr/sbin/pkg", "upgrade"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
Some(("FreeBSD Packages", success))
} else {
print_warning("No sudo or yay detected. Skipping package upgrade");
None
}
}
pub fn audit_packages(sudo: &Option<PathBuf>) -> Result<(), Error> {
if let Some(sudo) = sudo {
println!();
Command::new(sudo)
.args(&["/usr/sbin/pkg", "audit", "-Fr"])
.spawn()
.context(ErrorKind::ProcessExecution)?
.wait()
.context(ErrorKind::ProcessExecution)?;
}
Ok(())
}

407
src/steps/os/linux.rs Normal file
View File

@@ -0,0 +1,407 @@
use crate::error::{Error, ErrorKind};
use crate::executor::Executor;
use crate::terminal::{print_separator, print_warning};
use crate::utils::{which, Check};
use failure::ResultExt;
use std::fs;
use std::path::PathBuf;
use walkdir::WalkDir;
#[derive(Copy, Clone, Debug)]
pub enum Distribution {
Arch,
CentOS,
Fedora,
Debian,
Ubuntu,
Gentoo,
OpenSuse,
Void,
}
impl Distribution {
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);
}
if content.contains("CentOS") || content.contains("Oracle Linux") {
return Ok(Distribution::CentOS);
}
if content.contains("Fedora") {
return Ok(Distribution::Fedora);
}
if content.contains("Ubuntu") {
return Ok(Distribution::Ubuntu);
}
if content.contains("Debian") {
return Ok(Distribution::Debian);
}
if content.contains("openSUSE") {
return Ok(Distribution::OpenSuse);
}
if content.contains("void") {
return Ok(Distribution::Void);
}
if PathBuf::from("/etc/gentoo-release").exists() {
return Ok(Distribution::Gentoo);
}
Err(ErrorKind::UnknownLinuxDistribution)?
}
#[must_use]
pub fn upgrade(self, sudo: &Option<PathBuf>, cleanup: bool, dry_run: bool) -> Option<(&'static str, bool)> {
print_separator("System update");
let success = match self {
Distribution::Arch => upgrade_arch_linux(&sudo, dry_run),
Distribution::CentOS => upgrade_redhat(&sudo, dry_run),
Distribution::Fedora => upgrade_fedora(&sudo, dry_run),
Distribution::Ubuntu | Distribution::Debian => upgrade_debian(&sudo, cleanup, dry_run),
Distribution::Gentoo => upgrade_gentoo(&sudo, dry_run),
Distribution::OpenSuse => upgrade_opensuse(&sudo, dry_run),
Distribution::Void => upgrade_void(&sudo, dry_run),
};
Some(("System update", success.is_ok()))
}
pub fn show_summary(self) {
if let Distribution::Arch = self {
show_pacnew();
}
}
}
pub fn show_pacnew() {
let mut iter = WalkDir::new("/etc")
.into_iter()
.filter_map(|e| e.ok())
.filter(|f| {
f.path()
.extension()
.filter(|ext| ext == &"pacnew" || ext == &"pacsave")
.is_some()
})
.peekable();
if iter.peek().is_some() {
println!("\nPacman backup configuration files found:");
for entry in iter {
println!("{}", entry.path().display());
}
}
}
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") {
print_warning(format!(
"Python detected at {:?}, which is probably not the system Python.
It's dangerous to run yay since Python based AUR packages will be installed in the wrong location",
python
));
return Err(ErrorKind::NotSystemPython)?;
}
}
Executor::new(yay, dry_run).spawn()?.wait()?.check()?;
} else if let Some(sudo) = &sudo {
Executor::new(&sudo, dry_run)
.args(&["/usr/bin/pacman", "-Syu"])
.spawn()?
.wait()?
.check()?;
} else {
print_warning("No sudo or yay detected. Skipping system upgrade");
}
Ok(())
}
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"])
.spawn()?
.wait()?
.check()?;
} else {
print_warning("No sudo detected. Skipping system upgrade");
}
Ok(())
}
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"])
.spawn()?
.wait()?
.check()?;
Executor::new(&sudo, dry_run)
.args(&["/usr/bin/zypper", "dist-upgrade"])
.spawn()?
.wait()?
.check()?;
} else {
print_warning("No sudo detected. Skipping system upgrade");
}
Ok(())
}
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"])
.spawn()?
.wait()?
.check()?;
} else {
print_warning("No sudo detected. Skipping system upgrade");
}
Ok(())
}
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"])
.spawn()?
.wait()?
.check()?;
} else {
print_warning("No sudo detected. Skipping system upgrade");
}
Ok(())
}
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)
.arg(layman)
.args(&["-s", "ALL"])
.spawn()?
.wait()?
.check()?;
}
println!("Syncing portage");
Executor::new(&sudo, dry_run)
.arg("/usr/bin/emerge")
.args(&["-q", "--sync"])
.spawn()?
.wait()?
.check()?;
if let Some(eix_update) = which("eix-update") {
Executor::new(&sudo, dry_run).arg(eix_update).spawn()?.wait()?.check()?;
}
Executor::new(&sudo, dry_run)
.arg("/usr/bin/emerge")
.args(&["-uDNa", "world"])
.spawn()?
.wait()?
.check()?;
} else {
print_warning("No sudo detected. Skipping system upgrade");
}
Ok(())
}
fn upgrade_debian(sudo: &Option<PathBuf>, cleanup: bool, dry_run: bool) -> Result<(), Error> {
if let Some(sudo) = &sudo {
Executor::new(&sudo, dry_run)
.args(&["/usr/bin/apt", "update"])
.spawn()?
.wait()?
.check()?;
Executor::new(&sudo, dry_run)
.args(&["/usr/bin/apt", "dist-upgrade"])
.spawn()?
.wait()?
.check()?;
if cleanup {
Executor::new(&sudo, dry_run)
.args(&["/usr/bin/apt", "clean"])
.spawn()?
.wait()?
.check()?;
Executor::new(&sudo, dry_run)
.args(&["/usr/bin/apt", "autoremove"])
.spawn()?
.wait()?
.check()?;
}
} else {
print_warning("No sudo detected. Skipping system upgrade");
}
Ok(())
}
#[must_use]
pub fn run_needrestart(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(sudo) = sudo {
if let Some(needrestart) = which("needrestart") {
print_separator("Check for needed restarts");
let success = || -> Result<(), Error> {
Executor::new(&sudo, dry_run)
.arg(needrestart)
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("Restarts", success));
}
}
None
}
#[must_use]
pub fn run_fwupdmgr(dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(fwupdmgr) = which("fwupdmgr") {
print_separator("Firmware upgrades");
let success = || -> Result<(), Error> {
Executor::new(&fwupdmgr, dry_run)
.arg("refresh")
.spawn()?
.wait()?
.check()?;
Executor::new(&fwupdmgr, dry_run)
.arg("get-updates")
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("Firmware upgrade", success));
}
None
}
#[must_use]
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<(), Error> {
Executor::new(&flatpak, dry_run)
.args(&["update", "--user", "-y"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("Flatpak User Packages", success));
}
None
}
#[must_use]
pub fn flatpak_global_update(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(sudo) = sudo {
if let Some(flatpak) = which("flatpak") {
print_separator("Flatpak Global Packages");
let success = || -> Result<(), Error> {
Executor::new(&sudo, dry_run)
.args(&[flatpak.to_str().unwrap(), "update", "-y"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("Flatpak Global Packages", success));
}
}
None
}
#[must_use]
pub fn run_snap(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(sudo) = sudo {
if let Some(snap) = which("snap") {
if PathBuf::from("/var/snapd.socket").exists() {
print_separator("snap");
let success = || -> Result<(), Error> {
Executor::new(&sudo, dry_run)
.args(&[snap.to_str().unwrap(), "refresh"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("snap", success));
}
}
}
None
}
#[must_use]
pub fn run_etc_update(sudo: &Option<PathBuf>, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(sudo) = sudo {
if let Some(etc_update) = which("etc-update") {
print_separator("etc-update");
let success = || -> Result<(), Error> {
Executor::new(&sudo, dry_run)
.arg(&etc_update.to_str().unwrap())
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("etc-update", success));
}
}
None
}

21
src/steps/os/macos.rs Normal file
View File

@@ -0,0 +1,21 @@
use crate::error::Error;
use crate::executor::Executor;
use crate::terminal::print_separator;
use crate::utils::Check;
#[must_use]
pub fn upgrade_macos(dry_run: bool) -> Option<(&'static str, bool)> {
print_separator("App Store");
let success = || -> Result<(), Error> {
Executor::new("softwareupdate", dry_run)
.args(&["--install", "--all"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
Some(("App Store", success))
}

10
src/steps/os/mod.rs Normal file
View File

@@ -0,0 +1,10 @@
#[cfg(target_os = "freebsd")]
pub mod freebsd;
#[cfg(target_os = "linux")]
pub mod linux;
#[cfg(target_os = "macos")]
pub mod macos;
#[cfg(unix)]
pub mod unix;
#[cfg(target_os = "windows")]
pub mod windows;

101
src/steps/os/unix.rs Normal file
View File

@@ -0,0 +1,101 @@
use crate::error::Error;
use crate::executor::Executor;
use crate::terminal::print_separator;
use crate::utils::{which, Check};
use directories::BaseDirs;
pub fn run_zplug(base_dirs: &BaseDirs, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(zsh) = which("zsh") {
if base_dirs.home_dir().join(".zplug").exists() {
print_separator("zplug");
let success = || -> Result<(), Error> {
Executor::new(zsh, dry_run)
.args(&["-c", "source ~/.zshrc && zplug update"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("zplug", success));
}
}
None
}
pub fn run_fisher(base_dirs: &BaseDirs, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(fish) = which("fish") {
if base_dirs.home_dir().join(".config/fish/functions/fisher.fish").exists() {
print_separator("fisher");
let success = || -> Result<(), Error> {
Executor::new(&fish, dry_run)
.args(&["-c", "fisher self-update"])
.spawn()?
.wait()?
.check()?;
Executor::new(&fish, dry_run)
.args(&["-c", "fisher"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("fisher", success));
}
}
None
}
#[must_use]
pub fn run_homebrew(cleanup: bool, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(brew) = which("brew") {
print_separator("Brew");
let inner = || -> Result<(), Error> {
Executor::new(&brew, dry_run).arg("update").spawn()?.wait()?.check()?;
Executor::new(&brew, dry_run).arg("upgrade").spawn()?.wait()?.check()?;
if cleanup {
Executor::new(&brew, dry_run).arg("cleanup").spawn()?.wait()?.check()?;
}
Ok(())
};
return Some(("Brew", inner().is_ok()));
}
None
}
#[must_use]
pub fn run_nix(dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(nix) = which("nix") {
if let Some(nix_env) = which("nix-env") {
print_separator("Nix");
let inner = || -> Result<(), Error> {
Executor::new(&nix, dry_run)
.arg("upgrade-nix")
.spawn()?
.wait()?
.check()?;
Executor::new(&nix_env, dry_run)
.arg("--upgrade")
.spawn()?
.wait()?
.check()?;
Ok(())
};
return Some(("Nix", inner().is_ok()));
}
}
None
}

144
src/steps/os/windows.rs Normal file
View File

@@ -0,0 +1,144 @@
use crate::error::{Error, ErrorKind};
use crate::executor::Executor;
use crate::terminal::print_separator;
use crate::utils::{self, which, Check};
use failure::ResultExt;
use log::error;
use std::path::PathBuf;
use std::process::Command;
#[must_use]
pub fn run_chocolatey(dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(choco) = utils::which("choco") {
print_separator("Chocolatey");
let success = || -> Result<(), Error> {
Executor::new(&choco, dry_run)
.args(&["upgrade", "all"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("Chocolatey", success));
}
None
}
#[must_use]
pub fn run_scoop(dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(scoop) = utils::which("scoop") {
print_separator("Scoop");
let success = || -> Result<(), Error> {
Executor::new(&scoop, dry_run)
.args(&["update"])
.spawn()?
.wait()?
.check()?;
Executor::new(&scoop, dry_run)
.args(&["update", "*"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("Scoop", success));
}
None
}
pub struct Powershell {
path: Option<PathBuf>,
}
impl Powershell {
pub fn new() -> Self {
Powershell {
path: which("powershell"),
}
}
pub fn has_command(powershell: &PathBuf, command: &str) -> bool {
|| -> Result<(), Error> {
Command::new(&powershell)
.args(&["-Command", &format!("Get-Command {}", command)])
.output()
.context(ErrorKind::ProcessExecution)?
.check()?;
Ok(())
}()
.is_ok()
}
pub fn profile(&self) -> Option<PathBuf> {
if let Some(powershell) = &self.path {
let result = || -> Result<PathBuf, Error> {
let output = Command::new(powershell)
.args(&["-Command", "echo $profile"])
.output()
.context(ErrorKind::ProcessExecution)?;
output.status.check()?;
Ok(PathBuf::from(
String::from_utf8_lossy(&output.stdout).trim().to_string(),
))
}();
match result {
Err(e) => error!("Error getting Powershell profile: {}", e),
Ok(path) => return Some(path),
}
}
None
}
#[must_use]
pub fn update_modules(&self, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(powershell) = &self.path {
print_separator("Powershell Modules Update");
let success = || -> Result<(), Error> {
Executor::new(&powershell, dry_run)
.arg("Update-Module")
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("Powershell Modules Update", success));
}
None
}
#[must_use]
pub fn windows_update(&self, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(powershell) = &self.path {
if Self::has_command(&powershell, "Install-WindowsUpdate") {
print_separator("Windows Update");
let success = || -> Result<(), Error> {
Executor::new(&powershell, dry_run)
.args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"])
.spawn()?
.wait()?
.check()?;
Ok(())
}()
.is_ok();
return Some(("Windows Update", success));
}
}
None
}
}