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)]
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<UnixStream> {
.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)?;
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<Answer, AnswerErr> =
serde_cbor::from_reader(&stream).context("Could not read answer!")?;
match answer {
Ok(answer) => println!("{}", answer),
Err(err) => println!("Error: {}", err),
}
Ok(())
Ok(answer?)
}

View file

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

View file

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