main.rs 2.6 KB

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