aboutsummaryrefslogtreecommitdiff
path: root/karlc/src/main.rs
blob: bfd433e23af9b3a6a537b2751a35d2a4436dede3 (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
pub mod client;
pub mod pretty;

use std::{os::unix::net::UnixStream, process::exit};

use clap::{Parser, Subcommand};
use client::Client;
use karlcommon::{socket_path, version, ClientboundPacket, ServerboundPacket};
use log::{error, info};

/// 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 taskss
    ListTasks,
}

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::ListTasks => {
            client.send(ServerboundPacket::Download);
            if let ClientboundPacket::DownloadResponse(tasks) = client.receiver.recv().unwrap() {
                for t in tasks {
                    println!("{:?}", t)
                }
            }
        }
    }
}