main.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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. use std::sync::Arc;
  19. use clap::{Parser, Subcommand};
  20. use darkfi::{
  21. rpc::client::RpcClient,
  22. util::cli::{get_log_config, get_log_level},
  23. Result,
  24. };
  25. use simplelog::{ColorChoice, TermLogger, TerminalMode};
  26. use smol::Executor;
  27. use url::Url;
  28. use genevd::GenEvent;
  29. mod rpc;
  30. use rpc::Gen;
  31. #[derive(Parser)]
  32. #[clap(name = "genev", version)]
  33. struct Args {
  34. #[arg(short, action = clap::ArgAction::Count)]
  35. /// Increase verbosity (-vvv supported)
  36. verbose: u8,
  37. #[clap(short, long, default_value = "tcp://127.0.0.1:28880")]
  38. /// JSON-RPC endpoint
  39. endpoint: Url,
  40. #[clap(subcommand)]
  41. command: Option<SubCmd>,
  42. }
  43. #[derive(Subcommand)]
  44. enum SubCmd {
  45. Add { values: Vec<String> },
  46. List,
  47. }
  48. fn main() -> Result<()> {
  49. let args = Args::parse();
  50. let log_level = get_log_level(args.verbose);
  51. let log_config = get_log_config(args.verbose);
  52. TermLogger::init(log_level, log_config, TerminalMode::Mixed, ColorChoice::Auto)?;
  53. let executor = Arc::new(Executor::new());
  54. smol::block_on(executor.run(async {
  55. let rpc_client = RpcClient::new(args.endpoint, executor.clone()).await?;
  56. let gen = Gen { rpc_client };
  57. match args.command {
  58. Some(subcmd) => match subcmd {
  59. SubCmd::Add { values } => {
  60. let event = GenEvent {
  61. nick: values[0].clone(),
  62. title: values[1].clone(),
  63. text: values[2..].join(" "),
  64. };
  65. return gen.add(event).await
  66. }
  67. SubCmd::List => {
  68. let events = gen.list().await?;
  69. for event in events {
  70. println!("=============================");
  71. println!(
  72. "- nickname: {}, title: {}, text: {}",
  73. event.nick, event.title, event.text
  74. );
  75. }
  76. }
  77. },
  78. None => println!("none"),
  79. }
  80. gen.close_connection().await;
  81. Ok(())
  82. }))
  83. }