From 1e73011a1552074c34d5a0815bfa927da8809863 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Mon, 12 Nov 2018 11:13:43 +0200 Subject: [PATCH] Add FreeBSD --- .travis.yml | 1 + README.md | 2 ++ src/freebsd.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 19 ++++++++++++++++- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/freebsd.rs diff --git a/.travis.yml b/.travis.yml index b76be3a4..b8b74780 100644 --- a/.travis.yml +++ b/.travis.yml @@ -23,6 +23,7 @@ matrix: - env: TARGET=armv7-unknown-linux-gnueabihf - env: TARGET=x86_64-unknown-linux-gnu - env: TARGET=x86_64-unknown-linux-musl + - env: TARGET=x86_64-unknown-freebsd # OSX - env: TARGET=x86_64-apple-darwin diff --git a/README.md b/README.md index 25bcc34c..677648aa 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Just run `topgrade`. It will run the following steps: * *Gentoo*: Run `layman -s ALL && emerge --sync -q && eix-update && emerge -uDNa world` * *openSUSE*: Run `zypper refresh && zypper dist-upgrade` * *Linux*: Run [etc-update](https://dev.gentoo.org/~zmedico/portage/doc/man/etc-update.1.html): +* *FreeBSD*: Upgrade and audit packages * *Unix*: Run `brew update && brew upgrade`. This should handle both Homebrew and Linuxbrew * *Unix*: Run `nix upgrade-nix && nix --upgrade`. * *Windows*: Upgrade Powershell modules @@ -79,6 +80,7 @@ Just run `topgrade`. It will run the following steps: * *Linux*: Run [needrestart](https://github.com/liske/needrestart) * *Windows*: Run Windows Update (You'll have to install [PSWindowsUpdate](https://marckean.com/2016/06/01/use-powershell-to-install-windows-updates/)) * *macOS*: Upgrade App Store applications + * *FreeBSD*: Run `freebsd-upgrade` ## Flags * `-t/--tmux` - Topgrade will launch itself in a new tmux session. This flag has no effect if diff --git a/src/freebsd.rs b/src/freebsd.rs new file mode 100644 index 00000000..68cc24ca --- /dev/null +++ b/src/freebsd.rs @@ -0,0 +1,56 @@ +use super::executor::Executor; +use super::terminal::Terminal; +use super::utils::Check; +use failure; +use std::path::PathBuf; + +#[must_use] +pub fn upgrade_freebsd(sudo: &Option, terminal: &mut Terminal, dry_run: bool) -> Option<(&'static str, bool)> { + terminal.print_separator("FreeBSD Update"); + + if let Some(sudo) = sudo { + let success = || -> Result<(), failure::Error> { + Executor::new(sudo, dry_run) + .args(&["/usr/sbin/freebsd-update", "fetch", "install"]) + .spawn()? + .wait()? + .check()?; + Ok(()) + }().is_ok(); + + Some(("FreeBSD Update", success)) + } else { + terminal.print_warning("No sudo or yay detected. Skipping system upgrade"); + None + } +} + +#[must_use] +pub fn upgrade_packages( + sudo: &Option, + terminal: &mut Terminal, + dry_run: bool, +) -> Option<(&'static str, bool)> { + terminal.print_separator("FreeBSD Packages"); + + if let Some(sudo) = sudo { + let success = || -> Result<(), failure::Error> { + Executor::new(sudo, dry_run) + .args(&["/usr/sbin/pkg", "upgrade"]) + .spawn()? + .wait()? + .check()?; + Executor::new("/usr/sbin/pkg", dry_run) + .arg("audit") + .spawn()? + .wait()? + .check()?; + Ok(()) + }().is_ok(); + + Some(("FreeBSD Packages", success)) + } else { + terminal.print_warning("No sudo or yay detected. Skipping package upgrade"); + None + } +} diff --git a/src/main.rs b/src/main.rs index 330087f4..414d283b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,8 @@ extern crate lazy_static; extern crate self_update; extern crate walkdir; +#[cfg(target_os = "freebsd")] +mod freebsd; #[cfg(target_os = "linux")] mod linux; #[cfg(target_os = "macos")] @@ -132,7 +134,7 @@ fn run() -> Result<(), Error> { let config = Config::read(&base_dirs)?; let mut report = Report::new(); - #[cfg(target_os = "linux")] + #[cfg(any(target_os = "freebsd", target_os = "linux"))] let sudo = utils::which("sudo"); #[cfg(all( @@ -200,6 +202,11 @@ fn run() -> Result<(), Error> { |terminal| unix::run_homebrew(terminal, opt.dry_run), &mut execution_context, )?); + #[cfg(target_os = "freebsd")] + report.push_result(execute( + |terminal| freebsd::upgrade_packages(&sudo, terminal, opt.dry_run), + &mut execution_context, + )?); #[cfg(unix)] report.push_result(execute( |terminal| unix::run_nix(terminal, opt.dry_run), @@ -375,6 +382,16 @@ fn run() -> Result<(), Error> { } } + #[cfg(target_os = "freebsd")] + { + if !opt.no_system { + report.push_result(execute( + |terminal| freebsd::upgrade_freebsd(&sudo, terminal, opt.dry_run), + &mut execution_context, + )?); + } + } + #[cfg(windows)] { if !opt.no_system {