From 6d673b5d9cc5a25a8492ab971d8137a755b248c9 Mon Sep 17 00:00:00 2001 From: hheik <4469778+hheik@users.noreply.github.com> Date: Sat, 24 Feb 2024 16:05:51 +0200 Subject: [PATCH] Better handling for server commandline args --- Cargo.toml | 11 +---------- src/lib.rs | 3 ++- src/main.rs | 6 +++++- src/os_unix.rs | 4 ++-- src/server.rs | 8 ++++++-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8a8876f..d8188fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,14 +12,5 @@ crc32fast = "1.3.2" crossterm = "0.27.0" interprocess = "1.2.1" ratatui = "0.23.0" -rodio = {version = "0.17.3", features = [ - "claxon", - "flac", - "hound", - "lewton", - "mp3", - "symphonia-all", - "vorbis", - "wav", -]} +rodio = {version = "0.17.3", features = [ "symphonia-all" ], default-features = false } serde = "1.0.196" diff --git a/src/lib.rs b/src/lib.rs index ef3fd87..57e9581 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,8 @@ pub mod server { pub enum ServerError { Other(String), Io(std::io::Error), - AlreadyStarted, + AlreadyRunning, + NotRunning, MissingRuntimeDir(PathBuf), } diff --git a/src/main.rs b/src/main.rs index 8ecf7da..e779914 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,11 @@ fn main() { } if args.server { - server::run(args).unwrap(); + if server::is_running().unwrap() { + println!("Server is already running"); + } else { + server::run(args).unwrap(); + } return; } diff --git a/src/os_unix.rs b/src/os_unix.rs index c02a001..2f0eb7c 100644 --- a/src/os_unix.rs +++ b/src/os_unix.rs @@ -52,8 +52,8 @@ pub fn run_in_background() -> Result<(), ServerError> { pub fn kill() -> Result<(), ServerError> { let pid_path = get_pid_path()?; let socket_path = get_socket_path()?; - let pid = String::from_utf8(fs::read(&pid_path).map_err(|err| ServerError::Io(err))?) - .map_err(|err| ServerError::from_debuggable(err))?; + let pid = fs::read(&pid_path).map_err(|_| ServerError::NotRunning)?; + let pid = String::from_utf8(pid).map_err(|err| ServerError::from_debuggable(err))?; let pid = pid.trim(); Command::new("kill") .arg(pid) diff --git a/src/server.rs b/src/server.rs index da52579..b055491 100644 --- a/src/server.rs +++ b/src/server.rs @@ -153,13 +153,17 @@ fn handle_error(err: ServerError) -> i32 { eprintln!("IO error: {err}"); 2 } - ServerError::AlreadyStarted => { + ServerError::AlreadyRunning => { eprintln!("Server already running"); 100 } + ServerError::NotRunning => { + eprintln!("Server is not running"); + 101 + } ServerError::MissingRuntimeDir(path) => { eprintln!("Missing runtime directory: {}", path.to_string_lossy()); - 101 + 200 } } }