Skip to content

Commit

Permalink
aw-server: Added option parsing and --testing option
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-bjareholt committed Mar 27, 2020
1 parent cd00f31 commit c34b7f6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions aw-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fern = { version = "0.5", features = ["colored"] }
toml = "0.5"
gethostname = "0.2"
uuid = { version = "0.8", features = ["serde", "v4"] }
getopts = "0.2"

aw-datastore = { path = "../aw-datastore" }
aw-models = { path = "../aw-models" }
Expand Down
30 changes: 28 additions & 2 deletions aw-server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
#[macro_use]
extern crate log;
extern crate getopts;

use getopts::Options;
use rocket::config::Environment;
use std::env;

use aw_server::*;

fn print_usage(program: &str, opts: Options) {
let brief = format!("Usage: {} FILE [options]", program);
print!("{}", opts.usage(&brief));
}

fn main() {
use std::sync::Mutex;

let args: Vec<String> = env::args().collect();
let program = args[0].clone();

let mut opts = Options::new();
opts.optflag("", "testing", "run in testing mode");
opts.optflag("h", "help", "print this help menu");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => panic!(f.to_string()),
};
if matches.opt_present("h") {
print_usage(&program, opts);
return;
}

let env = Environment::active().expect("Failed to get current environment");
let testing = match env {
let mut testing = match env {
Environment::Production => false,
Environment::Development => true,
Environment::Staging => panic!("Staging environment not supported"),
};
// Always override environment if --testing is specified
if matches.opt_present("testing") {
testing = true;
}

logging::setup_logger(testing).expect("Failed to setup logging");

Expand Down Expand Up @@ -45,7 +72,6 @@ use std::path::PathBuf;
// TODO: Should we talk to upstream about this? This changes the behavior quite a lot so maybe they
// don't want this change?
fn site_data_dir(app: Option<&str>, _: Option<&str>) -> Result<PathBuf, ()> {
use std::env;
// Iterate over all XDG_DATA_DIRS and return first match that exists
match env::var_os("XDG_DATA_DIRS") {
Some(joined) => {
Expand Down

0 comments on commit c34b7f6

Please sign in to comment.