From beed2f16ab94321eb8291686d9218c18394078c3 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 2 May 2020 23:49:32 +0800 Subject: [PATCH] use rust_util 0.2.3 --- Cargo.lock | 6 ++--- Cargo.toml | 2 +- src/config_util.rs | 30 +++++++++++++---------- src/main.rs | 60 +++++++++++++++++++++++----------------------- src/zip_util.rs | 10 ++++---- 5 files changed, 56 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 87b4710..b031025 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1077,7 +1077,7 @@ dependencies = [ "indicatif 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", "json 0.11.15 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)", - "rust_util 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "rust_util 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "sequoia-openpgp 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)", "sha-1 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1433,7 +1433,7 @@ dependencies = [ [[package]] name = "rust_util" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2252,7 +2252,7 @@ dependencies = [ "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" "checksum reqwest 0.9.24 (registry+https://github.com/rust-lang/crates.io-index)" = "f88643aea3c1343c804950d7bf983bd2067f5ab59db6d613a08e05572f2714ab" "checksum rust-argon2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017" -"checksum rust_util 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd9973675144c03fdf6f9a559ea5c254b0008e90a8992b1f317f728edd3512bc" +"checksum rust_util 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "454c4fd8ae19dd245bdc073cb297e222362adf938c3e6f0c2f8ea1abc7be2fd0" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum rustc-hash 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" diff --git a/Cargo.toml b/Cargo.toml index e8aae50..cfa4d19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,4 @@ chrono = "0.4.10" zip = "0.5.3" tar = "0.4.26" flate2 = "1.0.14" -rust_util = "0.2.1" +rust_util = "0.2.3" diff --git a/src/config_util.rs b/src/config_util.rs index 2ec7271..bfe9542 100644 --- a/src/config_util.rs +++ b/src/config_util.rs @@ -7,7 +7,11 @@ use rust_util::{ iff, XResult, new_box_ioerror, - util_msg::*, + util_msg::{ + print_warn, + print_error, + print_debug, + }, }; use chrono::Utc; @@ -137,7 +141,7 @@ pub fn get_config_json(custom_oss_backupd_config: Option<&str>, verbose: bool) - let config_content = get_config_content(custom_oss_backupd_config, verbose)?; match json::parse(&config_content) { Ok(o) => Some(o), Err(e) => { - print_message(MessageType::ERROR, &format!("Parse config json failed: {}", e)); + print_error(&format!("Parse config json failed: {}", e)); None }, } @@ -247,18 +251,18 @@ fn get_u32_value(json: &json::JsonValue, key: &str) -> Option { fn get_config_content(custom_oss_backupd_config: Option<&str>, verbose: bool) -> Option { if let Some(custom_oss_backupd_config_val) = custom_oss_backupd_config { if verbose { - print_message(MessageType::DEBUG, &format!("Read config from: {}", custom_oss_backupd_config_val)); + print_debug(&format!("Read config from: {}", custom_oss_backupd_config_val)); } let custom_oss_backupd_config_path = Path::new(custom_oss_backupd_config_val); if custom_oss_backupd_config_path.exists() { return match fs::read_to_string(custom_oss_backupd_config_path) { Ok(o) => Some(o), Err(e) => { - print_message(MessageType::ERROR, &format!("Read config file {} error: {}", custom_oss_backupd_config_val, e)); + print_error(&format!("Read config file {} error: {}", custom_oss_backupd_config_val, e)); None }, }; } else { - print_message(MessageType::ERROR, &format!("Custom config file not found: {}", custom_oss_backupd_config_val)); + print_error(&format!("Custom config file not found: {}", custom_oss_backupd_config_val)); return None; } } @@ -266,18 +270,18 @@ fn get_config_content(custom_oss_backupd_config: Option<&str>, verbose: bool) -> let oss_backupd_config_path = Path::new(OSS_BACKUPD_CONFIG); if oss_backupd_config_path.exists() { if verbose { - print_message(MessageType::DEBUG, &format!("Read config from: {}", OSS_BACKUPD_CONFIG)); + print_debug(&format!("Read config from: {}", OSS_BACKUPD_CONFIG)); } return match fs::read_to_string(oss_backupd_config_path) { Ok(o) => Some(o), Err(e) => { - print_message(MessageType::ERROR, &format!("Read config file {} error: {}", OSS_BACKUPD_CONFIG, e)); + print_error(&format!("Read config file {} error: {}", OSS_BACKUPD_CONFIG, e)); None }, }; } let home_dot_oss_backupd_config = & match get_user_home_dir(DOT_OSS_BACKUPD_CONFIG) { Ok(o) => o, Err(e) => { - print_message(MessageType::WARN, &format!("Get user home error: {}", e)); + print_warn(&format!("Get user home error: {}", e)); String::new() }, }; @@ -285,11 +289,11 @@ fn get_config_content(custom_oss_backupd_config: Option<&str>, verbose: bool) -> let home_dot_oss_backupd_config_path = Path::new(home_dot_oss_backupd_config); if home_dot_oss_backupd_config_path.exists() { if verbose { - print_message(MessageType::DEBUG, &format!("Read config from: {}", home_dot_oss_backupd_config)); + print_debug(&format!("Read config from: {}", home_dot_oss_backupd_config)); } return match fs::read_to_string(home_dot_oss_backupd_config_path) { Ok(o) => Some(o), Err(e) => { - print_message(MessageType::ERROR, &format!("Read config file {} error: {}", home_dot_oss_backupd_config, e)); + print_error(&format!("Read config file {} error: {}", home_dot_oss_backupd_config, e)); None }, }; @@ -298,17 +302,17 @@ fn get_config_content(custom_oss_backupd_config: Option<&str>, verbose: bool) -> let etc_oss_backupd_config_path = Path::new(ETC_OSS_BACKUPD_CONFIG); if etc_oss_backupd_config_path.exists() { if verbose { - print_message(MessageType::DEBUG, &format!("Read config from: {}", ETC_OSS_BACKUPD_CONFIG)); + print_debug(&format!("Read config from: {}", ETC_OSS_BACKUPD_CONFIG)); } return match fs::read_to_string(etc_oss_backupd_config_path) { Ok(o) => Some(o), Err(e) => { - print_message(MessageType::ERROR, &format!("Read config file {} error: {}", ETC_OSS_BACKUPD_CONFIG, e)); + print_error(&format!("Read config file {} error: {}", ETC_OSS_BACKUPD_CONFIG, e)); None }, }; } - print_message(MessageType::ERROR, "Cannot find config file"); + print_error("Cannot find config file"); None } diff --git a/src/main.rs b/src/main.rs index de0b107..64ea594 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use std::{ use rust_util::{ iff, XResult, - util_msg::*, + util_msg::{ print_ok, print_info, print_error, print_debug, }, util_time::*, }; use oss_util::*; @@ -31,12 +31,12 @@ use opt::Options; fn main() -> XResult<()> { let options = Options::new_and_parse_args()?; if options.version { - print_message(MessageType::INFO, &format!("{} v{}", config_util::NAME, config_util::VERSION)); + print_info(&format!("{} v{}", config_util::NAME, config_util::VERSION)); return Ok(()); } if options.verbose { - print_message(MessageType::DEBUG, &format!("Config is: {}", &options.config)); + print_debug(&format!("Config is: {}", &options.config)); } let config_json = match get_config_json(iff!(options.config.is_empty(), None, Some(&options.config)), options.verbose) { @@ -46,57 +46,57 @@ fn main() -> XResult<()> { let oss_backupd_config = parse_config(&config_json); if options.verbose { - print_message(MessageType::DEBUG, &format!("OSS backup config: {:?}", &oss_backupd_config)); + print_debug(&format!("OSS backup config: {:?}", &oss_backupd_config)); } for (item_index, config_item) in oss_backupd_config.items.iter().enumerate() { if let Err(e) = process_config_item(&options, &config_item, &oss_backupd_config, item_index) { - print_message(MessageType::ERROR, &format!("Config {} not found, at item index: {}", e, item_index)); + print_error(&format!("Config {} not found, at item index: {}", e, item_index)); } } - print_message(MessageType::OK, "Backup all file(s) finished!"); + print_ok("Backup all file(s) finished!"); Ok(()) } fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, oss_backupd_config :&OSSBackupdConfig, item_index: usize) -> Result<(), String> { if options.verbose { - print_message(MessageType::DEBUG, &format!("Process config item index: {}, config: {:?}", item_index, config_item)); + print_debug(&format!("Process config item index: {}, config: {:?}", item_index, config_item)); } let encrypt_pubkey_file = config_item.encrypt_pubkey_file.as_ref().ok_or_else(|| "encrypt_pubkey_file".to_owned())?; if options.verbose { - print_message(MessageType::DEBUG, &format!("Encrypt pubkey file: {}", encrypt_pubkey_file)); + print_debug(&format!("Encrypt pubkey file: {}", encrypt_pubkey_file)); } let target = config_item.target.as_ref().ok_or_else(|| "target".to_owned())?; if options.verbose { - print_message(MessageType::DEBUG, &format!("Target file: {}", iff!(target.is_empty(), "", target))); + print_debug(&format!("Target file: {}", iff!(target.is_empty(), "", target))); } let oss_config = config_item.oss_config.as_ref().ok_or_else(|| "oss_config".to_owned())?; let endpoint = oss_config.endpoint.as_ref().ok_or_else(|| "oss_config#endpoint".to_owned())?; if options.verbose { - print_message(MessageType::DEBUG, &format!("Endpoint: {}", endpoint)); + print_debug(&format!("Endpoint: {}", endpoint)); } let access_key_id = oss_config.access_key_id.as_ref().ok_or_else(|| "oss_config#access_key_id".to_owned())?; if options.verbose { - print_message(MessageType::DEBUG, &format!("Access key id: {}", access_key_id)); + print_debug(&format!("Access key id: {}", access_key_id)); } let access_key_secret = oss_config.access_key_secret.as_ref().ok_or_else(|| "oss_config#access_key_secret".to_owned())?; let bucket = &oss_config.bucket.as_ref().ok_or_else(|| "oss_config#bucket".to_owned())?; if options.verbose { - print_message(MessageType::DEBUG, &format!("Bucket: {}", bucket)); + print_debug(&format!("Bucket: {}", bucket)); } let path = &match &oss_config.path { Some(path) => path.to_owned(), None => format!("default_path_at_{}", item_index), }; if options.verbose { - print_message(MessageType::DEBUG, &format!("Path: {}", path)); + print_debug(&format!("Path: {}", path)); } let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret); @@ -105,9 +105,9 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, let new_file = format!("{}/{}", path, config_item.make_oss_key(&oss_backupd_config, "zip.gpg")); if options.verbose { - print_message(MessageType::DEBUG, &format!("Backup count: {}", backup_count)); - print_message(MessageType::DEBUG, &format!("Meta file name: {}", meta_file_name)); - print_message(MessageType::DEBUG, &format!("New backup file: {}", new_file)); + print_debug(&format!("Backup count: {}", backup_count)); + print_debug(&format!("Meta file name: {}", meta_file_name)); + print_debug(&format!("New backup file: {}", new_file)); } let secs = get_current_secs(); @@ -118,7 +118,7 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, for f in &[temp_zip_file, temp_pgp_file] { if Path::new(f).is_file() { if options.verbose { - print_message(MessageType::DEBUG, &format!("Remove file: {}", f)); + print_debug(&format!("Remove file: {}", f)); } fs::remove_file(f).ok(); } @@ -126,13 +126,13 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, }; if options.verbose { - print_message(MessageType::DEBUG, &format!("Compress file: {} -> {}", target, temp_zip_file)); + print_debug(&format!("Compress file: {} -> {}", target, temp_zip_file)); } #[cfg(not(feature = "use_zip"))] let zip_file = || { if let Err(e) = zip_util::compress_file(target, temp_zip_file) { - print_message(MessageType::ERROR, &format!("Error in zip file: {}, at item index: {}", e, item_index)); + print_error(&format!("Error in zip file: {}, at item index: {}", e, item_index)); return false; }; true @@ -175,7 +175,7 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, let mut cmd = std::process::Command::new("gpg"); cmd.args(&["-e", "-r", encrypt_pubkey_file, "-o", temp_pgp_file, temp_zip_file]); if let Err(e) = rust_util::util_cmd::run_command_and_wait(&mut cmd) { - print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); + print_error(&format!("Error in encrypt file: {}, at item index: {}", e, item_index)); return false; } true @@ -187,27 +187,27 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, let file_temp_pgp_file = match File::open(temp_pgp_file) { Ok(f) => f, Err(e) => { - print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index)); + print_error(&format!("Error in open file: {}, at item index: {}", e, item_index)); remove_temp_files(); return Ok(()); }, }; if options.verbose { - print_message(MessageType::DEBUG, &format!("Upload file: {}", temp_pgp_file)); + print_debug(&format!("Upload file: {}", temp_pgp_file)); } if let Err(e) = oss_client.put_file(bucket, &new_file, oss_util::DEFAULT_URL_VALID_IN_SECS, file_temp_pgp_file) { - print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); + print_error(&format!("Error in encrypt file: {}, at item index: {}", e, item_index)); remove_temp_files(); return Ok(()); } if options.verbose { - print_message(MessageType::DEBUG, &format!("Processing meta file: {}", meta_file_name)); + print_debug(&format!("Processing meta file: {}", meta_file_name)); } match process_oss_files(&options, &oss_client, bucket, path, meta_file_name, &new_file, backup_count) { - Err(e) => print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index)), - Ok(_) => print_message(MessageType::OK, &format!("Success, at item index: {}", item_index)), + Err(e) => print_error(&format!("Error: {}, at item index: {}", e, item_index)), + Ok(_) => print_ok(&format!("Success, at item index: {}", item_index)), }; remove_temp_files(); @@ -217,19 +217,19 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, fn process_oss_files(options: &Options, oss_client: &OSSClient, bucket_name: &str, path: &str, meta_file_name: &str, new_file: &str, limit: usize) -> XResult<()> { let meta_file_key = &format!("{}/{}", path, meta_file_name); if options.verbose { - print_message(MessageType::DEBUG, &format!("Read meta file: {}", meta_file_key)); + print_debug(&format!("Read meta file: {}", meta_file_key)); } let meta_file_content = oss_client.get_file_content(bucket_name, meta_file_key)?.unwrap_or_else(|| "[]".to_owned()); if options.verbose { - print_message(MessageType::DEBUG, &format!("Read meta file content: {}", &meta_file_content)); + print_debug(&format!("Read meta file content: {}", &meta_file_content)); } let (removed_files, new_meta_file_content) = process_new_backup_file(&meta_file_content, new_file, limit)?; if options.verbose { - print_message(MessageType::DEBUG, &format!("Processed meta file content: {}", &new_meta_file_content)); + print_debug(&format!("Processed meta file content: {}", &new_meta_file_content)); } oss_client.put_file_content(bucket_name, meta_file_key, &new_meta_file_content)?; for rm_file in removed_files { - print_message(MessageType::INFO, &format!("Remove OSS key: {}", &rm_file)); + print_info(&format!("Remove OSS key: {}", &rm_file)); oss_client.delete_file(bucket_name, &rm_file)?; } Ok(()) diff --git a/src/zip_util.rs b/src/zip_util.rs index c7d40c7..74a4934 100644 --- a/src/zip_util.rs +++ b/src/zip_util.rs @@ -16,7 +16,7 @@ use flate2:: { use rust_util::{ XResult, new_box_ioerror, - util_msg::*, + util_msg::print_warn, util_io::*, util_file::*, }; @@ -63,19 +63,19 @@ pub fn zip_file(target: &str, zip_file: &str) -> XResult<()> { } else { let mut_zip = RefCell::new(zip); walk_dir(&target_path, &|p, e| { - print_message(MessageType::WARN, &format!("Compress {} failed: {}", &p.display(), &e)); + print_warn(&format!("Compress {} failed: {}", &p.display(), &e)); }, &|f| { let options = FileOptions::default().compression_method(CompressionMethod::Stored); let mut m_zip = mut_zip.borrow_mut(); let file_name = get_file_name(&f); // TODO file name! add path match m_zip.start_file(&file_name, options) { Ok(_) => match File::open(f) { - Err(e) => print_message(MessageType::WARN, &format!("Compress {} failed: {}", &f.display(), e)), + Err(e) => print_warn(&format!("Compress {} failed: {}", &f.display(), e)), Ok(mut ff) => if let Err(e) = copy_io_with_head(&mut ff, &mut *m_zip, -1, &format!("Compressing {}", &file_name)) { - print_message(MessageType::WARN, &format!("Compress {} failed: {}", &f.display(), e)) + print_warn(&format!("Compress {} failed: {}", &f.display(), e)) } }, - Err(e) => print_message(MessageType::WARN, &format!("Compress {} failed: {}", &f.display(), e)), + Err(e) => print_warn(&format!("Compress {} failed: {}", &f.display(), e)), }; }, &|_| { true })?;