Add support for a Raker config

rei/minimum
Olivier 'reivilibre' 2022-03-14 23:53:55 +00:00
parent 1f681164c9
commit 2763ff2481
6 changed files with 87 additions and 3 deletions

8
.gitignore vendored
View File

@ -1,4 +1,6 @@
.idea
data/cf_ips.txt
data
/.idea
/data/cf_ips.txt
/data
/qp_raker.toml
.*.swp

10
Cargo.lock generated
View File

@ -2200,6 +2200,7 @@ dependencies = [
"serde_bare",
"sitemap",
"tokio",
"toml",
]
[[package]]
@ -2889,6 +2890,15 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]]
name = "tower-service"
version = "0.3.1"

View File

@ -22,6 +22,8 @@ html5ever = "0.25.1"
serde = { version = "1.0.136", features = ["derive"] }
serde_bare = "0.5.0"
toml = "0.5.8"
### Dates
chrono = "0.4.19"

View File

@ -0,0 +1,31 @@
use clap::Parser;
use env_logger::Env;
use anyhow::Context;
use std::path::PathBuf;
use quickpeep_raker::config;
/// Seeds a raker's queue with URLs
#[derive(Clone, Debug, Parser)]
pub struct Opts {
#[clap(long = "config")]
config: Option<PathBuf>,
}
#[tokio::main]
pub async fn main() -> anyhow::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("info,quickpeep=debug")).init();
let opts: Opts = Opts::parse();
let config_path = opts
.config
.unwrap_or_else(|| PathBuf::from("qp_raker.toml"));
let config = config::RakerConfig::load(&config_path).context("Failed to load config")?;
eprintln!("{:#?}", config);
Ok(())
}

View File

@ -0,0 +1,37 @@
use anyhow::Context;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Config for a raker. All paths are relative to the config file if needed, but will be resolved
/// when loading.
pub struct RakerConfig {
/// Path to data files
data_dir: PathBuf,
/// Path to seeds
seed_dir: PathBuf,
/// Path to the raker's workbench (queue etc)
workbench_dir: PathBuf,
/// Directory where new rake packs will be emitted
emit_dir: PathBuf,
}
impl RakerConfig {
/// Loads a config at the specified path.
/// Will resolve all the paths in the RakerConfig for you.
pub fn load(path: &Path) -> anyhow::Result<RakerConfig> {
let config_dir = path.parent().context("Can't get parent of config file.")?;
let bytes = std::fs::read(path)?;
let mut raker_config: RakerConfig = toml::from_slice(&bytes)?;
raker_config.data_dir = config_dir.join(raker_config.data_dir);
raker_config.seed_dir = config_dir.join(raker_config.seed_dir);
raker_config.workbench_dir = config_dir.join(raker_config.workbench_dir);
raker_config.emit_dir = config_dir.join(raker_config.emit_dir);
Ok(raker_config)
}
}

View File

@ -1,4 +1,6 @@
pub mod raking;
pub mod config;
#[cfg(test)]
mod test;