Skip to main content

manycastr/cli/commands/
mod.rs

1use crate::cli::client::CliClient;
2use crate::custom_module::manycastr::Empty;
3use crate::tls::TlsOptions;
4use bimap::BiHashMap;
5use clap::ArgMatches;
6use log::info;
7use std::error::Error;
8use tonic::Request;
9use tonic::codec::CompressionEncoding;
10pub(crate) mod start;
11mod worker_list;
12
13/// Execute the command-line arguments and send the desired commands to the orchestrator.
14///
15/// # Arguments
16/// * `args` - the user-defined command-line arguments
17#[tokio::main]
18pub async fn execute(args: &ArgMatches) -> Result<(), Box<dyn Error>> {
19    let server_address = args.get_one::<String>("orchestrator").unwrap();
20    let tls = TlsOptions::from_args(args);
21
22    // Connect with orchestrator
23    info!("[CLI] Connecting to orchestrator - {server_address}");
24    let mut grpc_client = CliClient::connect(server_address, &tls)
25        .await
26        .map_err(|e| format!("Unable to connect to orchestrator: {e}"))?
27        .send_compressed(CompressionEncoding::Zstd);
28
29    // Obtain connected worker information
30    let response = grpc_client
31        .list_workers(Request::new(Empty::default()))
32        .await
33        .map_err(|e| format!("Connection to orchestrator failed: {}", e.message()))?;
34
35    let mut cli_client = CliClient { grpc_client };
36
37    if args.subcommand_matches("worker-list").is_some() {
38        worker_list::handle(response).await
39    } else if let Some(matches) = args.subcommand_matches("start") {
40        // Map to convert hostnames to worker IDs and vice versa
41        let worker_map: BiHashMap<u32, String> = response
42            .into_inner()
43            .workers
44            .into_iter()
45            .map(|worker| (worker.worker_id, worker.hostname))
46            .collect();
47
48        start::handle(matches, &mut cli_client, worker_map).await?
49    } else {
50        panic!("Unrecognized command");
51    };
52    Ok(())
53}