Dry run (fixes #22)

This commit is contained in:
Roey Darwish Dror
2018-08-26 16:12:59 +03:00
parent 043a2bc89f
commit 71c071d5db
10 changed files with 297 additions and 106 deletions

121
src/executor.rs Normal file
View File

@@ -0,0 +1,121 @@
use super::utils::Check;
use failure;
use std::ffi::{OsStr, OsString};
use std::io;
use std::path::Path;
use std::process::{Child, Command, ExitStatus};
pub enum Executor {
Wet(Command),
Dry(DryCommand),
}
impl Executor {
pub fn new<S: AsRef<OsStr>>(program: S, dry: bool) -> Self {
match dry {
false => Executor::Wet(Command::new(program)),
true => Executor::Dry(DryCommand {
program: program.as_ref().into(),
..Default::default()
}),
}
}
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Executor {
match self {
Executor::Wet(c) => {
c.arg(arg);
}
Executor::Dry(c) => {
c.args.push(arg.as_ref().into());
}
}
self
}
pub fn args<I, S>(&mut self, args: I) -> &mut Executor
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
match self {
Executor::Wet(c) => {
c.args(args);
}
Executor::Dry(c) => {
c.args.extend(args.into_iter().map(|arg| arg.as_ref().into()));
}
}
self
}
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Executor {
match self {
Executor::Wet(c) => {
c.current_dir(dir);
}
Executor::Dry(c) => c.directory = Some(dir.as_ref().into()),
}
self
}
pub fn spawn(&mut self) -> Result<ExecutorChild, io::Error> {
match self {
Executor::Wet(c) => c.spawn().map(|c| ExecutorChild::Wet(c)),
Executor::Dry(c) => {
print!(
"Dry running: {} {}",
c.program.to_string_lossy(),
c.args
.iter()
.map(|a| String::from(a.to_string_lossy()))
.collect::<Vec<String>>()
.join(" ")
);
match &c.directory {
Some(dir) => println!(" in {}", dir.to_string_lossy()),
None => println!(),
};
Ok(ExecutorChild::Dry)
}
}
}
}
#[derive(Default)]
pub struct DryCommand {
program: OsString,
args: Vec<OsString>,
directory: Option<OsString>,
}
pub enum ExecutorChild {
Wet(Child),
Dry,
}
impl ExecutorChild {
pub fn wait(&mut self) -> Result<ExecutorExitStatus, io::Error> {
match self {
ExecutorChild::Wet(c) => c.wait().map(|s| ExecutorExitStatus::Wet(s)),
ExecutorChild::Dry => Ok(ExecutorExitStatus::Dry),
}
}
}
pub enum ExecutorExitStatus {
Wet(ExitStatus),
Dry,
}
impl Check for ExecutorExitStatus {
fn check(self) -> Result<(), failure::Error> {
match self {
ExecutorExitStatus::Wet(e) => e.check(),
ExecutorExitStatus::Dry => Ok(()),
}
}
}

View File

@@ -1,19 +1,18 @@
use super::executor::Executor;
use super::terminal::Terminal; use super::terminal::Terminal;
use super::utils; use super::utils::{self, Check, PathExt};
use super::utils::{Check, PathExt};
use directories::BaseDirs; use directories::BaseDirs;
use failure::Error; use failure::Error;
use std::process::Command;
const EMACS_UPGRADE: &str = include_str!("emacs.el"); const EMACS_UPGRADE: &str = include_str!("emacs.el");
#[must_use] #[must_use]
pub fn run_cargo_update(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_cargo_update(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(cargo_update) = base_dirs.home_dir().join(".cargo/bin/cargo-install-update").if_exists() { if let Some(cargo_update) = base_dirs.home_dir().join(".cargo/bin/cargo-install-update").if_exists() {
terminal.print_separator("Cargo"); terminal.print_separator("Cargo");
let success = || -> Result<(), Error> { let success = || -> Result<(), Error> {
Command::new(cargo_update) Executor::new(cargo_update, dry_run)
.args(&["install-update", "--git", "--all"]) .args(&["install-update", "--git", "--all"])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -29,13 +28,13 @@ pub fn run_cargo_update(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option
} }
#[must_use] #[must_use]
pub fn run_emacs(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_emacs(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(emacs) = utils::which("emacs") { if let Some(emacs) = utils::which("emacs") {
if let Some(init_file) = base_dirs.home_dir().join(".emacs.d/init.el").if_exists() { if let Some(init_file) = base_dirs.home_dir().join(".emacs.d/init.el").if_exists() {
terminal.print_separator("Emacs"); terminal.print_separator("Emacs");
let success = || -> Result<(), Error> { let success = || -> Result<(), Error> {
Command::new(&emacs) Executor::new(&emacs, dry_run)
.args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE]) .args(&["--batch", "-l", init_file.to_str().unwrap(), "--eval", EMACS_UPGRADE])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -51,12 +50,12 @@ pub fn run_emacs(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'sta
} }
#[must_use] #[must_use]
pub fn run_apm(terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_apm(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(apm) = utils::which("apm") { if let Some(apm) = utils::which("apm") {
terminal.print_separator("Atom Package Manager"); terminal.print_separator("Atom Package Manager");
let success = || -> Result<(), Error> { let success = || -> Result<(), Error> {
Command::new(&apm) Executor::new(&apm, dry_run)
.args(&["upgrade", "--confirm=false"]) .args(&["upgrade", "--confirm=false"])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -72,20 +71,20 @@ pub fn run_apm(terminal: &mut Terminal) -> Option<(&'static str, bool)> {
} }
#[must_use] #[must_use]
pub fn run_rustup(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_rustup(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(rustup) = utils::which("rustup") { if let Some(rustup) = utils::which("rustup") {
terminal.print_separator("rustup"); terminal.print_separator("rustup");
let success = || -> Result<(), Error> { let success = || -> Result<(), Error> {
if rustup.is_descendant_of(base_dirs.home_dir()) { if rustup.is_descendant_of(base_dirs.home_dir()) {
Command::new(&rustup) Executor::new(&rustup, dry_run)
.args(&["self", "update"]) .args(&["self", "update"])
.spawn()? .spawn()?
.wait()? .wait()?
.check()?; .check()?;
} }
Command::new(&rustup).arg("update").spawn()?.wait()?.check()?; Executor::new(&rustup, dry_run).arg("update").spawn()?.wait()?.check()?;
Ok(()) Ok(())
}().is_ok(); }().is_ok();
@@ -96,9 +95,14 @@ pub fn run_rustup(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'st
} }
#[must_use] #[must_use]
pub fn run_custom_command(name: &str, command: &str, terminal: &mut Terminal) -> Result<(), Error> { pub fn run_custom_command(name: &str, command: &str, terminal: &mut Terminal, dry_run: bool) -> Result<(), Error> {
terminal.print_separator(name); terminal.print_separator(name);
Command::new("sh").arg("-c").arg(command).spawn()?.wait()?.check()?; Executor::new("sh", dry_run)
.arg("-c")
.arg(command)
.spawn()?
.wait()?
.check()?;
Ok(()) Ok(())
} }

View File

@@ -1,3 +1,4 @@
use super::executor::Executor;
use super::terminal::Terminal; use super::terminal::Terminal;
use super::utils::{which, Check}; use super::utils::{which, Check};
use failure::Error; use failure::Error;
@@ -34,8 +35,7 @@ impl Git {
if let Some(git) = &self.git { if let Some(git) = &self.git {
let output = Command::new(&git) let output = Command::new(&git)
.arg("rev-parse") .args(&["rev-parse", "--show-toplevel"])
.arg("--show-toplevel")
.current_dir(path) .current_dir(path)
.output(); .output();
@@ -57,7 +57,7 @@ impl Git {
None None
} }
pub fn pull<P: AsRef<Path>>(&self, path: P, terminal: &mut Terminal) -> Option<(String, bool)> { pub fn pull<P: AsRef<Path>>(&self, path: P, terminal: &mut Terminal, dry_run: bool) -> Option<(String, bool)> {
let path = path.as_ref(); let path = path.as_ref();
terminal.print_separator(format!("Pulling {}", path.display())); terminal.print_separator(format!("Pulling {}", path.display()));
@@ -65,20 +65,15 @@ impl Git {
let git = self.git.as_ref().unwrap(); let git = self.git.as_ref().unwrap();
let success = || -> Result<(), Error> { let success = || -> Result<(), Error> {
Command::new(git) Executor::new(git, dry_run)
.arg("pull") .args(&["pull", "--rebase", "--autostash"])
.arg("--rebase")
.arg("--autostash")
.current_dir(&path) .current_dir(&path)
.spawn()? .spawn()?
.wait()? .wait()?
.check()?; .check()?;
Command::new(git) Executor::new(git, dry_run)
.arg("submodule") .args(&["submodule", "update", "--init", "--recursive"])
.arg("update")
.arg("--init")
.arg("--recursive")
.current_dir(&path) .current_dir(&path)
.spawn()? .spawn()?
.wait()? .wait()?

View File

@@ -1,9 +1,9 @@
use super::executor::Executor;
use super::terminal::Terminal; use super::terminal::Terminal;
use super::utils::{which, Check}; use super::utils::{which, Check};
use failure; use failure;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command;
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum Distribution { pub enum Distribution {
@@ -50,7 +50,7 @@ impl Distribution {
} }
} }
fn upgrade_arch_linux(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(), failure::Error> { fn upgrade_arch_linux(sudo: &Option<PathBuf>, terminal: &mut Terminal, dry_run: bool) -> Result<(), failure::Error> {
if let Some(yay) = which("yay") { if let Some(yay) = which("yay") {
if let Some(python) = which("python") { if let Some(python) = which("python") {
if python != PathBuf::from("/usr/bin/python") { if python != PathBuf::from("/usr/bin/python") {
@@ -63,9 +63,9 @@ It's dangerous to run yay since Python based AUR packages will be installed in t
} }
} }
Command::new(yay).spawn()?.wait()?.check()?; Executor::new(yay, dry_run).spawn()?.wait()?.check()?;
} else if let Some(sudo) = &sudo { } else if let Some(sudo) = &sudo {
Command::new(&sudo) Executor::new(&sudo, dry_run)
.args(&["/usr/bin/pacman", "-Syu"]) .args(&["/usr/bin/pacman", "-Syu"])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -77,9 +77,9 @@ It's dangerous to run yay since Python based AUR packages will be installed in t
Ok(()) Ok(())
} }
fn upgrade_redhat(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(), failure::Error> { fn upgrade_redhat(sudo: &Option<PathBuf>, terminal: &mut Terminal, dry_run: bool) -> Result<(), failure::Error> {
if let Some(sudo) = &sudo { if let Some(sudo) = &sudo {
Command::new(&sudo) Executor::new(&sudo, dry_run)
.args(&["/usr/bin/yum", "upgrade"]) .args(&["/usr/bin/yum", "upgrade"])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -91,9 +91,9 @@ fn upgrade_redhat(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(),
Ok(()) Ok(())
} }
fn upgrade_fedora(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(), failure::Error> { fn upgrade_fedora(sudo: &Option<PathBuf>, terminal: &mut Terminal, dry_run: bool) -> Result<(), failure::Error> {
if let Some(sudo) = &sudo { if let Some(sudo) = &sudo {
Command::new(&sudo) Executor::new(&sudo, dry_run)
.args(&["/usr/bin/dnf", "upgrade"]) .args(&["/usr/bin/dnf", "upgrade"])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -105,15 +105,15 @@ fn upgrade_fedora(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(),
Ok(()) Ok(())
} }
fn upgrade_debian(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(), failure::Error> { fn upgrade_debian(sudo: &Option<PathBuf>, terminal: &mut Terminal, dry_run: bool) -> Result<(), failure::Error> {
if let Some(sudo) = &sudo { if let Some(sudo) = &sudo {
Command::new(&sudo) Executor::new(&sudo, dry_run)
.args(&["/usr/bin/apt", "update"]) .args(&["/usr/bin/apt", "update"])
.spawn()? .spawn()?
.wait()? .wait()?
.check()?; .check()?;
Command::new(&sudo) Executor::new(&sudo, dry_run)
.args(&["/usr/bin/apt", "dist-upgrade"]) .args(&["/usr/bin/apt", "dist-upgrade"])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -126,15 +126,15 @@ fn upgrade_debian(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Result<(),
} }
#[must_use] #[must_use]
pub fn upgrade(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn upgrade(sudo: &Option<PathBuf>, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
terminal.print_separator("System update"); terminal.print_separator("System update");
let success = match Distribution::detect() { let success = match Distribution::detect() {
Ok(distribution) => match distribution { Ok(distribution) => match distribution {
Distribution::Arch => upgrade_arch_linux(&sudo, terminal), Distribution::Arch => upgrade_arch_linux(&sudo, terminal, dry_run),
Distribution::CentOS => upgrade_redhat(&sudo, terminal), Distribution::CentOS => upgrade_redhat(&sudo, terminal, dry_run),
Distribution::Fedora => upgrade_fedora(&sudo, terminal), Distribution::Fedora => upgrade_fedora(&sudo, terminal, dry_run),
Distribution::Ubuntu | Distribution::Debian => upgrade_debian(&sudo, terminal), Distribution::Ubuntu | Distribution::Debian => upgrade_debian(&sudo, terminal, dry_run),
}.is_ok(), }.is_ok(),
Err(e) => { Err(e) => {
println!("Error detecting current distribution: {}", e); println!("Error detecting current distribution: {}", e);
@@ -146,13 +146,13 @@ pub fn upgrade(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Option<(&'sta
} }
#[must_use] #[must_use]
pub fn run_needrestart(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_needrestart(sudo: &Option<PathBuf>, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(sudo) = sudo { if let Some(sudo) = sudo {
if let Some(needrestart) = which("needrestart") { if let Some(needrestart) = which("needrestart") {
terminal.print_separator("Check for needed restarts"); terminal.print_separator("Check for needed restarts");
let success = || -> Result<(), failure::Error> { let success = || -> Result<(), failure::Error> {
Command::new(&sudo).arg(needrestart).spawn()?.wait()?.check()?; Executor::new(&sudo, dry_run).arg(needrestart).spawn()?.wait()?.check()?;
Ok(()) Ok(())
}().is_ok(); }().is_ok();
@@ -165,13 +165,21 @@ pub fn run_needrestart(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Optio
} }
#[must_use] #[must_use]
pub fn run_fwupdmgr(terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_fwupdmgr(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(fwupdmgr) = which("fwupdmgr") { if let Some(fwupdmgr) = which("fwupdmgr") {
terminal.print_separator("Firmware upgrades"); terminal.print_separator("Firmware upgrades");
let success = || -> Result<(), failure::Error> { let success = || -> Result<(), failure::Error> {
Command::new(&fwupdmgr).arg("refresh").spawn()?.wait()?.check()?; Executor::new(&fwupdmgr, dry_run)
Command::new(&fwupdmgr).arg("get-updates").spawn()?.wait()?.check()?; .arg("refresh")
.spawn()?
.wait()?
.check()?;
Executor::new(&fwupdmgr, dry_run)
.arg("get-updates")
.spawn()?
.wait()?
.check()?;
Ok(()) Ok(())
}().is_ok(); }().is_ok();
@@ -182,12 +190,16 @@ pub fn run_fwupdmgr(terminal: &mut Terminal) -> Option<(&'static str, bool)> {
} }
#[must_use] #[must_use]
pub fn run_flatpak(terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_flatpak(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(flatpak) = which("flatpak") { if let Some(flatpak) = which("flatpak") {
terminal.print_separator("Flatpak"); terminal.print_separator("Flatpak");
let success = || -> Result<(), failure::Error> { let success = || -> Result<(), failure::Error> {
Command::new(&flatpak).args(&["update", "-y"]).spawn()?.wait()?.check()?; Executor::new(&flatpak, dry_run)
.args(&["update", "-y"])
.spawn()?
.wait()?
.check()?;
Ok(()) Ok(())
}().is_ok(); }().is_ok();
@@ -198,14 +210,14 @@ pub fn run_flatpak(terminal: &mut Terminal) -> Option<(&'static str, bool)> {
} }
#[must_use] #[must_use]
pub fn run_snap(sudo: &Option<PathBuf>, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_snap(sudo: &Option<PathBuf>, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(sudo) = sudo { if let Some(sudo) = sudo {
if let Some(snap) = which("snap") { if let Some(snap) = which("snap") {
if PathBuf::from("/var/snapd.socket").exists() { if PathBuf::from("/var/snapd.socket").exists() {
terminal.print_separator("snap"); terminal.print_separator("snap");
let success = || -> Result<(), failure::Error> { let success = || -> Result<(), failure::Error> {
Command::new(&sudo) Executor::new(&sudo, dry_run)
.args(&[snap.to_str().unwrap(), "refresh"]) .args(&[snap.to_str().unwrap(), "refresh"])
.spawn()? .spawn()?
.wait()? .wait()?

View File

@@ -1,14 +1,14 @@
use super::executor::Executor;
use super::terminal::Terminal; use super::terminal::Terminal;
use super::utils::Check; use super::utils::Check;
use failure; use failure;
use std::process::Command;
#[must_use] #[must_use]
pub fn upgrade_macos(terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn upgrade_macos(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
terminal.print_separator("App Store"); terminal.print_separator("App Store");
let success = || -> Result<(), failure::Error> { let success = || -> Result<(), failure::Error> {
Command::new("softwareupdate") Executor::new("softwareupdate", dry_run)
.args(&["--install", "--all"]) .args(&["--install", "--all"])
.spawn()? .spawn()?
.wait()? .wait()?

View File

@@ -26,6 +26,7 @@ mod unix;
mod windows; mod windows;
mod config; mod config;
mod executor;
mod generic; mod generic;
mod git; mod git;
mod node; mod node;
@@ -84,6 +85,12 @@ fn run() -> Result<(), Error> {
.help("Don't perform system upgrade") .help("Don't perform system upgrade")
.long("no-system"), .long("no-system"),
) )
.arg(
Arg::with_name("dry_run")
.help("Print what would be done")
.short("n")
.long("dry-run"),
)
.get_matches(); .get_matches();
if matches.is_present("tmux") && env::var("TMUX").is_err() { if matches.is_present("tmux") && env::var("TMUX").is_err() {
@@ -100,13 +107,14 @@ fn run() -> Result<(), Error> {
let mut terminal = Terminal::new(); let mut terminal = Terminal::new();
let config = Config::read(&base_dirs)?; let config = Config::read(&base_dirs)?;
let mut report = Report::new(); let mut report = Report::new();
let dry_run = matches.is_present("dry_run");
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
let sudo = utils::which("sudo"); let sudo = utils::which("sudo");
if let Some(commands) = config.pre_commands() { if let Some(commands) = config.pre_commands() {
for (name, command) in commands { for (name, command) in commands {
generic::run_custom_command(&name, &command, &mut terminal)?; generic::run_custom_command(&name, &command, &mut terminal, dry_run)?;
} }
} }
@@ -114,20 +122,29 @@ fn run() -> Result<(), Error> {
let powershell = windows::Powershell::new(); let powershell = windows::Powershell::new();
#[cfg(windows)] #[cfg(windows)]
report.push_result(execute(|terminal| powershell.update_modules(terminal), &mut terminal)); report.push_result(execute(
|terminal| powershell.update_modules(terminal, dry_run),
&mut terminal,
));
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
if !(matches.is_present("no_system")) { if !(matches.is_present("no_system")) {
report.push_result(execute(|terminal| linux::upgrade(&sudo, terminal), &mut terminal)); report.push_result(execute(
|terminal| linux::upgrade(&sudo, terminal, dry_run),
&mut terminal,
));
} }
} }
#[cfg(windows)] #[cfg(windows)]
report.push_result(execute(|terminal| windows::run_chocolatey(terminal), &mut terminal)); report.push_result(execute(
|terminal| windows::run_chocolatey(terminal, dry_run),
&mut terminal,
));
#[cfg(unix)] #[cfg(unix)]
report.push_result(execute(|terminal| unix::run_homebrew(terminal), &mut terminal)); report.push_result(execute(|terminal| unix::run_homebrew(terminal, dry_run), &mut terminal));
git_repos.insert(base_dirs.home_dir().join(".emacs.d")); git_repos.insert(base_dirs.home_dir().join(".emacs.d"));
git_repos.insert(base_dirs.home_dir().join(".vim")); git_repos.insert(base_dirs.home_dir().join(".vim"));
@@ -155,56 +172,73 @@ fn run() -> Result<(), Error> {
} }
for repo in git_repos.repositories() { for repo in git_repos.repositories() {
report.push_result(execute(|terminal| git.pull(&repo, terminal), &mut terminal)); report.push_result(execute(|terminal| git.pull(&repo, terminal, dry_run), &mut terminal));
} }
#[cfg(unix)] #[cfg(unix)]
{ {
report.push_result(execute(|terminal| unix::run_zplug(&base_dirs, terminal), &mut terminal));
report.push_result(execute( report.push_result(execute(
|terminal| unix::run_fisherman(&base_dirs, terminal), |terminal| unix::run_zplug(&base_dirs, terminal, dry_run),
&mut terminal,
));
report.push_result(execute(
|terminal| unix::run_fisherman(&base_dirs, terminal, dry_run),
&mut terminal,
));
report.push_result(execute(
|terminal| unix::run_tpm(&base_dirs, terminal, dry_run),
&mut terminal, &mut terminal,
)); ));
report.push_result(execute(|terminal| unix::run_tpm(&base_dirs, terminal), &mut terminal));
} }
report.push_result(execute( report.push_result(execute(
|terminal| generic::run_rustup(&base_dirs, terminal), |terminal| generic::run_rustup(&base_dirs, terminal, dry_run),
&mut terminal, &mut terminal,
)); ));
report.push_result(execute( report.push_result(execute(
|terminal| generic::run_cargo_update(&base_dirs, terminal), |terminal| generic::run_cargo_update(&base_dirs, terminal, dry_run),
&mut terminal, &mut terminal,
)); ));
report.push_result(execute( report.push_result(execute(
|terminal| generic::run_emacs(&base_dirs, terminal), |terminal| generic::run_emacs(&base_dirs, terminal, dry_run),
&mut terminal, &mut terminal,
)); ));
report.push_result(execute( report.push_result(execute(
|terminal| vim::upgrade_vim(&base_dirs, terminal), |terminal| vim::upgrade_vim(&base_dirs, terminal, dry_run),
&mut terminal, &mut terminal,
)); ));
report.push_result(execute( report.push_result(execute(
|terminal| vim::upgrade_neovim(&base_dirs, terminal), |terminal| vim::upgrade_neovim(&base_dirs, terminal, dry_run),
&mut terminal, &mut terminal,
)); ));
report.push_result(execute( report.push_result(execute(
|terminal| node::run_npm_upgrade(&base_dirs, terminal), |terminal| node::run_npm_upgrade(&base_dirs, terminal, dry_run),
&mut terminal, &mut terminal,
)); ));
report.push_result(execute(|terminal| node::yarn_global_update(terminal), &mut terminal)); report.push_result(execute(
report.push_result(execute(|terminal| generic::run_apm(terminal), &mut terminal)); |terminal| node::yarn_global_update(terminal, dry_run),
&mut terminal,
));
report.push_result(execute(|terminal| generic::run_apm(terminal, dry_run), &mut terminal));
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
report.push_result(execute(|terminal| linux::run_flatpak(terminal), &mut terminal)); report.push_result(execute(|terminal| linux::run_flatpak(terminal, dry_run), &mut terminal));
report.push_result(execute(|terminal| linux::run_snap(&sudo, terminal), &mut terminal)); report.push_result(execute(
|terminal| linux::run_snap(&sudo, terminal, dry_run),
&mut terminal,
));
} }
if let Some(commands) = config.commands() { if let Some(commands) = config.commands() {
for (name, command) in commands { for (name, command) in commands {
report.push_result(execute( report.push_result(execute(
|terminal| Some((name, generic::run_custom_command(&name, &command, terminal).is_ok())), |terminal| {
Some((
name,
generic::run_custom_command(&name, &command, terminal, dry_run).is_ok(),
))
},
&mut terminal, &mut terminal,
)); ));
} }
@@ -212,9 +246,12 @@ fn run() -> Result<(), Error> {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
report.push_result(execute(|terminal| linux::run_fwupdmgr(terminal), &mut terminal));
report.push_result(execute( report.push_result(execute(
|terminal| linux::run_needrestart(&sudo, terminal), |terminal| linux::run_fwupdmgr(terminal, dry_run),
&mut terminal,
));
report.push_result(execute(
|terminal| linux::run_needrestart(&sudo, terminal, dry_run),
&mut terminal, &mut terminal,
)); ));
} }
@@ -222,14 +259,20 @@ fn run() -> Result<(), Error> {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
if !(matches.is_present("no_system")) { if !(matches.is_present("no_system")) {
report.push_result(execute(|terminal| macos::upgrade_macos(terminal), &mut terminal)); report.push_result(execute(
|terminal| macos::upgrade_macos(terminal, dry_run),
&mut terminal,
));
} }
} }
#[cfg(windows)] #[cfg(windows)]
{ {
if !(matches.is_present("no_system")) { if !(matches.is_present("no_system")) {
report.push_result(execute(|terminal| powershell.windows_update(terminal), &mut terminal)); report.push_result(execute(
|terminal| powershell.windows_update(terminal, dry_run),
&mut terminal,
));
} }
} }

View File

@@ -1,3 +1,4 @@
use super::executor::Executor;
use super::terminal::Terminal; use super::terminal::Terminal;
use super::utils::{which, Check, PathExt}; use super::utils::{which, Check, PathExt};
use directories::BaseDirs; use directories::BaseDirs;
@@ -22,8 +23,8 @@ impl NPM {
Ok(PathBuf::from(&String::from_utf8(output.stdout)?)) Ok(PathBuf::from(&String::from_utf8(output.stdout)?))
} }
fn upgrade(&self) -> Result<(), failure::Error> { fn upgrade(&self, dry_run: bool) -> Result<(), failure::Error> {
Command::new(&self.command) Executor::new(&self.command, dry_run)
.args(&["update", "-g"]) .args(&["update", "-g"])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -34,12 +35,12 @@ impl NPM {
} }
#[must_use] #[must_use]
pub fn run_npm_upgrade(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_npm_upgrade(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(npm) = which("npm").map(NPM::new) { if let Some(npm) = which("npm").map(NPM::new) {
if let Ok(npm_root) = npm.root() { if let Ok(npm_root) = npm.root() {
if npm_root.is_descendant_of(base_dirs.home_dir()) { if npm_root.is_descendant_of(base_dirs.home_dir()) {
terminal.print_separator("Node Package Manager"); terminal.print_separator("Node Package Manager");
let success = npm.upgrade().is_ok(); let success = npm.upgrade(dry_run).is_ok();
return Some(("NPM", success)); return Some(("NPM", success));
} }
} }
@@ -48,12 +49,12 @@ pub fn run_npm_upgrade(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<
} }
#[must_use] #[must_use]
pub fn yarn_global_update(terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn yarn_global_update(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(yarn) = which("yarn") { if let Some(yarn) = which("yarn") {
terminal.print_separator("Yarn"); terminal.print_separator("Yarn");
let success = || -> Result<(), failure::Error> { let success = || -> Result<(), failure::Error> {
Command::new(&yarn) Executor::new(&yarn, dry_run)
.args(&["global", "upgrade", "-s"]) .args(&["global", "upgrade", "-s"])
.spawn()? .spawn()?
.wait()? .wait()?

View File

@@ -1,3 +1,4 @@
use super::executor::Executor;
use super::terminal::Terminal; use super::terminal::Terminal;
use super::utils::{which, Check, PathExt}; use super::utils::{which, Check, PathExt};
use directories::BaseDirs; use directories::BaseDirs;
@@ -6,13 +7,13 @@ use std::env;
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
use std::process::Command; use std::process::Command;
pub fn run_zplug(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_zplug(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(zsh) = which("zsh") { if let Some(zsh) = which("zsh") {
if base_dirs.home_dir().join(".zplug").exists() { if base_dirs.home_dir().join(".zplug").exists() {
terminal.print_separator("zplug"); terminal.print_separator("zplug");
let success = || -> Result<(), Error> { let success = || -> Result<(), Error> {
Command::new(zsh) Executor::new(zsh, dry_run)
.args(&["-c", "source ~/.zshrc && zplug update"]) .args(&["-c", "source ~/.zshrc && zplug update"])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -27,13 +28,13 @@ pub fn run_zplug(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'sta
None None
} }
pub fn run_fisherman(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_fisherman(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(fish) = which("fish") { if let Some(fish) = which("fish") {
if base_dirs.home_dir().join(".config/fish/functions/fisher.fish").exists() { if base_dirs.home_dir().join(".config/fish/functions/fisher.fish").exists() {
terminal.print_separator("fisherman"); terminal.print_separator("fisherman");
let success = || -> Result<(), Error> { let success = || -> Result<(), Error> {
Command::new(fish) Executor::new(fish, dry_run)
.args(&["-c", "fisher update"]) .args(&["-c", "fisher update"])
.spawn()? .spawn()?
.wait()? .wait()?
@@ -48,7 +49,7 @@ pub fn run_fisherman(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&
None None
} }
pub fn run_tpm(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_tpm(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(tpm) = base_dirs if let Some(tpm) = base_dirs
.home_dir() .home_dir()
.join(".tmux/plugins/tpm/bin/update_plugins") .join(".tmux/plugins/tpm/bin/update_plugins")
@@ -57,7 +58,7 @@ pub fn run_tpm(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'stati
terminal.print_separator("tmux plugins"); terminal.print_separator("tmux plugins");
let success = || -> Result<(), Error> { let success = || -> Result<(), Error> {
Command::new(&tpm).arg("all").spawn()?.wait()?.check()?; Executor::new(&tpm, dry_run).arg("all").spawn()?.wait()?.check()?;
Ok(()) Ok(())
}().is_ok(); }().is_ok();
@@ -87,13 +88,13 @@ pub fn run_in_tmux() -> ! {
} }
#[must_use] #[must_use]
pub fn run_homebrew(terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_homebrew(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(brew) = which("brew") { if let Some(brew) = which("brew") {
terminal.print_separator("Brew"); terminal.print_separator("Brew");
let inner = || -> Result<(), Error> { let inner = || -> Result<(), Error> {
Command::new(&brew).arg("update").spawn()?.wait()?.check()?; Executor::new(&brew, dry_run).arg("update").spawn()?.wait()?.check()?;
Command::new(&brew).arg("upgrade").spawn()?.wait()?.check()?; Executor::new(&brew, dry_run).arg("upgrade").spawn()?.wait()?.check()?;
Ok(()) Ok(())
}; };

View File

@@ -1,10 +1,10 @@
use super::executor::Executor;
use super::terminal::Terminal; use super::terminal::Terminal;
use super::utils::{which, Check, PathExt}; use super::utils::{which, Check, PathExt};
use directories::BaseDirs; use directories::BaseDirs;
use failure; use failure;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command;
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub enum PluginFramework { pub enum PluginFramework {
@@ -54,8 +54,13 @@ fn nvimrc(base_dirs: &BaseDirs) -> Option<PathBuf> {
} }
#[must_use] #[must_use]
fn upgrade(vim: &PathBuf, vimrc: &PathBuf, plugin_framework: PluginFramework) -> Result<(), failure::Error> { fn upgrade(
Command::new(&vim) vim: &PathBuf,
vimrc: &PathBuf,
plugin_framework: PluginFramework,
dry_run: bool,
) -> Result<(), failure::Error> {
Executor::new(&vim, dry_run)
.args(&[ .args(&[
"-N", "-N",
"-u", "-u",
@@ -78,12 +83,12 @@ fn upgrade(vim: &PathBuf, vimrc: &PathBuf, plugin_framework: PluginFramework) ->
} }
#[must_use] #[must_use]
pub fn upgrade_vim(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn upgrade_vim(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(vim) = which("vim") { if let Some(vim) = which("vim") {
if let Some(vimrc) = vimrc(&base_dirs) { if let Some(vimrc) = vimrc(&base_dirs) {
if let Some(plugin_framework) = PluginFramework::detect(&vimrc) { if let Some(plugin_framework) = PluginFramework::detect(&vimrc) {
terminal.print_separator(&format!("Vim ({:?})", plugin_framework)); terminal.print_separator(&format!("Vim ({:?})", plugin_framework));
let success = upgrade(&vim, &vimrc, plugin_framework).is_ok(); let success = upgrade(&vim, &vimrc, plugin_framework, dry_run).is_ok();
return Some(("vim", success)); return Some(("vim", success));
} }
} }
@@ -93,12 +98,12 @@ pub fn upgrade_vim(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'s
} }
#[must_use] #[must_use]
pub fn upgrade_neovim(base_dirs: &BaseDirs, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn upgrade_neovim(base_dirs: &BaseDirs, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(nvim) = which("nvim") { if let Some(nvim) = which("nvim") {
if let Some(nvimrc) = nvimrc(&base_dirs) { if let Some(nvimrc) = nvimrc(&base_dirs) {
if let Some(plugin_framework) = PluginFramework::detect(&nvimrc) { if let Some(plugin_framework) = PluginFramework::detect(&nvimrc) {
terminal.print_separator(&format!("Neovim ({:?})", plugin_framework)); terminal.print_separator(&format!("Neovim ({:?})", plugin_framework));
let success = upgrade(&nvim, &nvimrc, plugin_framework).is_ok(); let success = upgrade(&nvim, &nvimrc, plugin_framework, dry_run).is_ok();
return Some(("Neovim", success)); return Some(("Neovim", success));
} }
} }

View File

@@ -1,3 +1,4 @@
use super::executor::Executor;
use super::terminal::Terminal; use super::terminal::Terminal;
use super::utils::{self, which, Check}; use super::utils::{self, which, Check};
use failure; use failure;
@@ -5,12 +6,16 @@ use std::path::PathBuf;
use std::process::Command; use std::process::Command;
#[must_use] #[must_use]
pub fn run_chocolatey(terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn run_chocolatey(terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(choco) = utils::which("choco") { if let Some(choco) = utils::which("choco") {
terminal.print_separator("Chocolatey"); terminal.print_separator("Chocolatey");
let success = || -> Result<(), failure::Error> { let success = || -> Result<(), failure::Error> {
Command::new(&choco).args(&["upgrade", "all"]).spawn()?.wait()?.check()?; Executor::new(&choco, dry_run)
.args(&["upgrade", "all"])
.spawn()?
.wait()?
.check()?;
Ok(()) Ok(())
}().is_ok(); }().is_ok();
@@ -60,12 +65,16 @@ impl Powershell {
} }
#[must_use] #[must_use]
pub fn update_modules(&self, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn update_modules(&self, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(powershell) = &self.path { if let Some(powershell) = &self.path {
terminal.print_separator("Powershell Modules Update"); terminal.print_separator("Powershell Modules Update");
let success = || -> Result<(), failure::Error> { let success = || -> Result<(), failure::Error> {
Command::new(&powershell).arg("Update-Module").spawn()?.wait()?.check()?; Executor::new(&powershell, dry_run)
.arg("Update-Module")
.spawn()?
.wait()?
.check()?;
Ok(()) Ok(())
}().is_ok(); }().is_ok();
@@ -76,13 +85,13 @@ impl Powershell {
} }
#[must_use] #[must_use]
pub fn windows_update(&self, terminal: &mut Terminal) -> Option<(&'static str, bool)> { pub fn windows_update(&self, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> {
if let Some(powershell) = &self.path { if let Some(powershell) = &self.path {
if Self::has_command(&powershell, "Install-WindowsUpdate") { if Self::has_command(&powershell, "Install-WindowsUpdate") {
terminal.print_separator("Windows Update"); terminal.print_separator("Windows Update");
let success = || -> Result<(), failure::Error> { let success = || -> Result<(), failure::Error> {
Command::new(&powershell) Executor::new(&powershell, dry_run)
.args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"]) .args(&["-Command", "Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -Verbose"])
.spawn()? .spawn()?
.wait()? .wait()?