diff --git a/src/main.rs b/src/main.rs index c71e7c5..674ad1c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,19 +36,6 @@ fn main() -> XResult<()> { print_message(MessageType::DEBUG, &format!("Config is: {}", &options.config)); } - println!("Hello, world!"); - println!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); - - // let oss_client = OSSClient::new("oss-cn-shanghai.aliyuncs.com", "", ""); - // let c = oss_client.get_file_content("hatterbucket", "Check.java")?; - // println!("XXXXX: {:?}", c); - // println!("XXXXX: {}", ); - - // zip_util::zip_file("hello.txt", "aa.zip")?; - - // let openpgp_client = OpenPGPTool::from_file("sample.gpg")?; - // openpgp_client.encrypt_file("a", "b.asc", true)?; - let config_json = match get_config_json(iff!(options.config.is_empty(), None, Some(&options.config)), options.verbose) { None => return Ok(()), Some(c) => c, @@ -56,16 +43,17 @@ fn main() -> XResult<()> { let oss_backupd_config = parse_config(&config_json); - println!("{:?}", &oss_backupd_config); + if options.verbose { + print_message(MessageType::DEBUG, &format!("OSS backup config: {:?}", &oss_backupd_config)); + } - // TODO ... - - println!("{:?}", oss_backupd_config); - let mut item_index = 0; + let mut item_index = -1; for config_item in &oss_backupd_config.items { - println!(">>>>>> index: {}, {:?}", item_index, config_item); - println!("______ {}", config_item.make_oss_key(&oss_backupd_config, "gpg")); - // println!("{}", config_item.make_oss_key(&oss_backupd_config, "asc")); + item_index += 1; + + if options.verbose { + print_message(MessageType::DEBUG, &format!("Process config item index: {}, config: {:?}", item_index, config_item)); + } let encrypt_pubkey_file = match &config_item.encrypt_pubkey_file { None => { @@ -74,6 +62,10 @@ fn main() -> XResult<()> { }, Some(encrypt_pubkey_file) => encrypt_pubkey_file, }; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Encrypt pubkey file: {}", encrypt_pubkey_file)); + } + let target = match &config_item.target { None => { print_message(MessageType::ERROR, &format!("Config target not found, at item index: {}", item_index)); @@ -81,6 +73,9 @@ fn main() -> XResult<()> { }, Some(target) => target, }; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Target file: {}", iff!(target.is_empty(), "", target))); + } let oss_config = match &config_item.oss_config { None => { @@ -96,6 +91,10 @@ fn main() -> XResult<()> { } Some(endpoint) => endpoint, }; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Endpoint: {}", endpoint)); + } + let access_key_id = match &oss_config.access_key_id { None => { print_message(MessageType::ERROR, &format!("Config oss_config#access_key_id not found, at item index: {}", item_index)); @@ -103,6 +102,10 @@ fn main() -> XResult<()> { }, Some(access_key_id) => access_key_id, }; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Access key id: {}", access_key_id)); + } + let access_key_secret = match &oss_config.access_key_secret { None => { print_message(MessageType::ERROR, &format!("Config oss_config#access_key_secret not found, at item index: {}", item_index)); @@ -117,15 +120,29 @@ fn main() -> XResult<()> { }, Some(bucket) => bucket, }; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Bucket: {}", bucket)); + } + let path = &match &oss_config.path { None => format!("default_path_at_{}", item_index), Some(path) => path.to_owned(), }; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Path: {}", path)); + } + let oss_client = OSSClient::new(endpoint, access_key_id, access_key_secret); - let default_limit = 10_usize; // TODO ... + let default_limit = 10_usize; let meta_file_name = &format!("ossbackupd_meta_{}.json", path); let new_file = config_item.make_oss_key(&oss_backupd_config, "gpg"); + if options.verbose { + print_message(MessageType::DEBUG, &format!("Default limit: {}", default_limit)); + print_message(MessageType::DEBUG, &format!("Meta file name: {}", meta_file_name)); + print_message(MessageType::DEBUG, &format!("New backup file: {}", new_file)); + } + let open_pgp_tool = match OpenPGPTool::from_file(encrypt_pubkey_file) { Err(e) => { print_message(MessageType::ERROR, &format!("Error in load pgp file: {}, at item index: {}", e, item_index)); @@ -136,12 +153,17 @@ fn main() -> XResult<()> { let temp_zip_file = &format!("temp_file_{}.zip", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); let temp_pgp_file = &format!("temp_file_{}.gpg", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); - print_message(MessageType::INFO, &format!("Compress file {} -> {}", target, temp_zip_file)); + if options.verbose { + print_message(MessageType::DEBUG, &format!("Compress file: {} -> {}", target, temp_zip_file)); + } if let Err(e) = zip_util::zip_file(target, temp_zip_file) { print_message(MessageType::ERROR, &format!("Error in zip file: {}, at item index: {}", e, item_index)); continue; }; + if options.verbose { + print_message(MessageType::DEBUG, &format!("Encrypt file: {} -> {}", temp_zip_file, temp_pgp_file)); + } if let Err(e) = open_pgp_tool.encrypt_file(temp_zip_file, temp_pgp_file, false) { print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); fs::remove_file(temp_zip_file).ok(); @@ -156,6 +178,10 @@ fn main() -> XResult<()> { continue;}, Ok(file_temp_pgp_file) => file_temp_pgp_file, }; + + if options.verbose { + print_message(MessageType::DEBUG, &format!("Upload file: {}", temp_pgp_file)); + } if let Err(e) = oss_client.put_file(bucket, &new_file, 1000, file_temp_pgp_file) { print_message(MessageType::ERROR, &format!("Error in encrypt file: {}, at item index: {}", e, item_index)); fs::remove_file(temp_zip_file).ok(); @@ -163,7 +189,9 @@ fn main() -> XResult<()> { continue; } - // TODO ... + if options.verbose { + print_message(MessageType::DEBUG, &format!("Processing meta file: {}", meta_file_name)); + } match process_oss_files(&oss_client, bucket, path, meta_file_name, &new_file, default_limit) { Err(e) => { print_message(MessageType::ERROR, &format!("Error: {}, at item index: {}", e, item_index)); @@ -174,14 +202,14 @@ fn main() -> XResult<()> { }; fs::remove_file(temp_zip_file).ok(); fs::remove_file(temp_pgp_file).ok(); - - item_index += 1; } + if options.verbose { + print_message(MessageType::DEBUG, "Backup all file(s) finished!"); + } Ok(()) } -// TODO call it! pub fn process_oss_files(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 *opt::IS_DEBUG { @@ -222,6 +250,7 @@ pub fn process_new_backup_file(backup_content_json: &str, new_item: &str, limit: Ok((removed_vec, stringifyed_json.to_string())) } +// stringify JSON array pub fn stringity_json_array(vec: &Vec) -> XResult { let mut json_arr = json::JsonValue::new_array(); for v in vec { @@ -230,6 +259,7 @@ pub fn stringity_json_array(vec: &Vec) -> XResult { Ok(json::stringify_pretty(json_arr, 4)) } +// parse JSON array pub fn parse_json_array(arr: &str) -> XResult> { let mut vec: Vec = vec![]; if arr != "" {