main.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. use serde_json::json;
  2. use simplelog::{ColorChoice, TermLogger, TerminalMode};
  3. use structopt::StructOpt;
  4. use url::Url;
  5. use darkfi::{
  6. rpc::{client::RpcClient, jsonrpc::JsonRequest},
  7. util::cli::{get_log_config, get_log_level},
  8. Result,
  9. };
  10. #[derive(Clone, Debug, StructOpt)]
  11. #[structopt(name = "darkwikiupdate")]
  12. struct Args {
  13. #[structopt(subcommand)]
  14. sub_command: ArgsSubCommand,
  15. #[structopt(short, parse(from_occurrences))]
  16. /// Increase verbosity (-vvv supported)
  17. verbose: u8,
  18. #[structopt(short, long, default_value = "tcp://127.0.0.1:24330")]
  19. /// darkfid JSON-RPC endpoint
  20. endpoint: Url,
  21. }
  22. #[derive(Debug, Clone, PartialEq, StructOpt)]
  23. enum ArgsSubCommand {
  24. /// Publish local patches and merging received patches
  25. Update {
  26. #[structopt(long, short)]
  27. /// Run without applying the changes
  28. dry_run: bool,
  29. /// Names of files to update (Note: Will update all the documents if left empty)
  30. values: Vec<String>,
  31. },
  32. /// Show the history of patches
  33. Log {
  34. /// Names of files to log (Note: Will show all the log if left empty)
  35. values: Vec<String>,
  36. },
  37. /// Undo the local changes
  38. Restore {
  39. #[structopt(long, short)]
  40. /// Run without applying the changes
  41. dry_run: bool,
  42. /// Names of files to restore (Note: Will restore all the documents if left empty)
  43. values: Vec<String>,
  44. },
  45. }
  46. fn print_patches(value: &Vec<serde_json::Value>) {
  47. for res in value {
  48. let res = res.as_array().unwrap();
  49. let (title, changes) = (res[0].as_str().unwrap(), res[1].as_str().unwrap());
  50. println!("FILE: {}", title);
  51. println!("{}", changes);
  52. println!("----------------------------------");
  53. }
  54. }
  55. #[async_std::main]
  56. async fn main() -> Result<()> {
  57. let args = Args::from_args();
  58. let log_level = get_log_level(args.verbose.into());
  59. let log_config = get_log_config();
  60. TermLogger::init(log_level, log_config, TerminalMode::Mixed, ColorChoice::Auto)?;
  61. let rpc_client = RpcClient::new(args.endpoint).await?;
  62. match args.sub_command {
  63. ArgsSubCommand::Update { dry_run, values } => {
  64. let req = JsonRequest::new("update", json!([dry_run, values]));
  65. let result = rpc_client.request(req).await?;
  66. let result = result.as_array().unwrap();
  67. let local_patches = result[0].as_array().unwrap();
  68. let sync_patches = result[1].as_array().unwrap();
  69. let merge_patches = result[2].as_array().unwrap();
  70. if !local_patches.is_empty() {
  71. println!();
  72. println!("PUBLISH LOCAL PATCHES:");
  73. println!();
  74. print_patches(local_patches);
  75. }
  76. if !sync_patches.is_empty() {
  77. println!();
  78. println!("RECEIVED PATCHES:");
  79. println!();
  80. print_patches(sync_patches);
  81. }
  82. if !merge_patches.is_empty() {
  83. println!();
  84. println!("MERGE:");
  85. println!();
  86. print_patches(merge_patches);
  87. }
  88. }
  89. ArgsSubCommand::Restore { dry_run, values } => {
  90. let req = JsonRequest::new("restore", json!([dry_run, values]));
  91. let result = rpc_client.request(req).await?;
  92. let result = result.as_array().unwrap();
  93. let patches = result[0].as_array().unwrap();
  94. if !patches.is_empty() {
  95. println!();
  96. println!("AFTER RESTORE:");
  97. println!();
  98. print_patches(patches);
  99. }
  100. }
  101. _ => unimplemented!(),
  102. }
  103. rpc_client.close().await
  104. }