pub mod client; use chrono::{NaiveDateTime, Utc}; use clap::{Parser, Subcommand}; use client::Client; use karlcommon::{socket_path, version, ClientboundPacket, ServerboundPacket}; use log::{error, info}; use std::{os::unix::net::UnixStream, process::exit}; /// CLI interface for karld #[derive(Parser)] #[clap(about, author, version)] struct Args { #[clap(subcommand)] action: Action, } #[derive(Subcommand)] pub enum Action { /// Show version of the client and daemon Version, /// List all tasks List, } fn main() { env_logger::init(); let args = Args::parse(); let socket = match UnixStream::connect(socket_path()) { Ok(s) => s, Err(e) => { error!("failed to connect to socket: {}", e); exit(1); } }; let mut client = Client::new(socket); info!("connected"); let handshake = client.receiver.recv().unwrap(); match args.action { Action::Version => { if let ClientboundPacket::Handshake { version: daemon_version, } = handshake { println!("{}", version!()); println!("{daemon_version}"); } else { error!("handshake is not the first packet") } } Action::List => { client.send(ServerboundPacket::ListTasks); if let ClientboundPacket::TaskList(tasks) = client.receiver.recv().unwrap() { for t in tasks { print!( " - \x1b[4m\x1b[1mTASK {}\x1b[0m \x1b[38;2;100;255;100mName:\x1b[0m {} \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 ); client.send(ServerboundPacket::ListInstances { task: t.id, limit: 5, range: Some(Utc::now().naive_local().timestamp())..None, }); if let ClientboundPacket::InstanceList(instances) = client.receiver.recv().unwrap() { for i in instances { println!( "\x1b[19G{} - {}", i.at.start .map(|e| format!( "{}", NaiveDateTime::from_timestamp(e as i64, 0) )) .unwrap_or("...".to_string()), i.at.end .map(|e| format!( "{}", NaiveDateTime::from_timestamp(e as i64, 0) )) .unwrap_or("...".to_string()), ); } } println!(); } } } } }