Add methods to DtpmConfig for loading configuration from a path and encoding file contents in base64
This commit is contained in:
parent
e9e4f1414a
commit
21d4747821
@ -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<RestartPolicy> for pb_dtpm::RestartPolicy {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DtpmConfig {
|
||||
pub fn from_path(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let config_str = std::fs::read_to_string(path)?;
|
||||
Ok(serde_yml::from_str(&config_str)?)
|
||||
}
|
||||
|
||||
pub fn load_data(mut self) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user