more option parsing + github ext

This commit is contained in:
Michael 2018-08-15 22:51:52 +00:00 committed by Michael Zhang
parent 433e50f841
commit 4529d4bac2
No known key found for this signature in database
GPG key ID: A1B65B603268116B
7 changed files with 63 additions and 10 deletions

4
Cargo.lock generated
View file

@ -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"

View file

@ -4,6 +4,9 @@ description = "Configurable webhook server."
version = "0.1.0"
authors = ["Michael Zhang <failed.down@gmail.com>"]
[workspace]
members = [".", "ext/github"]
[dependencies]
failure = "0.1"
hyper = "0.12"

6
ext/github/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "github"
version = "0.1.0"
authors = ["Michael <localhost>"]
[dependencies]

7
ext/github/src/lib.rs Normal file
View file

@ -0,0 +1,7 @@
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
assert_eq!(2 + 2, 4);
}
}

28
src/config.rs Normal file
View file

@ -0,0 +1,28 @@
use std::path::{Path, PathBuf};
pub struct Config {
pub root: PathBuf,
pub bind: String,
pub hook: Option<String>,
}
impl Config {
pub fn new(root: impl AsRef<Path>) -> 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<String>) -> Config {
if let Some(value) = value {
self.bind = value;
}
return self;
}
pub fn hook(mut self, value: Option<String>) -> Config {
self.hook = value;
return self;
}
}

View file

@ -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<P>(root: P) -> Result<(), Error>
where
P: AsRef<Path>,
{
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();

View file

@ -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<String>,
/// 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<String>,
}
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)
}