From 4529d4bac2e58e6e54c7f17c8c39e053635d74fb Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 15 Aug 2018 22:51:52 +0000 Subject: [PATCH] more option parsing + github ext --- Cargo.lock | 4 ++++ Cargo.toml | 3 +++ ext/github/Cargo.toml | 6 ++++++ ext/github/src/lib.rs | 7 +++++++ src/config.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 11 +++++------ src/main.rs | 14 ++++++++++---- 7 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 ext/github/Cargo.toml create mode 100644 ext/github/src/lib.rs create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 88b8b14..dd3a6de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -239,6 +239,10 @@ dependencies = [ "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "github" +version = "0.1.0" + [[package]] name = "h2" version = "0.1.12" diff --git a/Cargo.toml b/Cargo.toml index 2938378..d4ef466 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,9 @@ description = "Configurable webhook server." version = "0.1.0" authors = ["Michael Zhang "] +[workspace] +members = [".", "ext/github"] + [dependencies] failure = "0.1" hyper = "0.12" diff --git a/ext/github/Cargo.toml b/ext/github/Cargo.toml new file mode 100644 index 0000000..1ca1d36 --- /dev/null +++ b/ext/github/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "github" +version = "0.1.0" +authors = ["Michael "] + +[dependencies] diff --git a/ext/github/src/lib.rs b/ext/github/src/lib.rs new file mode 100644 index 0000000..31e1bb2 --- /dev/null +++ b/ext/github/src/lib.rs @@ -0,0 +1,7 @@ +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + assert_eq!(2 + 2, 4); + } +} diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..4d37664 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,28 @@ +use std::path::{Path, PathBuf}; + +pub struct Config { + pub root: PathBuf, + pub bind: String, + pub hook: Option, +} + +impl Config { + pub fn new(root: impl AsRef) -> Self { + let root = root.as_ref().to_path_buf(); + assert!(root.exists()); + + let bind = "0.0.0.0:5000".to_owned(); + let hook = None; + Config { root, bind, hook } + } + pub fn bind(mut self, value: Option) -> Config { + if let Some(value) = value { + self.bind = value; + } + return self; + } + pub fn hook(mut self, value: Option) -> Config { + self.hook = value; + return self; + } +} diff --git a/src/lib.rs b/src/lib.rs index 7805159..21b23c2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ extern crate regex; extern crate toml; extern crate walkdir; +pub mod config; pub mod handler; pub mod hook; @@ -29,6 +30,7 @@ use notify::{RecommendedWatcher, RecursiveMode, Watcher}; use regex::Regex; use walkdir::WalkDir; +pub use config::Config; pub use handler::*; use hook::*; @@ -172,13 +174,10 @@ where } /// Main entry point of the entire application. -pub fn run

(root: P) -> Result<(), Error> -where - P: AsRef, -{ - load_config(&root); +pub fn run(config: &Config) -> Result<(), Error> { + load_config(&config.root); - let v = root.as_ref().to_path_buf(); + let v = config.root.clone(); thread::spawn(|| watch(v)); let addr = ([127, 0, 0, 1], 3000).into(); diff --git a/src/main.rs b/src/main.rs index 8e4f95a..27290ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,22 +5,28 @@ extern crate failure; use std::path::PathBuf; +use dip::Config; use failure::Error; use structopt::StructOpt; #[derive(Debug, StructOpt)] struct Opt { - /// The root configuration directory for dip. + /// The root configuration directory for dip. This argument is required. #[structopt(short = "d", long = "root", parse(from_os_str))] root: PathBuf, - /// A string containing the address to bind to. + /// A string containing the address to bind to. This defaults to "0.0.0.0:5000". #[structopt(short = "b", long = "bind")] bind: Option, + /// If a hook is specified here, it will be triggered manually exactly once and then the + /// program will exit rather than running as a server. + #[structopt(short = "h", long = "hook")] + hook: Option, } fn main() -> Result<(), Error> { let opt = Opt::from_args(); println!("{:?}", opt); - assert!(opt.root.exists()); - dip::run(opt.root) + + let config = Config::new(opt.root).bind(opt.bind).hook(opt.hook); + dip::run(&config) }