flare_dht/
cli.rs

1use http::Uri;
2
3#[derive(clap::Parser, Clone, Debug)]
4#[clap(author, version, about, long_about = None)]
5pub struct FlareCli {
6    #[command(subcommand)]
7    pub command: FlareCommands,
8}
9
10#[derive(clap::Subcommand, Clone, Debug)]
11pub enum FlareCommands {
12    /// Start as server
13    Server(ServerArgs),
14    /// Collection operation
15    #[clap(aliases = &["col", "c"])]
16    Collection {
17        #[command(subcommand)]
18        opt: CollectionOperation,
19    },
20}
21
22#[derive(clap::Subcommand, Clone, Debug)]
23pub enum CollectionOperation {
24    #[clap(aliases = &["c"])]
25    Create {
26        name: String,
27        #[arg(default_value_t = 1)]
28        shard_count: u16,
29        #[clap(flatten)]
30        connection: ConnectionArgs,
31    },
32}
33
34#[derive(clap::Args, Debug, Clone)]
35pub struct ConnectionArgs {
36    #[arg(short, long, default_value = "http://127.0.0.1:8001")]
37    pub server_url: Uri,
38}
39
40#[derive(clap::Args, Debug, Clone, Default)]
41pub struct ServerArgs {
42    /// advertisement address
43    pub addr: Option<String>,
44    /// gRPC port
45    #[arg(short, long, env = "FLARE_PORT", default_value = "8001")]
46    pub port: u16,
47    /// Cluster id
48    #[arg(short, long, default_value = "default")]
49    pub cluster_id: String,
50    /// if start as Raft leader
51    #[arg(short, long)]
52    pub leader: bool,
53    #[arg(long, default_value = "false")]
54    pub not_server: bool,
55    /// Address to join the Raft cluster
56    #[arg(long)]
57    pub peer_addr: Option<String>,
58    /// Node ID. Randomized, if none.
59    #[arg(short, long)]
60    pub node_id: Option<u64>,
61}
62
63impl ServerArgs {
64    pub fn get_node_id(&self) -> u64 {
65        if let Some(id) = self.node_id {
66            return id;
67        }
68        rand::random()
69    }
70
71    pub fn get_addr(&self) -> String {
72        if let Some(addr) = &self.addr {
73            return addr.clone();
74        }
75        return format!("http://127.0.0.1:{}", self.port);
76    }
77}