main.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #[allow(clippy::single_match)]
  39. match options.command {
  40. Some(CliDaoSubCommands::Hello {}) => {
  41. let reply = client.say_hello().await?;
  42. println!("Server replied: {}", &reply.to_string());
  43. return Ok(())
  44. }
  45. None => {}
  46. }
  47. Ok(())
  48. }
  49. #[async_std::main]
  50. async fn main() -> Result<()> {
  51. let args = CliDao::parse();
  52. let _matches = CliDao::command().get_matches();
  53. //let config_path = if args.config.is_some() {
  54. // expand_path(&args.config.clone().unwrap())?
  55. //} else {
  56. // join_config_path(&PathBuf::from("drk.toml"))?
  57. //};
  58. // Spawn config file if it's not in place already.
  59. //spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  60. //let (lvl, conf) = log_config(matches)?;
  61. //TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  62. //let config = Config::<DrkConfig>::load(config_path)?;
  63. start(args).await
  64. }