From a947a2db2a5f3768d4dafd9c475876b39dfe5a59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 29 Jul 2023 16:52:39 +0200 Subject: [PATCH] feat: return correct exit code --- src/cli.rs | 26 +++++++++++--------------- src/daemon.rs | 2 +- src/main.rs | 7 ++++--- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index fb498bc..12d3aef 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -19,14 +19,14 @@ pub struct Cli { #[derive(Debug, Subcommand)] pub enum Command { /// Run as daemon - #[clap(visible_alias="d")] + #[clap(visible_alias = "d")] Daemon { /// do not send notifications #[arg(short, long)] no_notify: bool, }, /// Add a timer - #[clap(visible_alias="a")] + #[clap(visible_alias = "a")] Add { /// name of the timer name: String, @@ -34,24 +34,24 @@ pub enum Command { duration: humantime::Duration, }, /// List timers - #[clap(visible_alias="l")] + #[clap(visible_alias = "l")] List, /// Remove a timer - #[clap(visible_alias="r")] + #[clap(visible_alias = "r")] Remove { /// name of the timer to remove name: String, }, /// Pomodoro specific command #[command(subcommand)] - #[clap(visible_alias="p")] - Pomodoro(PomodoroCommand) + #[clap(visible_alias = "p")] + Pomodoro(PomodoroCommand), } #[derive(Debug, Subcommand)] pub enum PomodoroCommand { /// Start pomodoro - #[clap(visible_alias="s")] + #[clap(visible_alias = "s")] Start { /// duration to work for #[arg(long)] @@ -74,10 +74,10 @@ pub enum PomodoroCommand { pauses_till_long: u64, }, /// Stop the pomodoro - #[clap(visible_alias="p")] + #[clap(visible_alias = "p")] Remove, /// List the pomodoro settings and remaining duration - #[clap(visible_alias="l")] + #[clap(visible_alias = "l")] List, } @@ -86,7 +86,7 @@ fn get_stream(socket_path: &String) -> Result { .context(format!("Could not connect to socket {}!", socket_path)) } -pub fn send_command(socket_path: &String, command: OtherCommand) -> Result<()> { +pub fn send_command(socket_path: &String, command: OtherCommand) -> Result { let stream = get_stream(socket_path)?; serde_cbor::to_writer(&stream, &command).context("Could not write command!")?; stream @@ -94,9 +94,5 @@ pub fn send_command(socket_path: &String, command: OtherCommand) -> Result<()> { .context("Could not shutdown write!")?; let answer: Result = serde_cbor::from_reader(&stream).context("Could not read answer!")?; - match answer { - Ok(answer) => println!("{}", answer), - Err(err) => println!("Error: {}", err), - } - Ok(()) + Ok(answer?) } diff --git a/src/daemon.rs b/src/daemon.rs index 41fa1cd..9a08f99 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -36,7 +36,7 @@ pub enum Answer { impl Display for Answer { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { match self { - Answer::Ok => write!(f, "Ok"), + Answer::Ok => write!(f, ""), Answer::Timers(timers) => { if timers.is_empty() { writeln!(f, "No timers running.") diff --git a/src/main.rs b/src/main.rs index 72bfe2d..4af8bb2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,11 +5,10 @@ pub mod timer; use crate::cli::{send_command, Cli, Command as CliCommand}; use crate::daemon::{Command as DaemonCommand, Daemon}; -use anyhow::Result; use clap::Parser; use cli::PomodoroCommand; -fn main() -> Result<()> { +fn main() -> Result<(), anyhow::Error> { let args = Cli::parse(); let daemon_command = match args.command { CliCommand::Daemon { no_notify } => return Daemon::new(args.socket, no_notify)?.run(), @@ -34,5 +33,7 @@ fn main() -> Result<()> { PomodoroCommand::List => DaemonCommand::PomodoroList, }, }; - send_command(&args.socket, daemon_command) + let answer = send_command(&args.socket, daemon_command)?; + print!("{}", answer); + Ok(()) }