main.rs 2.8 KB

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