From 405cf3aba8d803d57b910a159cc914d937c17f5a Mon Sep 17 00:00:00 2001 From: zyl Date: Thu, 17 Oct 2024 18:27:41 -0700 Subject: [PATCH] add untested homebrew support --- src/main.rs | 57 ++++++++++++++++++++++++++++++++++++++++++----------- todo.md | 2 +- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index a36c9bf..9728533 100644 --- a/src/main.rs +++ b/src/main.rs @@ -77,8 +77,13 @@ async fn pupdate_remote( Ok((remote, success)) } -/// pupdates the local system using apt-get -async fn pupdate_apt(log_dir: Option) -> eyre::Result { +/// helper for pupdates which both follow the "X update && X upgrade" set of commands (e.g. apt-get and brew) +async fn update_and_upgrade( + log_dir: Option, + command: &str, + with_sudo: bool, + yes: bool, +) -> eyre::Result { async fn log(outputs: &[std::process::Output], log_dir: Option) -> eyre::Result { if let Some(log_dir) = log_dir { let mut stdout = File::create(log_dir.join("local.stdout.log")).await?; @@ -96,23 +101,42 @@ async fn pupdate_apt(log_dir: Option) -> eyre::Result { Ok(true) } - let update_output = Command::new("sudo") - .arg("apt-get") + fn command_with_sudo(command: &str, with_sudo: bool) -> Command { + if with_sudo { + let mut cmd = Command::new("sudo"); + cmd.arg(command); + cmd + } else { + Command::new(command) + } + } + + let update_output = command_with_sudo(command, with_sudo) .arg("update") .output() .await?; if !update_output.status.success() { return log(&[update_output], log_dir).await; } - let upgrade_output = Command::new("sudo") - .arg("apt-get") - .arg("upgrade") - .arg("-y") - .output() - .await?; + let mut upgrade_command = command_with_sudo(command, with_sudo); + upgrade_command.arg("upgrade"); + if yes { + upgrade_command.arg("-y"); + } + let upgrade_output = upgrade_command.output().await?; log(&[update_output, upgrade_output], log_dir).await } +/// pupdates the local system using apt-get +async fn pupdate_apt(log_dir: Option) -> eyre::Result { + update_and_upgrade(log_dir, "apt-get", true, true).await +} + +/// pupdates the local system using brew, untested +async fn pupdate_homebrew(log_dir: Option) -> eyre::Result { + update_and_upgrade(log_dir, "brew", false, false).await +} + #[tokio::main] async fn main() -> eyre::Result<()> { let args = Args::parse(); @@ -200,7 +224,18 @@ async fn main() -> eyre::Result<()> { if !args.skip_local { println!("running local pupdates, you may be pawmpted for your password"); let start = OffsetDateTime::now_utc(); - if pupdate_apt(log_dir).await? { + let result = if cfg!(target_os = "linux") { + println!("pupdating with apt-get..."); + Some(pupdate_apt(log_dir).await) + } else if cfg!(target_os = "macos") { + println!("pupdating with brew..."); + Some(pupdate_homebrew(log_dir).await) + } else { + eprintln!("unsupported operating system for local pupdates, skipping"); + None + }; + if let Some(result) = result { + result?; let end = OffsetDateTime::now_utc(); let duration = end - start; diff --git a/todo.md b/todo.md index 869cc85..7564e83 100644 --- a/todo.md +++ b/todo.md @@ -4,4 +4,4 @@ some things that may or may not get done at some point: - pupdate daemon to remotely pupdate without using ssh (use ssh keys for authentication though maybe?) - pupdate docker compose containers (interactively?) -- support other pupdate methods than just apt +- support other pupdate methods than just apt and homebrew