Better handling for server commandline args

master
hheik 2024-02-24 16:05:51 +02:00
parent a3f03702df
commit 6d673b5d9c
5 changed files with 16 additions and 16 deletions

View File

@ -12,14 +12,5 @@ crc32fast = "1.3.2"
crossterm = "0.27.0" crossterm = "0.27.0"
interprocess = "1.2.1" interprocess = "1.2.1"
ratatui = "0.23.0" ratatui = "0.23.0"
rodio = {version = "0.17.3", features = [ rodio = {version = "0.17.3", features = [ "symphonia-all" ], default-features = false }
"claxon",
"flac",
"hound",
"lewton",
"mp3",
"symphonia-all",
"vorbis",
"wav",
]}
serde = "1.0.196" serde = "1.0.196"

View File

@ -62,7 +62,8 @@ pub mod server {
pub enum ServerError { pub enum ServerError {
Other(String), Other(String),
Io(std::io::Error), Io(std::io::Error),
AlreadyStarted, AlreadyRunning,
NotRunning,
MissingRuntimeDir(PathBuf), MissingRuntimeDir(PathBuf),
} }

View File

@ -52,7 +52,11 @@ fn main() {
} }
if args.server { if args.server {
server::run(args).unwrap(); if server::is_running().unwrap() {
println!("Server is already running");
} else {
server::run(args).unwrap();
}
return; return;
} }

View File

@ -52,8 +52,8 @@ pub fn run_in_background() -> Result<(), ServerError> {
pub fn kill() -> Result<(), ServerError> { pub fn kill() -> Result<(), ServerError> {
let pid_path = get_pid_path()?; let pid_path = get_pid_path()?;
let socket_path = get_socket_path()?; let socket_path = get_socket_path()?;
let pid = String::from_utf8(fs::read(&pid_path).map_err(|err| ServerError::Io(err))?) let pid = fs::read(&pid_path).map_err(|_| ServerError::NotRunning)?;
.map_err(|err| ServerError::from_debuggable(err))?; let pid = String::from_utf8(pid).map_err(|err| ServerError::from_debuggable(err))?;
let pid = pid.trim(); let pid = pid.trim();
Command::new("kill") Command::new("kill")
.arg(pid) .arg(pid)

View File

@ -153,13 +153,17 @@ fn handle_error(err: ServerError) -> i32 {
eprintln!("IO error: {err}"); eprintln!("IO error: {err}");
2 2
} }
ServerError::AlreadyStarted => { ServerError::AlreadyRunning => {
eprintln!("Server already running"); eprintln!("Server already running");
100 100
} }
ServerError::NotRunning => {
eprintln!("Server is not running");
101
}
ServerError::MissingRuntimeDir(path) => { ServerError::MissingRuntimeDir(path) => {
eprintln!("Missing runtime directory: {}", path.to_string_lossy()); eprintln!("Missing runtime directory: {}", path.to_string_lossy());
101 200
} }
} }
} }