extern crate os_type; extern crate which; #[macro_use] extern crate error_chain; extern crate termion; mod error { error_chain!{ foreign_links { Io(::std::io::Error); } errors { ProcessFailed { description("Process failed") display("Process failed") } } } } mod git; mod terminal; use error::*; use git::Git; use os_type::OSType; use std::collections::HashSet; use std::env::home_dir; use std::path::PathBuf; use std::process::{Command, ExitStatus}; use terminal::Terminal; use which::which; trait Check { fn check(self) -> Result<()>; } impl Check for ExitStatus { fn check(self) -> Result<()> { if self.success() { Ok(()) } else { Err(ErrorKind::ProcessFailed.into()) } } } const EMACS_UPGRADE: &str = "(progn (let ((package-menu-async nil)) (package-list-packages)) (package-menu-mark-upgrades) (package-menu-execute 'noquery))"; fn home_path(p: &str) -> PathBuf { let mut path = home_dir().unwrap(); path.push(p); path } #[cfg(unix)] fn tpm() -> Option { let mut path = home_dir().unwrap(); path.push(".tmux/plugins/tpm/bin/update_plugins"); if path.exists() { Some(path) } else { None } } fn run() -> Result<()> { let git = Git::new(); let mut git_repos: HashSet = HashSet::new(); let terminal = Terminal::new(); { let mut collect_repo = |path| { if let Some(repo) = git.get_repo_root(path) { git_repos.insert(repo); } }; collect_repo(home_path(".emacs.d")); if cfg!(unix) { collect_repo(home_path(".zshrc")); collect_repo(home_path(".oh-my-zsh")); collect_repo(home_path(".tmux")); } } for repo in git_repos { terminal.print_separator(format!("Pulling {}", repo)); git.pull(repo)?; } if cfg!(unix) { if let Ok(zsh) = which("zsh") { terminal.print_separator("zplug"); if home_path(".zplug").exists() { Command::new(&zsh) .args(&["-c", "source ~/.zshrc && zplug update"]) .spawn()? .wait()?; } } if let Some(tpm) = tpm() { terminal.print_separator("tmux plugins"); Command::new(&tpm).arg("all").spawn()?.wait()?; } } let cargo_upgrade = home_path(".cargo/bin/cargo-install-update"); if cargo_upgrade.exists() { terminal.print_separator("Cargo"); Command::new(&cargo_upgrade) .args(&["install-update", "--all"]) .spawn()? .wait()?; } if let Ok(emacs) = which("emacs") { if home_path(".emacs.d").exists() { terminal.print_separator("Emacs"); Command::new(&emacs) .args(&[ "--batch", "-l", home_path(".emacs.d/init.el").to_str().unwrap(), "--eval", EMACS_UPGRADE, ]) .spawn()? .wait()?; } } if cfg!(target_os = "linux") { let sudo = which("sudo"); terminal.print_separator("System update"); match os_type::current_platform().os_type { OSType::Arch => { if let Ok(yay) = which("yay") { Command::new(yay).spawn()?.wait()?; } else { if let Ok(sudo) = &sudo { Command::new(&sudo) .args(&["pacman", "-Syu"]) .spawn()? .wait()?; } else { terminal.print_warning("No sudo or yay detected. Skipping system upgrade"); } } } OSType::CentOS | OSType::Redhat => { if let Ok(sudo) = &sudo { Command::new(&sudo) .args(&["yum", "upgrade"]) .spawn()? .wait()?; } } OSType::Ubuntu | OSType::Debian => { if let Ok(sudo) = &sudo { Command::new(&sudo) .args(&["apt", "update"]) .spawn()? .wait()? .check() .and_then(|()| { Command::new(&sudo) .args(&["apt", "dist-upgrade"]) .spawn()? .wait() .map_err(|e| e.into()) })?; } } OSType::Unknown => { terminal.print_warning( "Could not detect your Linux distribution. Do you have lsb-release installed?", ); } _ => (), } if let Ok(fwupdmgr) = which("fwupdmgr") { terminal.print_separator("Firmware upgrades"); Command::new(&fwupdmgr) .arg("refresh") .spawn()? .wait()? .check() .and_then(|()| { Command::new(&fwupdmgr) .arg("get-updates") .spawn()? .wait() .map_err(|e| e.into()) })?; } if let Ok(sudo) = &sudo { if let Ok(needrestart) = which("needrestart") { terminal.print_separator("Check for needed restarts"); Command::new(&sudo).arg(&needrestart).spawn()?.wait()?; } } } if cfg!(target_os = "macos") { if let Ok(brew) = which("brew") { terminal.print_separator("Homebrew"); Command::new(&brew) .arg("update") .spawn()? .wait()? .check() .and_then(|()| { Command::new(&brew) .arg("upgrade") .spawn()? .wait() .map_err(|e| e.into()) })?; } terminal.print_separator("System update"); Command::new("softwareupdate") .args(&["--install", "--all"]) .spawn()? .wait()?; } Ok(()) } quick_main!(run);