From 0c9f41294cb7a4d26909925e39009b687c064501 Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Sat, 23 Nov 2024 18:12:37 +0100 Subject: [PATCH 1/3] feat: shut down gracefully with ctrl+c Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com> --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/commands.rs | 18 +++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 1bba168f9..fec757cac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1113,6 +1113,16 @@ dependencies = [ "cipher", ] +[[package]] +name = "ctrlc" +version = "3.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90eeab0aa92f3f9b4e87f258c72b139c207d251f9cbc1080a0086b86a8870dd3" +dependencies = [ + "nix", + "windows-sys 0.59.0", +] + [[package]] name = "curve25519-dalek" version = "4.1.3" @@ -4170,6 +4180,7 @@ dependencies = [ "conflate", "convert_case", "crossterm", + "ctrlc", "dateparser", "dav-server", "derive_more", diff --git a/Cargo.toml b/Cargo.toml index b7a2d515e..894e139d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -117,6 +117,7 @@ open = "5.3.0" self_update = { version = "0.39.0", default-features = false, optional = true, features = ["rustls", "archive-tar", "compression-flate2"] } # FIXME: Downgraded to 0.39.0 due to https://github.com/jaemk/self_update/issues/136 tar = "0.4.42" toml = "0.8" +ctrlc = { version = "3.4.5", features = ["termination"] } [dev-dependencies] abscissa_core = { version = "0.8.1", default-features = false, features = ["testing"] } diff --git a/src/commands.rs b/src/commands.rs index 0b242ea10..f6dfa5b5a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -35,6 +35,7 @@ use std::fmt::Debug; use std::fs::File; use std::path::PathBuf; use std::str::FromStr; +use std::sync::mpsc::channel; #[cfg(feature = "mount")] use crate::commands::mount::MountCmd; @@ -63,8 +64,9 @@ use clap::builder::{ Styles, }; use convert_case::{Case, Casing}; +use ctrlc; use human_panic::setup_panic; -use log::{log, Level}; +use log::{info, log, Level}; use simplelog::{CombinedLogger, LevelFilter, TermLogger, TerminalMode, WriteLogger}; use self::find::FindCmd; @@ -179,6 +181,20 @@ impl Runnable for EntryPoint { // Set up panic hook for better error messages and logs setup_panic!(); + // Set up Ctrl-C handler + let (tx, rx) = channel(); + + ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel.")) + .expect("Error setting Ctrl-C handler"); + + _ = std::thread::spawn(move || { + // Wait for Ctrl-C + rx.recv().expect("Could not receive from channel."); + info!("Ctrl-C received, shutting down..."); + RUSTIC_APP.shutdown(Shutdown::Graceful) + }); + + // Run the subcommand self.commands.run(); RUSTIC_APP.shutdown(Shutdown::Graceful) } From 5915a79a50b21ee0a86bc2f37465d94c4d78a191 Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Sat, 23 Nov 2024 18:16:45 +0100 Subject: [PATCH 2/3] style: dprint fmt Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com> --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 894e139d3..f20448f7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,6 +101,7 @@ clap = { version = "4", features = ["derive", "env", "wrap_help"] } clap_complete = "4" conflate = "0.3.1" convert_case = "0.6.0" +ctrlc = { version = "3.4.5", features = ["termination"] } dateparser = "0.2.1" derive_more = { version = "1", features = ["debug"] } dialoguer = "0.11.0" @@ -117,7 +118,6 @@ open = "5.3.0" self_update = { version = "0.39.0", default-features = false, optional = true, features = ["rustls", "archive-tar", "compression-flate2"] } # FIXME: Downgraded to 0.39.0 due to https://github.com/jaemk/self_update/issues/136 tar = "0.4.42" toml = "0.8" -ctrlc = { version = "3.4.5", features = ["termination"] } [dev-dependencies] abscissa_core = { version = "0.8.1", default-features = false, features = ["testing"] } From 465c1db674c6015143430d3f22c10a687e21166c Mon Sep 17 00:00:00 2001 From: simonsan <14062932+simonsan@users.noreply.github.com> Date: Sat, 23 Nov 2024 18:17:18 +0100 Subject: [PATCH 3/3] remove unused import Signed-off-by: simonsan <14062932+simonsan@users.noreply.github.com> --- src/commands.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/commands.rs b/src/commands.rs index f6dfa5b5a..5ac4af30b 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -64,7 +64,6 @@ use clap::builder::{ Styles, }; use convert_case::{Case, Casing}; -use ctrlc; use human_panic::setup_panic; use log::{info, log, Level}; use simplelog::{CombinedLogger, LevelFilter, TermLogger, TerminalMode, WriteLogger};