main.rs 4.5 KB

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