2018-06-12 21:28:32 +03:00
|
|
|
use super::terminal::Terminal;
|
2018-06-17 14:17:36 +03:00
|
|
|
use super::utils::{which, Check};
|
2018-06-07 16:19:11 +03:00
|
|
|
use failure;
|
|
|
|
|
use std::fs;
|
2018-06-12 21:28:32 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::process::Command;
|
2018-06-07 16:19:11 +03:00
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
pub enum Distribution {
|
|
|
|
|
Arch,
|
|
|
|
|
CentOS,
|
|
|
|
|
Fedora,
|
|
|
|
|
Debian,
|
|
|
|
|
Ubuntu,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Fail)]
|
|
|
|
|
#[fail(display = "Unknown Linux Distribution")]
|
|
|
|
|
struct UnknownLinuxDistribution;
|
|
|
|
|
|
2018-06-12 21:35:04 +03:00
|
|
|
#[derive(Debug, Fail)]
|
|
|
|
|
#[fail(display = "Detected Python is not the system Python")]
|
|
|
|
|
struct NotSystemPython;
|
|
|
|
|
|
2018-06-07 16:19:11 +03:00
|
|
|
impl Distribution {
|
|
|
|
|
pub fn detect() -> Result<Self, failure::Error> {
|
|
|
|
|
let content = fs::read_to_string("/etc/os-release")?;
|
|
|
|
|
|
2018-07-11 07:35:30 +03:00
|
|
|
if content.contains("Arch") | content.contains("Manjaro") | content.contains("Antergos") {
|
2018-06-07 16:19:11 +03:00
|
|
|
return Ok(Distribution::Arch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if content.contains("CentOS") {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Err(UnknownLinuxDistribution.into())
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-12 21:28:32 +03:00
|
|
|
|
2018-07-03 14:33:48 +03:00
|
|
|
pub fn upgrade_arch_linux(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(), failure::Error> {
|
2018-06-17 14:17:36 +03:00
|
|
|
if let Some(yay) = which("yay") {
|
|
|
|
|
if let Some(python) = which("python") {
|
2018-06-12 21:35:04 +03:00
|
|
|
if python != PathBuf::from("/usr/bin/python") {
|
|
|
|
|
terminal.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(NotSystemPython.into());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-12 21:28:32 +03:00
|
|
|
Command::new(yay).spawn()?.wait()?.check()?;
|
2018-07-10 07:29:41 +03:00
|
|
|
} else if let Some(sudo) = &sudo {
|
|
|
|
|
Command::new(&sudo)
|
|
|
|
|
.args(&["/usr/bin/pacman", "-Syu"])
|
|
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.check()?;
|
2018-06-12 21:28:32 +03:00
|
|
|
} else {
|
2018-07-10 07:29:41 +03:00
|
|
|
terminal.print_warning("No sudo or yay detected. Skipping system upgrade");
|
2018-06-12 21:28:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 14:33:48 +03:00
|
|
|
pub fn upgrade_redhat(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(), failure::Error> {
|
2018-06-12 21:28:32 +03:00
|
|
|
if let Some(sudo) = &sudo {
|
|
|
|
|
Command::new(&sudo)
|
2018-07-01 20:24:01 +03:00
|
|
|
.args(&["/usr/bin/yum", "upgrade"])
|
2018-06-12 21:28:32 +03:00
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.check()?;
|
|
|
|
|
} else {
|
|
|
|
|
terminal.print_warning("No sudo detected. Skipping system upgrade");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 14:33:48 +03:00
|
|
|
pub fn upgrade_fedora(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(), failure::Error> {
|
2018-06-12 21:28:32 +03:00
|
|
|
if let Some(sudo) = &sudo {
|
|
|
|
|
Command::new(&sudo)
|
2018-07-01 20:24:01 +03:00
|
|
|
.args(&["/usr/bin/dnf", "upgrade"])
|
2018-06-12 21:28:32 +03:00
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.check()?;
|
|
|
|
|
} else {
|
|
|
|
|
terminal.print_warning("No sudo detected. Skipping system upgrade");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 14:33:48 +03:00
|
|
|
pub fn upgrade_debian(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(), failure::Error> {
|
2018-06-12 21:28:32 +03:00
|
|
|
if let Some(sudo) = &sudo {
|
|
|
|
|
Command::new(&sudo)
|
2018-07-01 20:24:01 +03:00
|
|
|
.args(&["/usr/bin/apt", "update"])
|
2018-06-12 21:28:32 +03:00
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.check()?;
|
|
|
|
|
|
|
|
|
|
Command::new(&sudo)
|
2018-07-01 20:24:01 +03:00
|
|
|
.args(&["/usr/bin/apt", "dist-upgrade"])
|
2018-06-12 21:28:32 +03:00
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.check()?;
|
|
|
|
|
} else {
|
|
|
|
|
terminal.print_warning("No sudo detected. Skipping system upgrade");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2018-06-28 12:16:54 +03:00
|
|
|
|
2018-07-01 20:24:01 +03:00
|
|
|
pub fn run_needrestart(sudo: &PathBuf, needrestart: &PathBuf) -> Result<(), failure::Error> {
|
2018-07-03 14:33:48 +03:00
|
|
|
Command::new(&sudo).arg(needrestart).spawn()?.wait()?.check()?;
|
2018-06-28 12:16:54 +03:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run_fwupdmgr(fwupdmgr: &PathBuf) -> Result<(), failure::Error> {
|
2018-07-03 14:33:48 +03:00
|
|
|
Command::new(&fwupdmgr).arg("refresh").spawn()?.wait()?.check()?;
|
2018-06-28 12:16:54 +03:00
|
|
|
|
2018-07-03 14:33:48 +03:00
|
|
|
Command::new(&fwupdmgr).arg("get-updates").spawn()?.wait()?.check()?;
|
2018-06-28 12:16:54 +03:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run_flatpak(flatpak: &PathBuf) -> Result<(), failure::Error> {
|
2018-08-22 10:44:27 +03:00
|
|
|
Command::new(&flatpak).args(&["update", "-y"]).spawn()?.wait()?.check()?;
|
2018-06-28 12:16:54 +03:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run_snap(sudo: &PathBuf, snap: &PathBuf) -> Result<(), failure::Error> {
|
|
|
|
|
Command::new(&sudo)
|
|
|
|
|
.args(&[snap.to_str().unwrap(), "refresh"])
|
|
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.check()?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|