mirror of
https://codeberg.org/demostf/cleanup.git
synced 2026-06-04 02:24:10 +02:00
proper config file
This commit is contained in:
parent
6b65ee144c
commit
6cf28c4504
4 changed files with 1211 additions and 470 deletions
59
src/config.rs
Normal file
59
src/config.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use demostf_client::ApiClient;
|
||||
use serde::Deserialize;
|
||||
use std::fs::read_to_string;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigError {
|
||||
#[error("Failed to read config from {path}: {error}")]
|
||||
Read { error: std::io::Error, path: String },
|
||||
#[error("Failed to parse config from {path}: {error}")]
|
||||
Parse {
|
||||
error: toml::de::Error,
|
||||
path: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
pub api: ApiConfig,
|
||||
pub storage: StorageConfig,
|
||||
pub cleanup: CleanupConfig,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load(path: String) -> Result<Self, ConfigError> {
|
||||
let content = read_to_string(&path).map_err(|error| ConfigError::Read {
|
||||
error,
|
||||
path: path.clone(),
|
||||
})?;
|
||||
toml::from_str(&content).map_err(|error| ConfigError::Parse { error, path })
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ApiConfig {
|
||||
#[serde(default = "default_api_base")]
|
||||
pub url: String,
|
||||
pub key_file: String,
|
||||
}
|
||||
|
||||
fn default_api_base() -> String {
|
||||
ApiClient::DEMOS_TF_BASE_URL.into()
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct CleanupConfig {
|
||||
#[serde(default = "default_from_backend")]
|
||||
pub from_backend: String,
|
||||
pub age: u64,
|
||||
}
|
||||
|
||||
fn default_from_backend() -> String {
|
||||
"freezer".into()
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct StorageConfig {
|
||||
pub root: String,
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue