mirror of
https://gitlab.steamos.cloud/holo/steamos-manager.git
synced 2025-07-13 09:52:00 -04:00
config: Allow config fragment loading code to be used generically
This commit is contained in:
parent
2d4647a918
commit
6c485684b8
2 changed files with 98 additions and 81 deletions
82
src/lib.rs
82
src/lib.rs
|
@ -6,12 +6,17 @@
|
|||
*/
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use async_trait::async_trait;
|
||||
use config::builder::AsyncState;
|
||||
use config::{AsyncSource, ConfigBuilder, ConfigError, FileFormat, Format, Map, Value};
|
||||
use std::fmt::Debug;
|
||||
use std::future::Future;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tokio::fs::File;
|
||||
use tokio::fs::{read_dir, read_to_string, File};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::{info, warn};
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
mod ds_inhibit;
|
||||
mod error;
|
||||
|
@ -69,6 +74,41 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct AsyncFileSource<F: Format, P: AsRef<Path> + Sized + Send + Sync> {
|
||||
path: P,
|
||||
format: F,
|
||||
}
|
||||
|
||||
impl<F: Format, P: AsRef<Path> + Sized + Send + Sync + Debug> AsyncFileSource<F, P> {
|
||||
fn from(path: P, format: F) -> AsyncFileSource<F, P> {
|
||||
AsyncFileSource { path, format }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<F: Format + Send + Sync + Debug, P: AsRef<Path> + Sized + Send + Sync + Debug> AsyncSource
|
||||
for AsyncFileSource<F, P>
|
||||
{
|
||||
async fn collect(&self) -> Result<Map<String, Value>, ConfigError> {
|
||||
let path = self.path.as_ref();
|
||||
let text = match read_to_string(&path).await {
|
||||
Ok(text) => text,
|
||||
Err(e) => {
|
||||
if e.kind() == ErrorKind::NotFound {
|
||||
info!("No config file {} found", path.to_string_lossy());
|
||||
return Ok(Map::new());
|
||||
}
|
||||
return Err(ConfigError::Foreign(Box::new(e)));
|
||||
}
|
||||
};
|
||||
let path = path.to_string_lossy().to_string();
|
||||
self.format
|
||||
.parse(Some(&path), &text)
|
||||
.map_err(ConfigError::Foreign)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
pub fn path<S: AsRef<str>>(path: S) -> PathBuf {
|
||||
PathBuf::from(path.as_ref())
|
||||
|
@ -126,6 +166,44 @@ pub(crate) fn get_appid(pid: u32) -> Result<Option<u64>> {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn read_config_directory<P: AsRef<Path> + Sync + Send>(
|
||||
builder: ConfigBuilder<AsyncState>,
|
||||
path: P,
|
||||
extensions: &[&str],
|
||||
format: FileFormat,
|
||||
) -> Result<ConfigBuilder<AsyncState>> {
|
||||
let mut dir = match read_dir(&path).await {
|
||||
Ok(dir) => dir,
|
||||
Err(e) => {
|
||||
if e.kind() == ErrorKind::NotFound {
|
||||
debug!(
|
||||
"No config fragment directory {} found",
|
||||
path.as_ref().to_string_lossy()
|
||||
);
|
||||
return Ok(builder);
|
||||
}
|
||||
error!(
|
||||
"Error reading config fragment directory {}: {e}",
|
||||
path.as_ref().to_string_lossy()
|
||||
);
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
let mut entries = Vec::new();
|
||||
while let Some(entry) = dir.next_entry().await? {
|
||||
let path = entry.path();
|
||||
if let Some(ext) = path.extension().and_then(|ext| ext.to_str()) {
|
||||
if extensions.contains(&ext) {
|
||||
entries.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
entries.sort();
|
||||
Ok(entries.into_iter().fold(builder, |builder, path| {
|
||||
builder.add_async_source(AsyncFileSource::from(path, format))
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::testing;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue