From 21d47478217d60f685607bf14597fd189d6bce7f Mon Sep 17 00:00:00 2001 From: Noor Date: Wed, 5 Feb 2025 17:34:56 +0530 Subject: [PATCH] Add methods to DtpmConfig for loading configuration from a path and encoding file contents in base64 --- src/types/dtpm.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/types/dtpm.rs b/src/types/dtpm.rs index cac3387..5442de6 100644 --- a/src/types/dtpm.rs +++ b/src/types/dtpm.rs @@ -1,4 +1,5 @@ use crate::pb::dtpm as pb_dtpm; +use base64::{engine::general_purpose::STANDARD as BASE64, Engine}; use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize, Default)] @@ -178,3 +179,23 @@ impl From for pb_dtpm::RestartPolicy { } } } + +impl DtpmConfig { + pub fn from_path(path: &str) -> Result> { + let config_str = std::fs::read_to_string(path)?; + Ok(serde_yml::from_str(&config_str)?) + } + + pub fn load_data(mut self) -> Result> { + self.filesystems.iter_mut().for_each(|x| { + if let FileContent::Path(path) = &x.content { + let content = + std::fs::read(path).unwrap_or_else(|_| panic!("Unable to read file {path}")); + let encoded = BASE64.encode(content); + x.content = FileContent::Data(encoded); + } + }); + + Ok(self) + } +}