main.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 log::info;
  20. use serde_json::json;
  21. use simplelog::{ColorChoice, TermLogger, TerminalMode};
  22. use url::Url;
  23. use darkfi::{
  24. cli_desc,
  25. rpc::{client::RpcClient, jsonrpc::JsonRequest},
  26. util::cli::{get_log_config, get_log_level},
  27. Result,
  28. };
  29. #[derive(Parser)]
  30. #[clap(name = "fu", about = cli_desc!(), version)]
  31. #[clap(arg_required_else_help(true))]
  32. struct Args {
  33. #[clap(short, action = clap::ArgAction::Count)]
  34. /// Increase verbosity (-vvv supported)
  35. verbose: u8,
  36. #[clap(short, long, default_value = "tcp://127.0.0.1:13336")]
  37. /// fud JSON-RPC endpoint
  38. endpoint: Url,
  39. #[clap(subcommand)]
  40. command: Subcmd,
  41. }
  42. #[derive(Subcommand)]
  43. enum Subcmd {
  44. /// List fud folder contents
  45. List,
  46. /// Sync fud folder contents and signal network for record changes
  47. Sync,
  48. /// Retrieve provided file name from the fud network
  49. Get {
  50. #[clap(short, long)]
  51. /// File name
  52. file: String,
  53. },
  54. }
  55. struct Fu {
  56. pub rpc_client: RpcClient,
  57. }
  58. impl Fu {
  59. async fn close_connection(&self) -> Result<()> {
  60. self.rpc_client.close().await
  61. }
  62. async fn list(&self) -> Result<()> {
  63. let req = JsonRequest::new("list", json!([]));
  64. let rep = self.rpc_client.request(req).await?;
  65. // Extract response
  66. let content = rep[0].as_array().unwrap();
  67. let new = rep[1].as_array().unwrap();
  68. let deleted = rep[2].as_array().unwrap();
  69. // Print info
  70. info!("----------Content-------------");
  71. if content.is_empty() {
  72. info!("No file records exists in DHT.");
  73. } else {
  74. for name in content {
  75. info!("\t{}", name.as_str().unwrap());
  76. }
  77. }
  78. info!("------------------------------");
  79. info!("----------New files-----------");
  80. if new.is_empty() {
  81. info!("No new files to import.");
  82. } else {
  83. for name in new {
  84. info!("\t{}", name.as_str().unwrap());
  85. }
  86. }
  87. info!("------------------------------");
  88. info!("----------Removed keys--------");
  89. if deleted.is_empty() {
  90. info!("No keys were removed.");
  91. } else {
  92. for key in deleted {
  93. info!("\t{}", key.as_str().unwrap());
  94. }
  95. }
  96. info!("------------------------------");
  97. Ok(())
  98. }
  99. async fn sync(&self) -> Result<()> {
  100. let req = JsonRequest::new("sync", json!([]));
  101. self.rpc_client.request(req).await?;
  102. info!("Daemon synced successfully!");
  103. Ok(())
  104. }
  105. async fn get(&self, file: String) -> Result<()> {
  106. let req = JsonRequest::new("get", json!([file]));
  107. let rep = self.rpc_client.request(req).await?;
  108. let path = rep.as_str().unwrap();
  109. info!("File waits you at: {}", path);
  110. Ok(())
  111. }
  112. }
  113. #[async_std::main]
  114. async fn main() -> Result<()> {
  115. let args = Args::parse();
  116. let log_level = get_log_level(args.verbose.into());
  117. let log_config = get_log_config();
  118. TermLogger::init(log_level, log_config, TerminalMode::Mixed, ColorChoice::Auto)?;
  119. let rpc_client = RpcClient::new(args.endpoint).await?;
  120. let fu = Fu { rpc_client };
  121. match args.command {
  122. Subcmd::List => fu.list().await,
  123. Subcmd::Sync => fu.sync().await,
  124. Subcmd::Get { file } => fu.get(file).await,
  125. }?;
  126. fu.close_connection().await
  127. }