Files
topgrade/src/windows.rs

135 lines
3.7 KiB
Rust
Raw Normal View History

2018-08-26 16:12:59 +03:00
use super::executor::Executor;
2018-12-05 11:34:08 +02:00
use super::terminal::print_separator;
2018-08-22 22:01:06 +03:00
use super::utils::{self, which, Check};
2018-06-28 12:16:54 +03:00
use failure;
use log::error;
2018-08-22 22:01:06 +03:00
use std::path::PathBuf;
2018-06-28 12:16:54 +03:00
use std::process::Command;
2018-08-19 14:45:23 +03:00
#[must_use]
2018-12-05 11:34:08 +02:00
pub fn run_chocolatey(dry_run: bool) -> Option<(&'static str, bool)> {
2018-08-19 14:45:23 +03:00
if let Some(choco) = utils::which("choco") {
2018-12-05 11:34:08 +02:00
print_separator("Chocolatey");
2018-06-28 12:16:54 +03:00
2018-08-19 14:45:23 +03:00
let success = || -> Result<(), failure::Error> {
2018-08-26 16:12:59 +03:00
Executor::new(&choco, dry_run)
.args(&["upgrade", "all"])
.spawn()?
.wait()?
.check()?;
2018-08-19 14:45:23 +03:00
Ok(())
}().is_ok();
return Some(("Chocolatey", success));
}
None
2018-06-28 12:16:54 +03:00
}
2018-08-22 22:01:06 +03:00
2018-10-17 13:57:30 +03:00
#[must_use]
2018-12-05 11:34:08 +02:00
pub fn run_scoop(dry_run: bool) -> Option<(&'static str, bool)> {
2018-10-17 13:57:30 +03:00
if let Some(scoop) = utils::which("scoop") {
2018-12-05 11:34:08 +02:00
print_separator("Scoop");
2018-10-17 13:57:30 +03:00
let success = || -> Result<(), failure::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
}
2018-08-22 22:01:06 +03:00
pub struct Powershell {
path: Option<PathBuf>,
}
impl Powershell {
pub fn new() -> Self {
Powershell {
path: which("powershell"),
}
}
2018-08-22 22:18:48 +03:00
pub fn has_command(powershell: &PathBuf, command: &str) -> bool {
|| -> Result<(), failure::Error> {
Command::new(&powershell)
.args(&["-Command", &format!("Get-Command {}", command)])
.output()?
.check()?;
Ok(())
}().is_ok()
}
2018-08-23 22:08:04 +03:00
pub fn profile(&self) -> Option<PathBuf> {
if let Some(powershell) = &self.path {
let result = || -> Result<PathBuf, failure::Error> {
let output = Command::new(powershell).args(&["-Command", "echo $profile"]).output()?;
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
}
2018-08-22 22:01:06 +03:00
#[must_use]
2018-12-05 11:34:08 +02:00
pub fn update_modules(&self, dry_run: bool) -> Option<(&'static str, bool)> {
2018-08-22 22:01:06 +03:00
if let Some(powershell) = &self.path {
2018-12-05 11:34:08 +02:00
print_separator("Powershell Modules Update");
2018-08-22 22:01:06 +03:00
let success = || -> Result<(), failure::Error> {
2018-08-26 16:12:59 +03:00
Executor::new(&powershell, dry_run)
.arg("Update-Module")
.spawn()?
.wait()?
.check()?;
2018-08-22 22:01:06 +03:00
Ok(())
}().is_ok();
2018-08-22 22:18:48 +03:00
return Some(("Powershell Modules Update", success));
}
None
}
#[must_use]
2018-12-05 11:34:08 +02:00
pub fn windows_update(&self, dry_run: bool) -> Option<(&'static str, bool)> {
2018-08-22 22:18:48 +03:00
if let Some(powershell) = &self.path {
if Self::has_command(&powershell, "Install-WindowsUpdate") {
2018-12-05 11:34:08 +02:00
print_separator("Windows Update");
2018-08-22 22:18:48 +03:00
let success = || -> Result<(), failure::Error> {
2018-08-26 16:12:59 +03:00
Executor::new(&powershell, dry_run)
2018-08-22 22:18:48 +03:00
.args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"])
.spawn()?
.wait()?
.check()?;
Ok(())
}().is_ok();
return Some(("Windows Update", success));
}
2018-08-22 22:01:06 +03:00
}
None
}
}