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"
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"

View File

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

View File

@ -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;
}

View File

@ -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)

View File

@ -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
}
}
}