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

@ -45,7 +45,7 @@ pub enum Command {
/// 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)]
@ -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(())
} }