feat: return correct exit code

This commit is contained in:
Moritz Böhme 2023-07-29 16:52:39 +02:00
parent 56beda6840
commit a947a2db2a
Signed by: moritz
GPG key ID: 970C6E89EB0547A9
3 changed files with 16 additions and 19 deletions

View file

@ -19,14 +19,14 @@ pub struct Cli {
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub enum Command { pub enum Command {
/// Run as daemon /// Run as daemon
#[clap(visible_alias="d")] #[clap(visible_alias = "d")]
Daemon { Daemon {
/// do not send notifications /// do not send notifications
#[arg(short, long)] #[arg(short, long)]
no_notify: bool, no_notify: bool,
}, },
/// Add a timer /// Add a timer
#[clap(visible_alias="a")] #[clap(visible_alias = "a")]
Add { Add {
/// name of the timer /// name of the timer
name: String, name: String,
@ -34,24 +34,24 @@ pub enum Command {
duration: humantime::Duration, duration: humantime::Duration,
}, },
/// List timers /// List timers
#[clap(visible_alias="l")] #[clap(visible_alias = "l")]
List, List,
/// Remove a timer /// Remove a timer
#[clap(visible_alias="r")] #[clap(visible_alias = "r")]
Remove { Remove {
/// name of the timer to remove /// name of the timer to remove
name: String, name: String,
}, },
/// Pomodoro specific command /// Pomodoro specific command
#[command(subcommand)] #[command(subcommand)]
#[clap(visible_alias="p")] #[clap(visible_alias = "p")]
Pomodoro(PomodoroCommand) Pomodoro(PomodoroCommand),
} }
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
pub enum PomodoroCommand { pub enum PomodoroCommand {
/// Start pomodoro /// Start pomodoro
#[clap(visible_alias="s")] #[clap(visible_alias = "s")]
Start { Start {
/// duration to work for /// duration to work for
#[arg(long)] #[arg(long)]
@ -74,10 +74,10 @@ pub enum PomodoroCommand {
pauses_till_long: u64, pauses_till_long: u64,
}, },
/// Stop the pomodoro /// Stop the pomodoro
#[clap(visible_alias="p")] #[clap(visible_alias = "p")]
Remove, Remove,
/// List the pomodoro settings and remaining duration /// List the pomodoro settings and remaining duration
#[clap(visible_alias="l")] #[clap(visible_alias = "l")]
List, List,
} }
@ -86,7 +86,7 @@ fn get_stream(socket_path: &String) -> Result<UnixStream> {
.context(format!("Could not connect to socket {}!", socket_path)) .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<Answer> {
let stream = get_stream(socket_path)?; let stream = get_stream(socket_path)?;
serde_cbor::to_writer(&stream, &command).context("Could not write command!")?; serde_cbor::to_writer(&stream, &command).context("Could not write command!")?;
stream stream
@ -94,9 +94,5 @@ pub fn send_command(socket_path: &String, command: OtherCommand) -> Result<()> {
.context("Could not shutdown write!")?; .context("Could not shutdown write!")?;
let answer: Result<Answer, AnswerErr> = let answer: Result<Answer, AnswerErr> =
serde_cbor::from_reader(&stream).context("Could not read answer!")?; serde_cbor::from_reader(&stream).context("Could not read answer!")?;
match answer { Ok(answer?)
Ok(answer) => println!("{}", answer),
Err(err) => println!("Error: {}", err),
}
Ok(())
} }

View file

@ -36,7 +36,7 @@ pub enum Answer {
impl Display for Answer { impl Display for Answer {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
match self { match self {
Answer::Ok => write!(f, "Ok"), Answer::Ok => write!(f, ""),
Answer::Timers(timers) => { Answer::Timers(timers) => {
if timers.is_empty() { if timers.is_empty() {
writeln!(f, "No timers running.") writeln!(f, "No timers running.")

View file

@ -5,11 +5,10 @@ pub mod timer;
use crate::cli::{send_command, Cli, Command as CliCommand}; use crate::cli::{send_command, Cli, Command as CliCommand};
use crate::daemon::{Command as DaemonCommand, Daemon}; use crate::daemon::{Command as DaemonCommand, Daemon};
use anyhow::Result;
use clap::Parser; use clap::Parser;
use cli::PomodoroCommand; use cli::PomodoroCommand;
fn main() -> Result<()> { fn main() -> Result<(), anyhow::Error> {
let args = Cli::parse(); let args = Cli::parse();
let daemon_command = match args.command { let daemon_command = match args.command {
CliCommand::Daemon { no_notify } => return Daemon::new(args.socket, no_notify)?.run(), CliCommand::Daemon { no_notify } => return Daemon::new(args.socket, no_notify)?.run(),
@ -34,5 +33,7 @@ fn main() -> Result<()> {
PomodoroCommand::List => DaemonCommand::PomodoroList, PomodoroCommand::List => DaemonCommand::PomodoroList,
}, },
}; };
send_command(&args.socket, daemon_command) let answer = send_command(&args.socket, daemon_command)?;
print!("{}", answer);
Ok(())
} }