main.rs 2.6 KB

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