Turn report to a struct

This commit is contained in:
Roey Darwish Dror
2018-08-24 21:52:17 +03:00
parent eb133bff6b
commit 5fb2686e56
2 changed files with 52 additions and 40 deletions

View File

@@ -36,7 +36,7 @@ mod vim;
use self::config::Config;
use self::git::{Git, Repositories};
use self::report::{report, Report};
use self::report::Report;
use self::terminal::Terminal;
use clap::{App, Arg};
use failure::Error;
@@ -81,7 +81,7 @@ fn run() -> Result<(), Error> {
let mut git_repos = Repositories::new(&git);
let mut terminal = Terminal::new();
let config = Config::read(&base_dirs)?;
let mut reports = Report::new();
let mut report = Report::new();
#[cfg(target_os = "linux")]
let sudo = utils::which("sudo");
@@ -96,20 +96,20 @@ fn run() -> Result<(), Error> {
let powershell = windows::Powershell::new();
#[cfg(windows)]
report(&mut reports, powershell.update_modules(&mut terminal));
report.push_result(powershell.update_modules(&mut terminal));
#[cfg(target_os = "linux")]
{
if !(matches.is_present("no_system")) {
report(&mut reports, linux::upgrade(&sudo, &mut terminal));
report.push_result(linux::upgrade(&sudo, &mut terminal));
}
}
#[cfg(windows)]
report(&mut reports, windows::run_chocolatey(&mut terminal));
report.push_result(windows::run_chocolatey(&mut terminal));
#[cfg(unix)]
report(&mut reports, unix::run_homebrew(&mut terminal));
report.push_result(unix::run_homebrew(&mut terminal));
git_repos.insert(base_dirs.home_dir().join(".emacs.d"));
git_repos.insert(base_dirs.home_dir().join(".vim"));
@@ -137,72 +137,69 @@ fn run() -> Result<(), Error> {
}
for repo in git_repos.repositories() {
report(&mut reports, git.pull(&repo, &mut terminal));
report.push_result(git.pull(&repo, &mut terminal));
}
#[cfg(unix)]
{
report(&mut reports, unix::run_zplug(&base_dirs, &mut terminal));
report(&mut reports, unix::run_fisherman(&base_dirs, &mut terminal));
report(&mut reports, unix::run_tpm(&base_dirs, &mut terminal));
report.push_result(unix::run_zplug(&base_dirs, &mut terminal));
report.push_result(unix::run_fisherman(&base_dirs, &mut terminal));
report.push_result(unix::run_tpm(&base_dirs, &mut terminal));
}
report(&mut reports, generic::run_rustup(&base_dirs, &mut terminal));
report(&mut reports, generic::run_cargo_update(&base_dirs, &mut terminal));
report(&mut reports, generic::run_emacs(&base_dirs, &mut terminal));
report(&mut reports, vim::upgrade_vim(&base_dirs, &mut terminal));
report(&mut reports, vim::upgrade_neovim(&base_dirs, &mut terminal));
report(&mut reports, node::run_npm_upgrade(&base_dirs, &mut terminal));
report(&mut reports, node::yarn_global_update(&mut terminal));
report(&mut reports, generic::run_apm(&mut terminal));
report.push_result(generic::run_rustup(&base_dirs, &mut terminal));
report.push_result(generic::run_cargo_update(&base_dirs, &mut terminal));
report.push_result(generic::run_emacs(&base_dirs, &mut terminal));
report.push_result(vim::upgrade_vim(&base_dirs, &mut terminal));
report.push_result(vim::upgrade_neovim(&base_dirs, &mut terminal));
report.push_result(node::run_npm_upgrade(&base_dirs, &mut terminal));
report.push_result(node::yarn_global_update(&mut terminal));
report.push_result(generic::run_apm(&mut terminal));
#[cfg(target_os = "linux")]
{
report(&mut reports, linux::run_flatpak(&mut terminal));
report(&mut reports, linux::run_snap(&sudo, &mut terminal));
report.push_result(linux::run_flatpak(&mut terminal));
report.push_result(linux::run_snap(&sudo, &mut terminal));
}
if let Some(commands) = config.commands() {
for (name, command) in commands {
report(
&mut reports,
Some((
name,
generic::run_custom_command(&name, &command, &mut terminal).is_ok(),
)),
);
report.push_result(Some((
name,
generic::run_custom_command(&name, &command, &mut terminal).is_ok(),
)));
}
}
#[cfg(target_os = "linux")]
{
report(&mut reports, linux::run_fwupdmgr(&mut terminal));
report(&mut reports, linux::run_needrestart(&sudo, &mut terminal));
report.push_result(linux::run_fwupdmgr(&mut terminal));
report.push_result(linux::run_needrestart(&sudo, &mut terminal));
}
#[cfg(target_os = "macos")]
{
if !(matches.is_present("no_system")) {
report(&mut reports, macos::upgrade_macos(&mut terminal));
report.push_result(macos::upgrade_macos(&mut terminal));
}
}
#[cfg(windows)]
{
if !(matches.is_present("no_system")) {
report(&mut reports, powershell.windows_update(&mut terminal));
report.push_result(powershell.windows_update(&mut terminal));
}
}
if !reports.is_empty() {
if !report.data().is_empty() {
terminal.print_separator("Summary");
for (key, succeeded) in &reports {
for (key, succeeded) in report.data() {
terminal.print_result(key, *succeeded);
}
}
if reports.iter().all(|(_, succeeded)| *succeeded) {
if report.data().iter().all(|(_, succeeded)| *succeeded) {
Ok(())
} else {
Err(StepFailed.into())

View File

@@ -1,13 +1,28 @@
use std::borrow::Cow;
type CowString<'a> = Cow<'a, str>;
pub type Report<'a> = Vec<(CowString<'a>, bool)>;
pub struct Report<'a> {
data: Vec<(CowString<'a>, bool)>,
}
pub fn report<'a, M: Into<CowString<'a>>>(report: &mut Report<'a>, result: Option<(M, bool)>) {
if let Some((key, success)) = result {
let key = key.into();
impl<'a> Report<'a> {
pub fn new() -> Self {
Self { data: Vec::new() }
}
debug_assert!(!report.iter().any(|(k, _)| k == &key), "{} already reported", key);
report.push((key, success));
pub fn push_result<M>(&mut self, result: Option<(M, bool)>)
where
M: Into<CowString<'a>>,
{
if let Some((key, success)) = result {
let key = key.into();
debug_assert!(!self.data.iter().any(|(k, _)| k == &key), "{} already reported", key);
self.data.push((key, success));
}
}
pub fn data(&self) -> &Vec<(CowString<'a>, bool)> {
&self.data
}
}