aboutsummaryrefslogtreecommitdiff
path: root/karlc/src/main.rs
diff options
context:
space:
mode:
authormetamuffin <yvchraiqi@protonmail.com>2022-06-10 17:48:32 +0200
committermetamuffin <yvchraiqi@protonmail.com>2022-06-10 17:48:32 +0200
commit45f2d1651f17d7e155748cfd1e1cd9a96f3e530e (patch)
tree6847a4552db06b56a5e0a55aa56354eccb7c41b1 /karlc/src/main.rs
parentd65b915f3dfda28aad6f2806df38c8ad77135d8b (diff)
downloadkarlender-45f2d1651f17d7e155748cfd1e1cd9a96f3e530e.tar
karlender-45f2d1651f17d7e155748cfd1e1cd9a96f3e530e.tar.bz2
karlender-45f2d1651f17d7e155748cfd1e1cd9a96f3e530e.tar.zst
better cli
Diffstat (limited to 'karlc/src/main.rs')
-rw-r--r--karlc/src/main.rs77
1 files changed, 70 insertions, 7 deletions
diff --git a/karlc/src/main.rs b/karlc/src/main.rs
index 9a89de9..ef56fd9 100644
--- a/karlc/src/main.rs
+++ b/karlc/src/main.rs
@@ -1,16 +1,19 @@
pub mod client;
use chrono::{NaiveDateTime, Utc};
-use clap::{Parser, Subcommand};
+use clap::{Args, Parser, Subcommand};
use client::Client;
-use karlcommon::{socket_path, version, ClientboundPacket, ServerboundPacket};
+use karlcommon::{socket_path, version, ClientboundPacket, ServerboundPacket, Task};
use log::{error, info};
-use std::{os::unix::net::UnixStream, process::exit};
+use std::{os::unix::net::UnixStream, path::PathBuf, process::exit};
/// CLI interface for karld
#[derive(Parser)]
#[clap(about, author, version)]
-struct Args {
+struct Arguments {
+ /// Custom path to the daemon socket
+ #[clap(long)]
+ socket_path: Option<PathBuf>,
#[clap(subcommand)]
action: Action,
}
@@ -21,13 +24,36 @@ pub enum Action {
Version,
/// List all tasks
List,
+ /// Remove a task by id
+ Remove { id: u64 },
+ /// Update a task by id
+ Update {
+ id: u64,
+ #[clap(flatten)]
+ task: TaskSpec,
+ },
+ /// Create a task
+ Create {
+ #[clap(flatten)]
+ task: TaskSpec,
+ },
+}
+
+#[derive(Args)]
+pub struct TaskSpec {
+ #[clap(short, long)]
+ name: String,
+ #[clap(short, long)]
+ description: Option<String>,
+ #[clap(short, long)]
+ tags: Vec<String>,
}
fn main() {
env_logger::init();
- let args = Args::parse();
+ let args = Arguments::parse();
- let socket = match UnixStream::connect(socket_path()) {
+ let socket = match UnixStream::connect(args.socket_path.unwrap_or(socket_path())) {
Ok(s) => s,
Err(e) => {
error!("failed to connect to socket: {}", e);
@@ -62,7 +88,11 @@ fn main() {
\x1b[38;2;100;255;100mDescription:\x1b[0m {}
\x1b[38;2;100;255;100mOccurence:\x1b[0m {:?}
\x1b[38;2;100;255;100mNext instances: \x1b[0m",
- t.id, t.name, t.description, t.occurence
+ t.id,
+ t.name,
+ t.description
+ .unwrap_or("\x1b[3m\x1b[2m(no description)\x1b[0m".to_string()),
+ t.occurence
);
client.send(ServerboundPacket::ListInstances {
task: t.id,
@@ -94,5 +124,38 @@ fn main() {
}
}
}
+ Action::Remove { id } => {
+ client.send(ServerboundPacket::RemoveTask(id));
+ }
+ Action::Update { id, task } => {
+ let mut t = task.build();
+ t.id = id;
+ client.send(ServerboundPacket::UpdateTask(t))
+ }
+ Action::Create { task } => client.send(ServerboundPacket::UpdateTask(task.build())),
+ }
+
+ // sync
+ client.send(ServerboundPacket::Sync);
+ for p in client.receiver.iter() {
+ if let ClientboundPacket::Sync = p {
+ break;
+ }
+ }
+}
+
+impl TaskSpec {
+ pub fn build(self) -> Task {
+ Task {
+ id: rand::random(),
+ name: self.name,
+ description: self.description,
+ tags: self.tags,
+ priority: 0.0,
+ completed: None,
+ scheduled: None,
+ occurence: None,
+ deadline: None,
+ }
}
}