From f5740b97cecd2a2984e274fb5fbd141675e3be24 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 12 Nov 2017 15:06:06 +0100 Subject: main(): Add command line options Add options for: * Jenkins URL * Jenkins username * Jenkins token * GitHub token * Port Add a help option for usage information. All options except "port" are required. Didn't use `reqopt` because that panics when you try to ask for help without the required options. Now, when trying to run without a required option, the usage help is printed. Maybe we should have an error message instead. --- src/main.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 5882909..e9b7cc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,92 @@ +extern crate getopts; #[macro_use] extern crate rouille; extern crate kipper; +use std::env; use std::io::Read; +use getopts::Options; + use kipper::jenkins; use kipper::pull_request::CommitRef; +const DEFAULT_PORT: u16 = 8000; + +fn print_usage(opts: Options) { + let brief = "Usage: kipper --jenkins-url 'https://jenkins.example.com' --jenkins-user-id username --jenkins-token a72a57d448694703b2c3fd19e666ecc5 --github-token 1dc41fad0516460b870014b25b11847d"; + print!("{}", opts.usage(&brief)); +} + fn internal_server_error() -> rouille::Response { rouille::Response::text("500 Internal Server Error") .with_status_code(500) } fn main() { + let args: Vec = env::args().collect(); + + let mut opts = Options::new(); + opts.optopt("", "jenkins-url", "Jenkins URL (required)", "https://jenkins.example.com"); + opts.optopt("", "jenkins-user-id", "Jenkins user ID (required)", "USER_ID"); + opts.optopt("", "jenkins-token", "Jenkins API token (required)", "TOKEN"); + opts.optopt( + "", + "github-token", + "GitHub API token with \"repo:status\" permission (required)", + "TOKEN" + ); + opts.optopt("p", "port", "set port number", "PORT"); + opts.optflag("h", "help", "print this help menu"); + + let opt_matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(e) => panic!(e.to_string()), + }; + + if opt_matches.opt_present("h") { + print_usage(opts); + return; + } + + let jenkins_url = match opt_matches.opt_str("jenkins-url") { + Some(url) => url, + None => { + print_usage(opts); + return; + }, + }; + + let jenkins_user_id = match opt_matches.opt_str("jenkins-user-id") { + Some(user_id) => user_id, + None => { + print_usage(opts); + return; + }, + }; + + let jenkins_token = match opt_matches.opt_str("jenkins-token") { + Some(token) => token, + None => { + print_usage(opts); + return; + }, + }; + + let github_token = match opt_matches.opt_str("github-token") { + Some(token) => token, + None => { + print_usage(opts); + return; + }, + }; + + let port = match opt_matches.opt_str("p") { + Some(p) => p.parse().expect("Unable to parse specified port"), + None => DEFAULT_PORT, + }; + rouille::start_server("localhost:8000", move |request| { router!(request, (POST) (/github/pull_request_event) => { -- cgit v1.2.3