genev/
main.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use std::sync::Arc;
20
21use clap::{Parser, Subcommand};
22use darkfi::{
23    rpc::client::RpcClient,
24    util::cli::{get_log_config, get_log_level},
25    Result,
26};
27use simplelog::{ColorChoice, TermLogger, TerminalMode};
28use smol::Executor;
29use url::Url;
30
31use genevd::GenEvent;
32
33mod rpc;
34use rpc::Gen;
35
36#[derive(Parser)]
37#[clap(name = "genev", version)]
38struct Args {
39    #[arg(short, action = clap::ArgAction::Count)]
40    /// Increase verbosity (-vvv supported)
41    verbose: u8,
42
43    #[clap(short, long, default_value = "tcp://127.0.0.1:28880")]
44    /// JSON-RPC endpoint
45    endpoint: Url,
46
47    #[clap(subcommand)]
48    command: Option<SubCmd>,
49}
50
51#[derive(Subcommand)]
52enum SubCmd {
53    Add { values: Vec<String> },
54
55    List,
56}
57
58fn main() -> Result<()> {
59    let args = Args::parse();
60
61    let log_level = get_log_level(args.verbose);
62    let log_config = get_log_config(args.verbose);
63    TermLogger::init(log_level, log_config, TerminalMode::Mixed, ColorChoice::Auto)?;
64
65    let executor = Arc::new(Executor::new());
66
67    smol::block_on(executor.run(async {
68        let rpc_client = RpcClient::new(args.endpoint, executor.clone()).await?;
69        let gen = Gen { rpc_client };
70
71        match args.command {
72            Some(subcmd) => match subcmd {
73                SubCmd::Add { values } => {
74                    let event = GenEvent {
75                        nick: values[0].clone(),
76                        title: values[1].clone(),
77                        text: values[2..].join(" "),
78                    };
79
80                    return gen.add(event).await
81                }
82
83                SubCmd::List => {
84                    let events = gen.list().await?;
85                    for event in events {
86                        println!("=============================");
87                        println!(
88                            "- nickname: {}, title: {}, text: {}",
89                            event.nick, event.title, event.text
90                        );
91                    }
92                }
93            },
94            None => println!("none"),
95        }
96
97        gen.close_connection().await;
98
99        Ok(())
100    }))
101}