main.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. use clap::{IntoApp, Parser, Subcommand};
  2. use log::{debug, error};
  3. use serde_json::{json, Value};
  4. use url::Url;
  5. use darkfi::{
  6. rpc::{jsonrpc, jsonrpc::JsonResult},
  7. Error, Result,
  8. };
  9. #[derive(Subcommand)]
  10. pub enum CliDaoSubCommands {
  11. /// Say hello to the RPC
  12. Hello {},
  13. }
  14. /// DAO cli
  15. #[derive(Parser)]
  16. #[clap(name = "dao")]
  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 Client {
  25. url: String,
  26. }
  27. impl Client {
  28. pub fn new(url: String) -> Self {
  29. Self { url }
  30. }
  31. async fn request(&self, r: jsonrpc::JsonRequest) -> Result<Value> {
  32. let reply: JsonResult =
  33. match jsonrpc::send_request(&Url::parse(&self.url)?, json!(r), None).await {
  34. Ok(v) => v,
  35. Err(e) => return Err(e),
  36. };
  37. match reply {
  38. JsonResult::Resp(r) => {
  39. debug!(target: "RPC", "<-- {}", serde_json::to_string(&r)?);
  40. Ok(r.result)
  41. }
  42. JsonResult::Err(e) => {
  43. debug!(target: "RPC", "<-- {}", serde_json::to_string(&e)?);
  44. Err(Error::JsonRpcError(e.error.message.to_string()))
  45. }
  46. JsonResult::Notif(n) => {
  47. debug!(target: "RPC", "<-- {}", serde_json::to_string(&n)?);
  48. Err(Error::JsonRpcError("Unexpected reply".to_string()))
  49. }
  50. }
  51. }
  52. // --> {"jsonrpc": "2.0", "method": "say_hello", "params": [], "id": 42}
  53. // <-- {"jsonrpc": "2.0", "result": "hello world", "id": 42}
  54. async fn say_hello(&self) -> Result<Value> {
  55. let req = jsonrpc::request(json!("say_hello"), json!([]));
  56. Ok(self.request(req).await?)
  57. }
  58. }
  59. async fn start(options: CliDao) -> Result<()> {
  60. let rpc_addr = "tcp://127.0.0.1:7777";
  61. let client = Client::new(rpc_addr.to_string());
  62. match options.command {
  63. Some(CliDaoSubCommands::Hello {}) => {
  64. let reply = client.say_hello().await?;
  65. println!("Server replied: {}", &reply.to_string());
  66. return Ok(())
  67. }
  68. None => {}
  69. }
  70. error!("Please run 'dao help' to see usage.");
  71. Err(Error::MissingParams)
  72. }
  73. #[async_std::main]
  74. async fn main() -> Result<()> {
  75. let args = CliDao::parse();
  76. let _matches = CliDao::command().get_matches();
  77. //let config_path = if args.config.is_some() {
  78. // expand_path(&args.config.clone().unwrap())?
  79. //} else {
  80. // join_config_path(&PathBuf::from("drk.toml"))?
  81. //};
  82. // Spawn config file if it's not in place already.
  83. //spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  84. //let (lvl, conf) = log_config(matches)?;
  85. //TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  86. //let config = Config::<DrkConfig>::load(config_path)?;
  87. start(args).await
  88. }