2018-12-15 21:52:21 +02:00
|
|
|
use crate::error::{Error, ErrorKind};
|
2018-12-31 14:05:15 +02:00
|
|
|
use crate::executor::RunType;
|
2018-12-15 21:52:21 +02:00
|
|
|
use crate::terminal::print_separator;
|
|
|
|
|
use crate::utils::{self, which, Check};
|
2018-12-11 16:43:26 +02:00
|
|
|
use failure::ResultExt;
|
2018-11-18 11:52:37 +02:00
|
|
|
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-31 13:26:17 +02:00
|
|
|
pub fn run_chocolatey(run_type: RunType) -> 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-12-11 16:43:26 +02:00
|
|
|
let success = || -> Result<(), Error> {
|
2018-12-31 22:00:34 +02:00
|
|
|
run_type.execute(&choco).args(&["upgrade", "all"]).check_run()?;
|
2018-08-19 14:45:23 +03:00
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok();
|
2018-08-19 14:45:23 +03:00
|
|
|
|
|
|
|
|
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-31 13:26:17 +02:00
|
|
|
pub fn run_scoop(run_type: RunType) -> 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
|
|
|
|
2018-12-11 16:43:26 +02:00
|
|
|
let success = || -> Result<(), Error> {
|
2018-12-31 22:00:34 +02:00
|
|
|
run_type.execute(&scoop).args(&["update"]).check_run()?;
|
|
|
|
|
run_type.execute(&scoop).args(&["update", "*"]).check_run()?;
|
2018-10-17 13:57:30 +03:00
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok();
|
2018-10-17 13:57:30 +03:00
|
|
|
|
|
|
|
|
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 {
|
2018-12-11 16:43:26 +02:00
|
|
|
|| -> Result<(), Error> {
|
2018-08-22 22:18:48 +03:00
|
|
|
Command::new(&powershell)
|
|
|
|
|
.args(&["-Command", &format!("Get-Command {}", command)])
|
2018-12-11 16:43:26 +02:00
|
|
|
.output()
|
|
|
|
|
.context(ErrorKind::ProcessExecution)?
|
2018-08-22 22:18:48 +03:00
|
|
|
.check()?;
|
|
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok()
|
2018-08-22 22:18:48 +03:00
|
|
|
}
|
|
|
|
|
|
2018-08-23 22:08:04 +03:00
|
|
|
pub fn profile(&self) -> Option<PathBuf> {
|
|
|
|
|
if let Some(powershell) = &self.path {
|
2018-12-11 16:43:26 +02:00
|
|
|
let result = || -> Result<PathBuf, Error> {
|
|
|
|
|
let output = Command::new(powershell)
|
|
|
|
|
.args(&["-Command", "echo $profile"])
|
|
|
|
|
.output()
|
|
|
|
|
.context(ErrorKind::ProcessExecution)?;
|
2018-08-23 22:08:04 +03:00
|
|
|
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-31 13:26:17 +02:00
|
|
|
pub fn update_modules(&self, run_type: RunType) -> 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
|
|
|
|
2018-12-11 16:43:26 +02:00
|
|
|
let success = || -> Result<(), Error> {
|
2018-12-31 22:00:34 +02:00
|
|
|
run_type.execute(&powershell).arg("Update-Module").check_run()?;
|
2018-08-22 22:01:06 +03:00
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok();
|
2018-08-22 22:01:06 +03:00
|
|
|
|
2018-08-22 22:18:48 +03:00
|
|
|
return Some(("Powershell Modules Update", success));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
2018-12-31 13:26:17 +02:00
|
|
|
pub fn windows_update(&self, run_type: RunType) -> 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
|
|
|
|
2018-12-11 16:43:26 +02:00
|
|
|
let success = || -> Result<(), Error> {
|
2018-12-31 13:34:56 +02:00
|
|
|
run_type
|
|
|
|
|
.execute(&powershell)
|
2018-08-22 22:18:48 +03:00
|
|
|
.args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"])
|
2018-12-31 22:00:34 +02:00
|
|
|
.check_run()?;
|
2018-08-22 22:18:48 +03:00
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok();
|
2018-08-22 22:18:48 +03:00
|
|
|
|
|
|
|
|
return Some(("Windows Update", success));
|
|
|
|
|
}
|
2018-08-22 22:01:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|