timers/src/cli.rs

72 lines
2.0 KiB
Rust
Raw Normal View History

2023-07-29 10:41:56 +02:00
use crate::daemon::{Answer, AnswerErr, Command as OtherCommand};
2023-07-28 19:52:23 +02:00
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use std::net::Shutdown;
use std::os::unix::net::UnixStream;
use std::time::Duration;
2023-07-28 19:52:23 +02:00
#[derive(Debug, Parser)]
#[command(name = "timers")]
#[command(about = "A advanced timer daemon/cli.", long_about = None)]
#[command(arg_required_else_help = true)]
pub struct Cli {
#[command(subcommand)]
pub command: Command,
#[arg(short, long)]
#[clap(default_value = "/tmp/timers.socket")]
pub socket: String,
}
#[derive(Debug, Subcommand)]
pub enum Command {
2023-07-29 10:41:56 +02:00
Daemon {
#[arg(short, long)]
notify: bool,
},
Add {
name: String,
duration: humantime::Duration,
2023-07-29 10:41:56 +02:00
},
2023-07-28 19:52:23 +02:00
List,
2023-07-29 10:41:56 +02:00
Remove {
name: String,
},
#[command(subcommand)]
Pomodoro(PomodoroCommand)
}
#[derive(Debug, Subcommand)]
pub enum PomodoroCommand {
Start {
#[clap(default_value_t = Duration::from_secs(25 * 60).into())]
work: humantime::Duration,
#[clap(default_value_t = Duration::from_secs(5 * 60).into())]
pause: humantime::Duration,
#[clap(default_value_t = Duration::from_secs(10 * 60).into())]
long_pause: humantime::Duration,
2023-07-29 12:15:19 +02:00
#[clap(default_value_t = 3)]
pauses_till_long: u64,
},
Stop,
2023-07-28 19:52:23 +02:00
}
fn get_stream(socket_path: &String) -> Result<UnixStream> {
UnixStream::connect(socket_path)
.context(format!("Could not connect to socket {}!", socket_path))
}
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
.shutdown(Shutdown::Write)
.context("Could not shutdown write!")?;
2023-07-29 10:41:56 +02:00
let answer: Result<Answer, AnswerErr> =
serde_cbor::from_reader(&stream).context("Could not read answer!")?;
2023-07-28 19:52:23 +02:00
match answer {
Ok(answer) => println!("{}", answer),
Err(err) => println!("Error: {}", err),
}
Ok(())
}