main.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 res: Vec<&str> = res.iter().map(|r| r.as_str().unwrap()).collect();
  50. let (title, workspace, changes) = (res[0], res[1], res[2]);
  51. println!("WORKSPACE: {} FILE: {}", workspace, title);
  52. println!("{}", changes);
  53. println!("----------------------------------");
  54. }
  55. }
  56. #[async_std::main]
  57. async fn main() -> Result<()> {
  58. let args = Args::from_args();
  59. let log_level = get_log_level(args.verbose.into());
  60. let log_config = get_log_config();
  61. TermLogger::init(log_level, log_config, TerminalMode::Mixed, ColorChoice::Auto)?;
  62. let rpc_client = RpcClient::new(args.endpoint).await?;
  63. match args.sub_command {
  64. ArgsSubCommand::Update { dry_run, values } => {
  65. let req = JsonRequest::new("update", json!([dry_run, values]));
  66. let result = rpc_client.request(req).await?;
  67. let result = result.as_array().unwrap();
  68. let local_patches = result[0].as_array().unwrap();
  69. let sync_patches = result[1].as_array().unwrap();
  70. let merge_patches = result[2].as_array().unwrap();
  71. if !local_patches.is_empty() {
  72. println!();
  73. println!("PUBLISH LOCAL PATCHES:");
  74. println!();
  75. print_patches(local_patches);
  76. }
  77. if !sync_patches.is_empty() {
  78. println!();
  79. println!("RECEIVED PATCHES:");
  80. println!();
  81. print_patches(sync_patches);
  82. }
  83. if !merge_patches.is_empty() {
  84. println!();
  85. println!("MERGE:");
  86. println!();
  87. print_patches(merge_patches);
  88. }
  89. }
  90. ArgsSubCommand::Restore { dry_run, values } => {
  91. let req = JsonRequest::new("restore", json!([dry_run, values]));
  92. let result = rpc_client.request(req).await?;
  93. let result = result.as_array().unwrap();
  94. let patches = result[0].as_array().unwrap();
  95. if !patches.is_empty() {
  96. println!();
  97. println!("AFTER RESTORE:");
  98. println!();
  99. print_patches(patches);
  100. }
  101. }
  102. _ => unimplemented!(),
  103. }
  104. rpc_client.close().await
  105. }