Update repo repeatedly
This commit is contained in:
parent
0a555dd9b4
commit
980e84b0f6
5 changed files with 72 additions and 24 deletions
|
|
@ -1,23 +1,45 @@
|
|||
//! Configuration from a file.
|
||||
|
||||
use std::{fs, io::ErrorKind, path::Path};
|
||||
use std::{fs, io::ErrorKind, path::Path, time::Duration};
|
||||
|
||||
use serde::Deserialize;
|
||||
use tracing::info;
|
||||
use tracing::{debug, info};
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub struct Config {}
|
||||
mod default {
|
||||
use std::time::Duration;
|
||||
|
||||
pub fn repo_update_delay() -> Duration {
|
||||
Duration::from_secs(60)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
#[serde(default = "default::repo_update_delay", with = "humantime_serde")]
|
||||
pub repo_update_delay: Duration,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
repo_update_delay: default::repo_update_delay(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load(path: &Path) -> anyhow::Result<Self> {
|
||||
info!(path = %path.display(), "Loading config");
|
||||
Ok(match fs::read_to_string(path) {
|
||||
let config = match fs::read_to_string(path) {
|
||||
Ok(str) => toml::from_str(&str)?,
|
||||
Err(e) if e.kind() == ErrorKind::NotFound => {
|
||||
info!("No config file found, using default config");
|
||||
Self::default()
|
||||
}
|
||||
Err(e) => Err(e)?,
|
||||
})
|
||||
};
|
||||
|
||||
debug!("Loaded config:\n{config:#?}");
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue