Commit fa82a56d authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Use structopt to parse option from commandline, make config file and database file configurable

parent f70c3094
......@@ -11,7 +11,7 @@ rusqlite = "0.13.0"
time = "0.1.40"
iron = "0.5.1"
rand = "*"
rand = "0.5.5"
mount ="0.3"
router = "0.5"
params = "0.7"
......@@ -23,6 +23,7 @@ serde_derive = "1.0"
handlebars-iron = "0.25.1"
iron-sessionstorage = "*"
serde_yaml = "*"
structopt = "*"
reqwest = "0.6.2"
......
use db_objects::*;
use ::functions;
use std::path::{Path};
pub trait MedalConnection {
fn create() -> Self;
fn create(file: &Path) -> Self;
fn dbtype(&self) -> &'static str;
fn migration_already_applied(&self, name: &str) -> bool;
......
......@@ -9,6 +9,8 @@ use rand::{thread_rng, Rng, distributions::Alphanumeric};
use time;
use std::path::{Path};
use ::functions; // todo: remove (usertype in db)
......@@ -17,8 +19,8 @@ fn hash_password(password: &str, hash: &str) -> String {
}
impl MedalConnection for Connection {
fn create() -> Connection {
Connection::open("blub.db").unwrap()
fn create(file: &Path) -> Connection {
Connection::open(file).unwrap()
}
fn dbtype(&self) -> &'static str {
......
......@@ -7,6 +7,9 @@ extern crate router;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate structopt;
extern crate rusqlite;
extern crate iron_sessionstorage;
extern crate urlencoded;
......@@ -42,32 +45,51 @@ mod functions;
use std::path;
use std::fs;
use std::path::{Path,PathBuf};
use structopt::StructOpt;
#[derive(Serialize, Deserialize)]
pub struct Config {
oauth_client_id: Option<String>,
oauth_client_secret: Option<String>,
oauth_access_token_url: Option<String>,
oauth_user_data_url: Option<String>,
database_file: Option<PathBuf>,
}
fn read_config_from_file(filename: &str) -> Config {
fn read_config_from_file(file: &Path) -> Config {
use std::io::Read;
if let Ok(mut file) = fs::File::open(filename) {
if let Ok(mut file) = fs::File::open(file) {
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
serde_json::from_str(&contents).unwrap()
} else {
println!("Configuration file '{}' not found.", filename);
println!("Configuration file '{}' not found.", file.to_str().unwrap_or("<Encoding error>"));
Config {
oauth_client_id: None,
oauth_client_secret: None,
oauth_access_token_url: None,
oauth_user_data_url: None,
database_file: None,
}
}
}
#[derive(StructOpt, Debug)]
#[structopt()]
struct Opt {
/// Config file to use (default: 'config.json')
#[structopt(short = "c", long = "config", default_value = "config.json", parse(from_os_str))]
configfile: PathBuf,
/// Database file to use (default: from config file or 'medal.db')
#[structopt(short = "d", long = "database", parse(from_os_str))]
databasefile: Option<PathBuf>,
}
fn read_contest(p: &path::PathBuf) -> Option<Contest> {
use std::fs::File;
use std::io::Read;
......@@ -134,9 +156,19 @@ fn refresh_all_contests(conn : &mut Connection) {
}
fn main() {
let config = read_config_from_file("config.json");
let opt = Opt::from_args();
println!("{:?}", opt);
let mut config = read_config_from_file(&opt.configfile);
if opt.databasefile.is_some() { config.database_file = opt.databasefile; }
//if config.database_file.is_none() { config.database_file = Some(.to_path_buf()); }
let mut conn = match config.database_file {
Some(ref path) => Connection::create(path),
None => Connection::create(&Path::new("medal.db")),
};
let mut conn = Connection::create();
db_apply_migrations::test(&mut conn);
refresh_all_contests(&mut conn);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment