main.rs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. use clap::{IntoApp, Parser, Subcommand};
  2. use serde_json::{json, Value};
  3. use url::Url;
  4. use darkfi::{
  5. rpc::{client::RpcClient, jsonrpc::JsonRequest},
  6. Result,
  7. };
  8. #[derive(Subcommand)]
  9. pub enum CliDaoSubCommands {
  10. /// Say hello to the RPC
  11. Hello {},
  12. }
  13. /// DAO cli
  14. #[derive(Parser)]
  15. #[clap(name = "dao")]
  16. #[clap(arg_required_else_help(true))]
  17. pub struct CliDao {
  18. /// Increase verbosity
  19. #[clap(short, parse(from_occurrences))]
  20. pub verbose: u8,
  21. #[clap(subcommand)]
  22. pub command: Option<CliDaoSubCommands>,
  23. }
  24. pub struct Rpc {
  25. client: RpcClient,
  26. }
  27. impl Rpc {
  28. // --> {"jsonrpc": "2.0", "method": "say_hello", "params": [], "id": 42}
  29. // <-- {"jsonrpc": "2.0", "result": "hello world", "id": 42}
  30. async fn say_hello(&self) -> Result<Value> {
  31. let req = JsonRequest::new("say_hello", json!([]));
  32. self.client.request(req).await
  33. }
  34. }
  35. async fn start(options: CliDao) -> Result<()> {
  36. let rpc_addr = "tcp://127.0.0.1:7777";
  37. let client = Rpc { client: RpcClient::new(Url::parse(rpc_addr)?).await? };
  38. match options.command {
  39. Some(CliDaoSubCommands::Hello {}) => {
  40. let reply = client.say_hello().await?;
  41. println!("Server replied: {}", &reply.to_string());
  42. return Ok(())
  43. }
  44. None => {}
  45. }
  46. Ok(())
  47. }
  48. #[async_std::main]
  49. async fn main() -> Result<()> {
  50. let args = CliDao::parse();
  51. let _matches = CliDao::command().get_matches();
  52. //let config_path = if args.config.is_some() {
  53. // expand_path(&args.config.clone().unwrap())?
  54. //} else {
  55. // join_config_path(&PathBuf::from("drk.toml"))?
  56. //};
  57. // Spawn config file if it's not in place already.
  58. //spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  59. //let (lvl, conf) = log_config(matches)?;
  60. //TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  61. //let config = Config::<DrkConfig>::load(config_path)?;
  62. start(args).await
  63. }