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) + } +}