Skip to main content

manycastr/cli/commands/
worker_list.rs

1use crate::custom_module::manycastr::{Status, WorkerStatus};
2use log::info;
3use prettytable::{Attr, Cell, Row, Table, color, format, row};
4use tonic::Response;
5
6/// Handle the worker-list command by pretty-printing the list of workers
7///
8/// # Arguments
9///
10/// * `response` - The response from the orchestrator containing the list of workers
11pub async fn handle(response: Response<Status>) {
12    // Perform the worker-list command
13    info!("[CLI] Requesting workers list from orchestrator");
14    // Pretty print to command-line
15    let mut table = Table::new();
16    table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
17    table.add_row(Row::new(vec![
18        Cell::new("Hostname")
19            .with_style(Attr::Bold)
20            .with_style(Attr::ForegroundColor(color::GREEN)),
21        Cell::new("Worker ID")
22            .with_style(Attr::Bold)
23            .with_style(Attr::ForegroundColor(color::GREEN)),
24        Cell::new("Status")
25            .with_style(Attr::Bold)
26            .with_style(Attr::ForegroundColor(color::GREEN)),
27        Cell::new("Unicast IPv4")
28            .with_style(Attr::Bold)
29            .with_style(Attr::ForegroundColor(color::GREEN)),
30        Cell::new("Unicast IPv6")
31            .with_style(Attr::Bold)
32            .with_style(Attr::ForegroundColor(color::GREEN)),
33    ]));
34
35    let mut connected_workers = 0;
36    let mut workers = response.into_inner().workers;
37    workers.sort_by_key(|a| a.worker_id);
38
39    for worker in workers {
40        let unicast_v4 = if let Some(addr) = &worker.unicast_v4 {
41            addr.to_string()
42        } else {
43            "N/A".to_string()
44        };
45
46        let unicast_v6 = if let Some(addr) = &worker.unicast_v6 {
47            addr.to_string()
48        } else {
49            "N/A".to_string()
50        };
51
52        table.add_row(row![
53            worker.hostname,
54            worker.worker_id,
55            worker.status().as_str_name(),
56            unicast_v4,
57            unicast_v6
58        ]);
59        if worker.status() != WorkerStatus::Disconnected {
60            connected_workers += 1;
61        }
62    }
63
64    table.printstd();
65    info!("[CLI] Total number of connected workers: {connected_workers}");
66}