From 1ec9ee7cc79f7c5bfe9ee639d81980106d28f9e4 Mon Sep 17 00:00:00 2001 From: shibedrill Date: Wed, 8 Jan 2025 22:53:45 -0500 Subject: [PATCH] Hunting down a bug --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/command/devel.rs | 39 ++++++++++++++++++++++++++++----------- src/main.rs | 4 ++++ 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c06152..01de83a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2322,7 +2322,7 @@ dependencies = [ [[package]] name = "shibe-bot" -version = "1.0.0" +version = "1.0.1" dependencies = [ "anyhow", "dotenvy", diff --git a/Cargo.toml b/Cargo.toml index 4e8ee8f..a324bd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "shibe-bot" description = "A Discord bot written in Rust, using Poise." license = "MIT" readme = "README.md" -version = "1.0.0" +version = "1.0.1" edition = "2021" build = "build.rs" diff --git a/src/command/devel.rs b/src/command/devel.rs index e862907..9b0e523 100644 --- a/src/command/devel.rs +++ b/src/command/devel.rs @@ -42,9 +42,12 @@ pub async fn version(ctx: Context<'_>) -> Result<(), Error> { Ok(()) } -/// Update the bot remotely (Requires updater systemd service) +/// Update the bot remotely (requires Github CI) #[poise::command(slash_command, owners_only, hide_in_help)] -pub async fn update(ctx: Context<'_>, override_check: bool) -> Result<(), Error> { +pub async fn update( + ctx: Context<'_>, + #[description = "Whether to skip the update check"] override_check: bool, +) -> Result<(), Error> { // Check if the current commit hash is different from HEAD let head: octocrab::models::repos::Ref = octocrab::instance() .get( @@ -56,7 +59,8 @@ pub async fn update(ctx: Context<'_>, override_check: bool) -> Result<(), Error> if sha == env!("GIT_COMMIT_ID") { if override_check { info!("Update unnecessary, but check overridden."); - ctx.say("Update unecessary, but check overridden. Updating.").await?; + ctx.say("Update unecessary, but check overridden. Updating.") + .await?; let Err(what) = self_update(); error!("Update failed: {}", what); ctx.say(format!("Error occurred while updating: {}", what)) @@ -73,7 +77,8 @@ pub async fn update(ctx: Context<'_>, override_check: bool) -> Result<(), Error> .await?; } } else { - ctx.say("Update failed: Object field in response is not a Commit.").await?; + ctx.say("Update failed: Object field in response is not a Commit.") + .await?; error!("Checking for updates failed: Response field incorrect type"); } Ok(()) @@ -126,7 +131,6 @@ fn self_update() -> Result { dest.write_all(&content)?; trace!("Downloaded latest build artifact successfully"); - let mut archive = zip::ZipArchive::new(dest)?; trace!("Created zip archive reader"); let mut zipped_bin = archive.by_index(0)?; @@ -137,12 +141,26 @@ fn self_update() -> Result { std::io::copy(&mut zipped_bin, &mut new_bin)?; trace!("Extracted binary successfully"); + let new_command_path = std::env::current_exe()?; + + trace!("Testing file prior to replace"); + { + let _ = std::fs::File::open(&new_command_path)?; + } + self_replace::self_replace(&new_bin_path)?; trace!("Replaced self with new binary successfully"); + trace!("Testing file after replace"); + { + let _ = std::fs::File::open(&new_command_path)?; + } + let new_command_args: Vec<_> = std::env::args_os().skip(1).collect(); - let new_command_path = std::env::current_exe()?; - trace!("Got current executable path successfully: {}", new_command_path.display()); + trace!( + "Got current executable path successfully: {}", + new_command_path.display() + ); Err(Box::new( std::process::Command::new(new_command_path) @@ -153,17 +171,16 @@ fn self_update() -> Result { mod test { - #[cfg(test)] - use std::convert::Infallible; #[cfg(test)] use crate::Error; + #[cfg(test)] + use std::convert::Infallible; #[test] fn test_self_update() -> Result { - use pretty_env_logger; use crate::command::devel::self_update; + use pretty_env_logger; pretty_env_logger::init(); self_update() } - } diff --git a/src/main.rs b/src/main.rs index 8d82a16..e1a7761 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,10 @@ async fn main() { // Initialize logging pretty_env_logger::init(); info!("Initialized logger successfully"); + info!( + "Current executable path: {}", + std::env::current_exe().unwrap() + ); // Get secure env vars from .env file match dotenv() { Ok(_) => info!("Loaded env vars from .env successfully"),