From a81af92f9d43efc33b5bbf7319a54f5b7372a571 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 9 Jan 2020 17:35:44 +0100 Subject: [PATCH] src/section_config.rs: implement generic lookup --- src/bin/proxmox-backup-manager.rs | 4 +++- src/config/remotes.rs | 21 --------------------- src/section_config.rs | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/bin/proxmox-backup-manager.rs b/src/bin/proxmox-backup-manager.rs index 5218674d..5afbbf80 100644 --- a/src/bin/proxmox-backup-manager.rs +++ b/src/bin/proxmox-backup-manager.rs @@ -385,7 +385,9 @@ async fn start_datastore_sync( let mut client = connect()?; - let remote = proxmox_backup::config::remotes::lookup(&remote)?; + use proxmox_backup::config::remotes::{self, Remote}; + let remote_config = remotes::config()?; + let remote: Remote = remote_config.lookup("remote", &remote)?; let args = json!({ "store": store, diff --git a/src/config/remotes.rs b/src/config/remotes.rs index 83bf9b67..d203dbf9 100644 --- a/src/config/remotes.rs +++ b/src/config/remotes.rs @@ -80,27 +80,6 @@ pub fn config() -> Result { CONFIG.parse(REMOTES_CFG_FILENAME, &content) } -pub fn lookup(remote: &str) -> Result { - - let remotes = config()?; - - let config = match remotes.sections.get(remote) { - Some((type_name, config)) => { - if type_name != "remote" { - bail!("got unexpected type '{}' for remote '{}'", type_name, remote); - } - config - } - None => { - bail!("no such remote '{}'", remote); - } - }; - - let remote: Remote = serde_json::from_value(config.clone())?; - - Ok(remote) -} - pub fn save_config(config: &SectionConfigData) -> Result<(), Error> { let raw = CONFIG.write(REMOTES_CFG_FILENAME, &config)?; diff --git a/src/section_config.rs b/src/section_config.rs index dd0ae83d..39209697 100644 --- a/src/section_config.rs +++ b/src/section_config.rs @@ -5,6 +5,7 @@ use std::collections::HashSet; use std::collections::VecDeque; use serde_json::{json, Value}; +use serde::de::DeserializeOwned; use proxmox::api::schema::*; use proxmox::tools::try_block; @@ -53,6 +54,25 @@ impl SectionConfigData { self.sections.insert(section_id.to_string(), (type_name.to_string(), config)); } + pub fn lookup(&self, type_name: &str, remote: &str) -> Result { + + let config = match self.sections.get(remote) { + Some((section_type_name, config)) => { + if type_name != section_type_name { + bail!("got unexpected type '{}' for {} '{}'", section_type_name, type_name, remote); + } + config + } + None => { + bail!("no such {} '{}'", type_name, remote); + } + }; + + let data = T::deserialize(config.clone())?; + + Ok(data) + } + fn record_order(&mut self, section_id: &str) { self.order.push_back(section_id.to_string()); }