From 42c95fbb1803114f62b5ad015edb1c61e0555b3a Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 30 Nov 2019 10:28:12 +0800 Subject: [PATCH] update config_util.rs --- oss-backupd-config.json | 15 +++++++------- src/config_util.rs | 45 ++++++++++++++++++++++++----------------- src/main.rs | 23 ++++++++++++++++++--- 3 files changed, 53 insertions(+), 30 deletions(-) diff --git a/oss-backupd-config.json b/oss-backupd-config.json index 37ca66d..1691232 100644 --- a/oss-backupd-config.json +++ b/oss-backupd-config.json @@ -1,18 +1,17 @@ { "oss_config": { - "endpoint": "", - "access_key_id": "", - "access_key_secret": "", - "bucket": "", - "path": "" + "endpoint": "_endpoint", + "access_key_id": "_keyid", + "access_key_secret": "_keysecret", + "bucket": "_bucket", + "path": "_path" }, "host": "sample_host", - "encrypt_pubkey_file": "", + "encrypt_pubkey_file": "_enc", "items": [ { - "oss_config": null, "target": "", - "file_name": "sample_file", + "file_name": "sample_file" } ] } \ No newline at end of file diff --git a/src/config_util.rs b/src/config_util.rs index 7fd88d3..407799b 100644 --- a/src/config_util.rs +++ b/src/config_util.rs @@ -8,7 +8,6 @@ use rust_util::{ util_msg::*, }; use chrono::{ - prelude::*, Utc, }; @@ -38,6 +37,7 @@ pub const DOT_OSS_BACKUPD_CONFIG: &str = ".oss-backupd-config.json"; } */ +#[derive(Debug, Clone)] pub struct OSSConfig { pub endpoint: Option, pub access_key_id: Option, @@ -46,6 +46,7 @@ pub struct OSSConfig { pub path: Option, } +#[derive(Debug)] pub struct OSSBackupdConfigItem { pub target: Option, pub file_name: Option, @@ -53,6 +54,7 @@ pub struct OSSBackupdConfigItem { pub encrypt_pubkey_file: Option, } +#[derive(Debug)] pub struct OSSBackupdConfig { pub oss_config: Option, pub prefix: Option, @@ -127,28 +129,33 @@ pub fn parse_oss_backupd_config_item(item: &json::JsonValue, root_oss_config_obj let file_name = get_string_value(item, "file_name"); let mut encrypt_pubkey_file = get_string_value(item, "encrypt_pubkey_file"); let mut oss_config = parse_sub_oss_config(item); - if oss_config.is_some() && root_oss_config_object.is_some() { - let mut oc = oss_config.unwrap(); - let root_oc = root_oss_config_object.as_ref().unwrap(); + if root_oss_config_object.is_some() { + if oss_config.is_some() { + let mut oc = oss_config.unwrap(); + let root_oc = root_oss_config_object.as_ref().unwrap(); - if oc.endpoint.is_none() && root_oc.endpoint.is_some() { - oc.endpoint = root_oc.endpoint.clone() + if oc.endpoint.is_none() && root_oc.endpoint.is_some() { + oc.endpoint = root_oc.endpoint.clone() + } + if oc.access_key_id.is_none() && root_oc.access_key_id.is_some() { + oc.access_key_id = root_oc.access_key_id.clone() + } + if oc.access_key_secret.is_none() && root_oc.access_key_secret.is_some() { + oc.access_key_secret = root_oc.access_key_secret.clone(); + } + if oc.bucket.is_none() && root_oc.bucket.is_some() { + oc.bucket = root_oc.bucket.clone(); + } + if oc.path.is_none() && root_oc.path.is_some() { + oc.path = root_oc.path.clone(); + } + oss_config = Some(oc); + } else { + oss_config = root_oss_config_object.clone(); } - if oc.access_key_id.is_none() && root_oc.access_key_id.is_some() { - oc.access_key_id = root_oc.access_key_id.clone() - } - if oc.access_key_secret.is_none() && root_oc.access_key_secret.is_some() { - oc.access_key_secret = root_oc.access_key_secret.clone(); - } - if oc.bucket.is_none() && root_oc.bucket.is_some() { - oc.bucket = root_oc.bucket.clone(); - } - if oc.path.is_none() && root_oc.path.is_some() { - oc.path = root_oc.path.clone(); - } - oss_config = Some(oc); } + if encrypt_pubkey_file.is_none() && root_encrypt_pubkey_file.is_some() { encrypt_pubkey_file = root_encrypt_pubkey_file.clone(); } diff --git a/src/main.rs b/src/main.rs index 8bf1232..a28ec6b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,8 @@ use std::{ use rust_util::{ XResult, }; -use pgp_util::OpenPGPTool; +use config_util::*; +// use pgp_util::OpenPGPTool; // https://docs.sequoia-pgp.org/sequoia_openpgp/serialize/stream/struct.Encryptor.html // https://gitlab.com/sequoia-pgp/sequoia/blob/master/openpgp/examples/generate-encrypt-decrypt.rs @@ -18,9 +19,25 @@ fn main() -> XResult<()> { println!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()); - let openpgp_client = OpenPGPTool::from_file("sample.gpg")?; + // let openpgp_client = OpenPGPTool::from_file("sample.gpg")?; - openpgp_client.encrypt_file("a", "b.gpg", false)?; + // openpgp_client.encrypt_file("a", "b.gpg", false)?; + + let config_json = get_config_json(); + + let j = config_json.unwrap(); + + println!("{}", j); + + let c = parse_config(&j); + + println!("{:?}", c); + + println!(""); + + for i in c.items { + println!("{:?}", i); + } Ok(()) }