update use

This commit is contained in:
2020-04-12 01:19:44 +08:00
parent f1ede0bca7
commit b7c747c565
5 changed files with 24 additions and 57 deletions

View File

@@ -1,10 +1,7 @@
use std::{ use std::{
fs, fs,
path::Path, path::Path,
cmp::{ cmp::{ min, max, },
min,
max,
},
}; };
use rust_util::{ use rust_util::{
iff, 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<json::JsonValue> { pub fn get_config_json(custom_oss_backupd_config: Option<&str>, verbose: bool) -> Option<json::JsonValue> {
let config_content = get_config_content(custom_oss_backupd_config, verbose)?; let config_content = get_config_content(custom_oss_backupd_config, verbose)?;
match json::parse(&config_content) { match json::parse(&config_content) {
Err(e) => { Ok(o) => Some(o), Err(e) => {
print_message(MessageType::ERROR, &format!("Parse config json failed: {}", e)); print_message(MessageType::ERROR, &format!("Parse config json failed: {}", e));
None 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); let custom_oss_backupd_config_path = Path::new(custom_oss_backupd_config_val);
if custom_oss_backupd_config_path.exists() { if custom_oss_backupd_config_path.exists() {
return match fs::read_to_string(custom_oss_backupd_config_path) { 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)); print_message(MessageType::ERROR, &format!("Read config file {} error: {}", custom_oss_backupd_config_val, e));
None None
}, },
Ok(o) => Some(o),
}; };
} else { } else {
print_message(MessageType::ERROR, &format!("Custom config file not found: {}", custom_oss_backupd_config_val)); 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)); print_message(MessageType::DEBUG, &format!("Read config from: {}", OSS_BACKUPD_CONFIG));
} }
return match fs::read_to_string(oss_backupd_config_path) { 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)); print_message(MessageType::ERROR, &format!("Read config file {} error: {}", OSS_BACKUPD_CONFIG, e));
None None
}, },
Ok(o) => Some(o),
}; };
} }
let home_dot_oss_backupd_config = & match get_user_home_dir(DOT_OSS_BACKUPD_CONFIG) { 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)); print_message(MessageType::WARN, &format!("Get user home error: {}", e));
String::new() String::new()
}, },
Ok(o) => o,
}; };
if !home_dot_oss_backupd_config.is_empty() { if !home_dot_oss_backupd_config.is_empty() {
let home_dot_oss_backupd_config_path = Path::new(home_dot_oss_backupd_config); 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)); 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) { 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)); print_message(MessageType::ERROR, &format!("Read config file {} error: {}", home_dot_oss_backupd_config, e));
None 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)); print_message(MessageType::DEBUG, &format!("Read config from: {}", ETC_OSS_BACKUPD_CONFIG));
} }
return match fs::read_to_string(etc_oss_backupd_config_path) { 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)); print_message(MessageType::ERROR, &format!("Read config file {} error: {}", ETC_OSS_BACKUPD_CONFIG, e));
None None
}, },
Ok(o) => Some(o),
}; };
} }

View File

@@ -10,10 +10,7 @@ mod opt;
use std::{ use std::{
time::SystemTime, time::SystemTime,
path::Path, path::Path,
fs::{ fs::{ self, File, },
self,
File,
},
}; };
use rust_util::{ use rust_util::{
iff, 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) { let config_json = match get_config_json(iff!(options.config.is_empty(), None, Some(&options.config)), options.verbose) {
None => return Ok(()), Some(c) => c, None => return Ok(()),
Some(c) => c,
}; };
let oss_backupd_config = parse_config(&config_json); 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 { let path = &match &oss_config.path {
None => format!("default_path_at_{}", item_index), Some(path) => path.to_owned(), None => format!("default_path_at_{}", item_index),
Some(path) => path.to_owned(),
}; };
if options.verbose { if options.verbose {
print_message(MessageType::DEBUG, &format!("Path: {}", path)); 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) { 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)); print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index));
return Ok(()); return Ok(());
}, },
Ok(open_pgp_tool) => open_pgp_tool,
}; };
let secs = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs(); 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) { 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)); print_message(MessageType::ERROR, &format!("Error in open file: {}, at item index: {}", e, item_index));
remove_temp_files(); remove_temp_files();
return Ok(()); return Ok(());
}, },
Ok(file_temp_pgp_file) => file_temp_pgp_file,
}; };
if options.verbose { 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)); 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)? { let meta_file_content = match oss_client.get_file_content(bucket_name, meta_file_key)? {
None => "[]".to_owned(), Some(c) => c, None => "[]".to_owned(),
Some(c) => c,
}; };
if options.verbose { if options.verbose {
print_message(MessageType::DEBUG, &format!("Read meta file content: {}", &meta_file_content)); print_message(MessageType::DEBUG, &format!("Read meta file content: {}", &meta_file_content));

View File

@@ -1,15 +1,13 @@
use std::{ use std::fs::File;
fs::File,
time::SystemTime,
};
use sha1::Sha1; use sha1::Sha1;
use hmac::{ use hmac::{ Hmac, Mac, };
Hmac,
Mac,
};
use reqwest::Response; 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; 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), Err(e) => format!("[ERROR]Hmac error: {}", e),
} }
} }
fn get_current_secs() -> u64 {
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()
}

View File

@@ -10,10 +10,7 @@ use std::{
BufWriter, BufWriter,
}, },
}; };
use rust_util::{ use rust_util::{ XResult, new_box_error, };
XResult,
new_box_error,
};
use openpgp::{ use openpgp::{
types::KeyFlags, types::KeyFlags,
TPK, TPK,
@@ -25,10 +22,7 @@ use openpgp::{
LiteralWriter, LiteralWriter,
}, },
}; };
use indicatif::{ use indicatif::{ ProgressBar, ProgressStyle, };
ProgressBar,
ProgressStyle
};
const BUFF_SIZE: usize = 512 * 1024; const BUFF_SIZE: usize = 512 * 1024;
const PB_PROGRESS: &str = "#-"; const PB_PROGRESS: &str = "#-";

View File

@@ -7,10 +7,7 @@ use std::{
}; };
use zip::{ use zip::{
CompressionMethod, CompressionMethod,
write::{ write::{ ZipWriter, FileOptions, },
ZipWriter,
FileOptions,
},
}; };
use rust_util::{ use rust_util::{
XResult, XResult,