Files
topgrade/src/windows.rs

82 lines
2.3 KiB
Rust
Raw Normal View History

2018-08-19 14:45:23 +03:00
use super::terminal::Terminal;
2018-08-22 22:01:06 +03:00
use super::utils::{self, which, Check};
2018-06-28 12:16:54 +03:00
use failure;
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]
pub fn run_chocolatey(terminal: &mut Terminal) -> Option<(&'static str, bool)> {
if let Some(choco) = utils::which("choco") {
terminal.print_separator("Chocolatey");
2018-06-28 12:16:54 +03:00
2018-08-19 14:45:23 +03:00
let success = || -> Result<(), failure::Error> {
Command::new(&choco).args(&["upgrade", "all"]).spawn()?.wait()?.check()?;
Ok(())
}().is_ok();
return Some(("Chocolatey", success));
}
None
2018-06-28 12:16:54 +03:00
}
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-22 22:01:06 +03:00
#[must_use]
pub fn update_modules(&self, terminal: &mut Terminal) -> Option<(&'static str, bool)> {
if let Some(powershell) = &self.path {
2018-08-22 22:18:48 +03:00
terminal.print_separator("Powershell Modules Update");
2018-08-22 22:01:06 +03:00
let success = || -> Result<(), failure::Error> {
Command::new(&powershell).arg("Update-Module").spawn()?.wait()?.check()?;
Ok(())
}().is_ok();
2018-08-22 22:18:48 +03:00
return Some(("Powershell Modules Update", success));
}
None
}
#[must_use]
pub fn windows_update(&self, terminal: &mut Terminal) -> Option<(&'static str, bool)> {
if let Some(powershell) = &self.path {
if Self::has_command(&powershell, "Install-WindowsUpdate") {
terminal.print_separator("Windows Update");
let success = || -> Result<(), failure::Error> {
Command::new(&powershell)
.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
}
}