feat: add sync-rs

This commit is contained in:
2023-02-20 23:54:08 +08:00
parent f229156f70
commit 1bdc08307f
3 changed files with 276 additions and 0 deletions

59
sync-rs/src/main.rs Normal file
View File

@@ -0,0 +1,59 @@
#!/usr/bin/env runrs
//! ```cargo
//! [dependencies]
//! rust_util = { version = "0.6" }
//! ```
use std::env;
use std::path::PathBuf;
use std::process::Command;
use rust_util::{failure_and_exit, information, success, warning};
fn main() -> Result<(), Box<dyn std::error::Error>> {
if let Some(args_1) = env::args().nth(1) {
match args_1.as_str() {
"md" => {
Command::new("markdowndocs.js").spawn()?.wait()?;
}
"ms" => {
failure_and_exit!("ms is not implemented!");
}
_ => {} // IGNORE
}
}
information!("Git Add All");
Command::new("git").args(&["add", "."]).spawn()?.wait()?;
information!("Git Commit All");
Command::new("git").args(&["commit", "-a", "-m", "'auto sync'"]).spawn()?.wait()?;
information!("Git Push");
Command::new("git").args(&["push"]).spawn()?.wait()?;
information!("Sync Server Repo");
let page_name = match find_parents_exists_dir("page") {
Some(path) => path.file_name().unwrap().to_str().unwrap().to_owned(),
None => {
warning!("Cannot find page name!");
return Ok(());
}
};
let sync_url = "https://playsecurity.org/update_pages?".to_owned() + &page_name;
information!("curl URL: {}", sync_url);
Command::new("curl").args(&[sync_url]).spawn()?.wait()?;
success!("Sync success!");
Ok(())
}
// ------------------------------------------------------------------------------------ //
pub fn find_parents_exists_dir(dir: &str) -> Option<PathBuf> {
match PathBuf::from(".").canonicalize() {
Err(_) => None,
Ok(mut path) => loop {
if path.join(dir).is_dir() { return Some(path); }
if !path.pop() { return None; }
}
}
}