diff --git a/src/config_util.rs b/src/config_util.rs index cf20afa..c1ec7e0 100644 --- a/src/config_util.rs +++ b/src/config_util.rs @@ -1,10 +1,7 @@ use std::{ fs, path::Path, - cmp::{ - min, - max, - }, + cmp::{ min, max, }, }; use rust_util::{ iff, @@ -136,11 +133,10 @@ pub fn parse_config(config_json: &json::JsonValue) -> OSSBackupdConfig { pub fn get_config_json(custom_oss_backupd_config: Option<&str>, verbose: bool) -> Option { let config_content = get_config_content(custom_oss_backupd_config, verbose)?; match json::parse(&config_content) { - Err(e) => { + Ok(o) => Some(o), Err(e) => { print_message(MessageType::ERROR, &format!("Parse config json failed: {}", e)); None }, - Ok(o) => Some(o), } } @@ -255,11 +251,10 @@ fn get_config_content(custom_oss_backupd_config: Option<&str>, verbose: bool) -> 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) { - Err(e) => { + Ok(o) => Some(o), Err(e) => { print_message(MessageType::ERROR, &format!("Read config file {} error: {}", custom_oss_backupd_config_val, e)); None }, - Ok(o) => Some(o), }; } else { print_message(MessageType::ERROR, &format!("Custom config file not found: {}", custom_oss_backupd_config_val)); @@ -273,19 +268,17 @@ fn get_config_content(custom_oss_backupd_config: Option<&str>, verbose: bool) -> print_message(MessageType::DEBUG, &format!("Read config from: {}", OSS_BACKUPD_CONFIG)); } return match fs::read_to_string(oss_backupd_config_path) { - Err(e) => { + Ok(o) => Some(o), Err(e) => { print_message(MessageType::ERROR, &format!("Read config file {} error: {}", OSS_BACKUPD_CONFIG, e)); None }, - Ok(o) => Some(o), }; } let home_dot_oss_backupd_config = & match get_user_home_dir(DOT_OSS_BACKUPD_CONFIG) { - Err(e) => { + Ok(o) => o, Err(e) => { print_message(MessageType::WARN, &format!("Get user home error: {}", e)); String::new() }, - Ok(o) => o, }; if !home_dot_oss_backupd_config.is_empty() { let home_dot_oss_backupd_config_path = Path::new(home_dot_oss_backupd_config); @@ -294,11 +287,10 @@ fn get_config_content(custom_oss_backupd_config: Option<&str>, verbose: bool) -> print_message(MessageType::DEBUG, &format!("Read config from: {}", home_dot_oss_backupd_config)); } return match fs::read_to_string(home_dot_oss_backupd_config_path) { - Err(e) => { + Ok(o) => Some(o), Err(e) => { print_message(MessageType::ERROR, &format!("Read config file {} error: {}", home_dot_oss_backupd_config, e)); None }, - Ok(o) => Some(o), }; } } @@ -308,11 +300,10 @@ fn get_config_content(custom_oss_backupd_config: Option<&str>, verbose: bool) -> print_message(MessageType::DEBUG, &format!("Read config from: {}", ETC_OSS_BACKUPD_CONFIG)); } return match fs::read_to_string(etc_oss_backupd_config_path) { - Err(e) => { + Ok(o) => Some(o), Err(e) => { print_message(MessageType::ERROR, &format!("Read config file {} error: {}", ETC_OSS_BACKUPD_CONFIG, e)); None }, - Ok(o) => Some(o), }; } diff --git a/src/main.rs b/src/main.rs index d0a8db1..5eb1255 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,10 +10,7 @@ mod opt; use std::{ time::SystemTime, path::Path, - fs::{ - self, - File, - }, + fs::{ self, File, }, }; use rust_util::{ iff, @@ -39,8 +36,7 @@ fn main() -> XResult<()> { } let config_json = match get_config_json(iff!(options.config.is_empty(), None, Some(&options.config)), options.verbose) { - None => return Ok(()), - Some(c) => c, + Some(c) => c, None => return Ok(()), }; let oss_backupd_config = parse_config(&config_json); @@ -93,8 +89,7 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, } let path = &match &oss_config.path { - None => format!("default_path_at_{}", item_index), - Some(path) => path.to_owned(), + Some(path) => path.to_owned(), None => format!("default_path_at_{}", item_index), }; if options.verbose { print_message(MessageType::DEBUG, &format!("Path: {}", path)); @@ -112,11 +107,10 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, } let open_pgp_tool = match OpenPGPTool::from_file(encrypt_pubkey_file) { - Err(e) => { + Ok(t) => t, Err(e) => { print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index)); return Ok(()); }, - Ok(open_pgp_tool) => open_pgp_tool, }; let secs = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); @@ -152,12 +146,11 @@ fn process_config_item(options: &Options, config_item: &OSSBackupdConfigItem, }; let file_temp_pgp_file = match File::open(temp_pgp_file) { - Err(e) => { + Ok(f) => f, Err(e) => { print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index)); remove_temp_files(); return Ok(()); }, - Ok(file_temp_pgp_file) => file_temp_pgp_file, }; if options.verbose { @@ -187,8 +180,7 @@ fn process_oss_files(options: &Options, oss_client: &OSSClient, bucket_name: &st print_message(MessageType::DEBUG, &format!("Read meta file: {}", meta_file_key)); } let meta_file_content = match oss_client.get_file_content(bucket_name, meta_file_key)? { - None => "[]".to_owned(), - Some(c) => c, + Some(c) => c, None => "[]".to_owned(), }; if options.verbose { print_message(MessageType::DEBUG, &format!("Read meta file content: {}", &meta_file_content)); diff --git a/src/oss_util.rs b/src/oss_util.rs index 2622d8b..d42ea5e 100644 --- a/src/oss_util.rs +++ b/src/oss_util.rs @@ -1,15 +1,13 @@ -use std::{ - fs::File, - time::SystemTime, -}; +use std::fs::File; use sha1::Sha1; -use hmac::{ - Hmac, - Mac, -}; +use hmac::{ Hmac, Mac, }; use reqwest::Response; -use rust_util::*; - +use rust_util::{ + iff, + XResult, + new_box_ioerror, + util_time::get_current_secs, +}; pub const DEFAULT_URL_VALID_IN_SECS: u64 = 1000; @@ -136,8 +134,3 @@ fn calc_hmac_sha1(key: &[u8], message: &[u8]) -> String { Err(e) => format!("[ERROR]Hmac error: {}", e), } } - -fn get_current_secs() -> u64 { - SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() -} - diff --git a/src/pgp_util.rs b/src/pgp_util.rs index 68f236f..33fc3f7 100644 --- a/src/pgp_util.rs +++ b/src/pgp_util.rs @@ -10,10 +10,7 @@ use std::{ BufWriter, }, }; -use rust_util::{ - XResult, - new_box_error, -}; +use rust_util::{ XResult, new_box_error, }; use openpgp::{ types::KeyFlags, TPK, @@ -25,10 +22,7 @@ use openpgp::{ LiteralWriter, }, }; -use indicatif::{ - ProgressBar, - ProgressStyle -}; +use indicatif::{ ProgressBar, ProgressStyle, }; const BUFF_SIZE: usize = 512 * 1024; const PB_PROGRESS: &str = "#-"; diff --git a/src/zip_util.rs b/src/zip_util.rs index 2184693..df702b7 100644 --- a/src/zip_util.rs +++ b/src/zip_util.rs @@ -7,10 +7,7 @@ use std::{ }; use zip::{ CompressionMethod, - write::{ - ZipWriter, - FileOptions, - }, + write::{ ZipWriter, FileOptions, }, }; use rust_util::{ XResult,