flare_dht/
cli.rs

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
use http::Uri;

#[derive(clap::Parser, Clone, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct FlareCli {
    #[command(subcommand)]
    pub command: FlareCommands,
}

#[derive(clap::Subcommand, Clone, Debug)]
pub enum FlareCommands {
    /// Start as server
    Server(ServerArgs),
    /// Collection operation
    #[clap(aliases = &["col", "c"])]
    Collection {
        #[command(subcommand)]
        opt: CollectionOperation,
    },
}

#[derive(clap::Subcommand, Clone, Debug)]
pub enum CollectionOperation {
    #[clap(aliases = &["c"])]
    Create {
        name: String,
        #[arg(default_value_t = 1)]
        shard_count: u16,
        #[clap(flatten)]
        connection: ConnectionArgs,
    },
}

#[derive(clap::Args, Debug, Clone)]
pub struct ConnectionArgs {
    #[arg(short, long, default_value = "http://127.0.0.1:8001")]
    pub server_url: Uri,
}

#[derive(clap::Args, Debug, Clone, Default)]
pub struct ServerArgs {
    /// advertisement address
    pub addr: Option<String>,
    /// gRPC port
    #[arg(short, long, env = "FLARE_PORT", default_value = "8001")]
    pub port: u16,
    /// Cluster id
    #[arg(short, long, default_value = "default")]
    pub cluster_id: String,
    /// if start as Raft leader
    #[arg(short, long)]
    pub leader: bool,
    #[arg(long, default_value = "false")]
    pub not_server: bool,
    /// Address to join the Raft cluster
    #[arg(long)]
    pub peer_addr: Option<String>,
    /// Node ID. Randomized, if none.
    #[arg(short, long)]
    pub node_id: Option<u64>,
}

impl ServerArgs {
    pub fn get_node_id(&self) -> u64 {
        if let Some(id) = self.node_id {
            return id;
        }
        rand::random()
    }

    pub fn get_addr(&self) -> String {
        if let Some(addr) = &self.addr {
            return addr.clone();
        }
        return format!("http://127.0.0.1:{}", self.port);
    }
}