From a6116498345df2434d53ed814a12a7551870880f Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 2 Aug 2020 23:01:53 +0800 Subject: [PATCH] style: code style --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- src/main.rs | 67 ++++++++++++++++++++++++++--------------------------- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e260e3c..01f3867 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1597,9 +1597,9 @@ dependencies = [ [[package]] name = "rust_util" -version = "0.2.6" +version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "316c77fb3848231e129f0503aac127ac5ee4f5c433f033d1c11914759f6d31e1" +checksum = "9e9cf201657d8553fd7eddf4c20e00b1bdebca40e9fa2ede5c87f6874d02750f" dependencies = [ "lazy_static", "libc", diff --git a/Cargo.toml b/Cargo.toml index 14e31cd..ea53074 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.6" +rust_util = "0.6.3" diff --git a/src/main.rs b/src/main.rs index 3d5b1b3..17516e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ -// #[macro_use] -// extern crate lazy_static; +#[macro_use] +extern crate rust_util; #[cfg(feature = "use_sequoia_openpgp")] extern crate sequoia_openpgp as openpgp; + mod oss_util; #[cfg(feature = "use_sequoia_openpgp")] mod pgp_util; @@ -12,12 +13,10 @@ mod opt; use std::{ path::Path, - fs::{ self, File, }, + fs::{ self, File }, }; use rust_util::{ - iff, XResult, - util_msg::*, util_time::*, }; use oss_util::*; @@ -31,12 +30,12 @@ use opt::Options; fn main() -> XResult<()> { let options = Options::new_and_parse_args()?; if options.version { - print_info(&format!("{} v{}", config_util::NAME, config_util::VERSION)); + information!("{} v{}", config_util::NAME, config_util::VERSION); return Ok(()); } if options.verbose { - print_debug(&format!("Config is: {}", &options.config)); + debugging!("Config is: {}", &options.config); } let config_json = match get_config_json(iff!(options.config.is_empty(), None, Some(&options.config)), options.verbose) { @@ -46,57 +45,57 @@ fn main() -> XResult<()> { let oss_backupd_config = parse_config(&config_json); if options.verbose { - print_debug(&format!("OSS backup config: {:?}", &oss_backupd_config)); + debugging!("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_error(&format!("Config {} not found, at item index: {}", e, item_index)); + failure!("Config {} not found, at item index: {}", e, item_index); } } - print_ok("Backup all file(s) finished!"); + success!("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_debug(&format!("Process config item index: {}, config: {:?}", item_index, config_item)); + debugging!("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_debug(&format!("Encrypt pubkey file: {}", encrypt_pubkey_file)); + debugging!("Encrypt pubkey file: {}", encrypt_pubkey_file); } let target = config_item.target.as_ref().ok_or_else(|| "target".to_owned())?; if options.verbose { - print_debug(&format!("Target file: {}", iff!(target.is_empty(), "", target))); + debugging!("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_debug(&format!("Endpoint: {}", endpoint)); + debugging!("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_debug(&format!("Access key id: {}", access_key_id)); + debugging!("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_debug(&format!("Bucket: {}", bucket)); + debugging!("Bucket: {}", bucket); } let path = &match &oss_config.path { Some(path) => path.to_owned(), None => format!("default_path_at_{}", item_index), }; if options.verbose { - print_debug(&format!("Path: {}", path)); + debugging!("Path: {}", path); } let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret); @@ -105,9 +104,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_debug(&format!("Backup count: {}", backup_count)); - print_debug(&format!("Meta file name: {}", meta_file_name)); - print_debug(&format!("New backup file: {}", new_file)); + debugging!("Backup count: {}", backup_count); + debugging!("Meta file name: {}", meta_file_name); + debugging!("New backup file: {}", new_file); } let secs = get_current_secs(); @@ -118,7 +117,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_debug(&format!("Remove file: {}", f)); + debugging!("Remove file: {}", f); } fs::remove_file(f).ok(); } @@ -126,13 +125,13 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, }; if options.verbose { - print_debug(&format!("Compress file: {} -> {}", target, temp_zip_file)); + debugging!("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_error(&format!("Error in zip file: {}, at item index: {}", e, item_index)); + failure!("Error in zip file: {}, at item index: {}", e, item_index); false } else { true @@ -178,7 +177,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_error(&format!("Error in encrypt file: {}, at item index: {}", e, item_index)); + failure!("Error in encrypt file: {}, at item index: {}", e, item_index); false } else { true @@ -191,27 +190,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_error(&format!("Error in open file: {}, at item index: {}", e, item_index)); + failure!("Error in open file: {}, at item index: {}", e, item_index); remove_temp_files(); return Ok(()); }, }; if options.verbose { - print_debug(&format!("Upload file: {}", temp_pgp_file)); + debugging!("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_error(&format!("Error in encrypt file: {}, at item index: {}", e, item_index)); + failure!("Error in encrypt file: {}, at item index: {}", e, item_index); remove_temp_files(); return Ok(()); } if options.verbose { - print_debug(&format!("Processing meta file: {}", meta_file_name)); + debugging!("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_error(&format!("Error: {}, at item index: {}", e, item_index)), - Ok(_) => print_ok(&format!("Success, at item index: {}", item_index)), + Err(e) => failure!("Error: {}, at item index: {}", e, item_index), + Ok(_) => success!("Success, at item index: {}", item_index), }; remove_temp_files(); @@ -221,19 +220,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_debug(&format!("Read meta file: {}", meta_file_key)); + debugging!("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_debug(&format!("Read meta file content: {}", &meta_file_content)); + debugging!("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_debug(&format!("Processed meta file content: {}", &new_meta_file_content)); + debugging!("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_info(&format!("Remove OSS key: {}", &rm_file)); + information!("Remove OSS key: {}", &rm_file); oss_client.delete_file(bucket_name, &rm_file)?; } Ok(())