Files
scriptbase/sync-rs/src/main.rs
2025-06-20 22:07:16 +08:00

63 lines
1.9 KiB
Rust

#!/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://play.hatter.me/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; }
}
}
}
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20250620T220543+08:00.MEUCIHsFQ1sf53x3ATTLFkMQ
// oUau9y550grtnm7yGglEoDI/AiEAgZcouLIJXHP042IFnhkTljMo67O9Cs6MlpvNT+BzOA8=