From 2763ff2481573f9a035ed1e09ce2c4dffe5a1bba Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Mon, 14 Mar 2022 23:53:55 +0000 Subject: [PATCH] Add support for a Raker config --- .gitignore | 8 ++++--- Cargo.lock | 10 ++++++++ quickpeep_raker/Cargo.toml | 2 ++ quickpeep_raker/src/bin/qp-seeds.rs | 31 ++++++++++++++++++++++++ quickpeep_raker/src/config.rs | 37 +++++++++++++++++++++++++++++ quickpeep_raker/src/lib.rs | 2 ++ 6 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 quickpeep_raker/src/bin/qp-seeds.rs create mode 100644 quickpeep_raker/src/config.rs diff --git a/.gitignore b/.gitignore index 7c5e7e0..72240dd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -.idea -data/cf_ips.txt -data \ No newline at end of file +/.idea +/data/cf_ips.txt +/data +/qp_raker.toml +.*.swp diff --git a/Cargo.lock b/Cargo.lock index d21361f..bad66a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/quickpeep_raker/Cargo.toml b/quickpeep_raker/Cargo.toml index e11c875..2ad2cb1 100644 --- a/quickpeep_raker/Cargo.toml +++ b/quickpeep_raker/Cargo.toml @@ -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" diff --git a/quickpeep_raker/src/bin/qp-seeds.rs b/quickpeep_raker/src/bin/qp-seeds.rs new file mode 100644 index 0000000..1044c1b --- /dev/null +++ b/quickpeep_raker/src/bin/qp-seeds.rs @@ -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, +} + +#[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(()) +} diff --git a/quickpeep_raker/src/config.rs b/quickpeep_raker/src/config.rs new file mode 100644 index 0000000..2ce5b36 --- /dev/null +++ b/quickpeep_raker/src/config.rs @@ -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 { + 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) + } +} diff --git a/quickpeep_raker/src/lib.rs b/quickpeep_raker/src/lib.rs index 56fab88..fc8a82f 100644 --- a/quickpeep_raker/src/lib.rs +++ b/quickpeep_raker/src/lib.rs @@ -1,4 +1,6 @@ pub mod raking; +pub mod config; + #[cfg(test)] mod test;