aboutsummaryrefslogtreecommitdiff
path: root/karlc/src/main.rs
blob: 9a89de9ee43cf6df34006ae23a959728f5003a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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!();
                }
            }
        }
    }
}