summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs109
1 files changed, 79 insertions, 30 deletions
diff --git a/src/main.rs b/src/main.rs
index 32333f1..f88b7e2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,36 +1,85 @@
#[macro_use]
extern crate clap;
+use clap::App;
+use clap::Arg;
+use clap::SubCommand;
+use diesel::connection::Connection;
+use diesel::pg::PgConnection;
fn main() {
- let matches = clap_app!(pjournal =>
- (version: "0.1")
- (author: "KJ Ørbekk <kj@orbekk.com>")
- (about: "Practice Journaling")
- (@arg strava_client_secret:
- --strava_client_secret
- +required +takes_value
- "Client secret for strava authentication")
- (@arg strava_client_id:
- --strava_client_id
- +required +takes_value
- "Client id for strava authentication")
- (@arg base_url:
- --base_url
- +takes_value
- "Endpoint for this web app")
- )
- .get_matches();
+ let matches = App::new("pjournal")
+ .version("0.1")
+ .author("KJ Ørbekk <kj@orbekk.com>")
+ .about("Practice Journaling")
+ .arg(
+ Arg::with_name("database_url")
+ .long("database_url")
+ .required(true)
+ .takes_value(true)
+ .help("URL to postgresql database"),
+ )
+ .arg(
+ Arg::with_name("base_url")
+ .long("base_url")
+ .takes_value(true)
+ .help("Endpoint for this web server"),
+ )
+ .subcommand(
+ SubCommand::with_name("init")
+ .about("initialize database config")
+ .arg(
+ Arg::with_name("rocket_secret_key")
+ .long("rocket_secret_key")
+ .takes_value(true)
+ .required(true)
+ .help("Secret passed to rocket for encrypted cookies"),
+ )
+ .arg(
+ Arg::with_name("strava_client_secret")
+ .long("strava_client_secret")
+ .takes_value(true)
+ .required(true)
+ .help("Client secret for strava authentication"),
+ )
+ .arg(
+ Arg::with_name("strava_client_id")
+ .long("strava_client_id")
+ .takes_value(true)
+ .required(true)
+ .help("Client id for strava authentication"),
+ ),
+ )
+ .subcommand(
+ SubCommand::with_name("adduser")
+ .about("add a user account")
+ .arg(Arg::with_name("USERNAME").required(true).index(1))
+ .arg(Arg::with_name("PASSWORD").required(true).index(2)),
+ )
+ .get_matches();
- let config = pjournal::Config {
- client_id: matches
- .value_of("strava_client_id")
- .unwrap().to_string(),
- client_secret: matches
- .value_of("strava_client_secret")
- .unwrap().to_string(),
- base_url: matches
- .value_of("base_url")
- .unwrap_or("http://localhost:8000").to_string(),
- };
- pjournal::start_server(config);
+ let base_url = matches
+ .value_of("base_url")
+ .unwrap_or("http://localhost:8000");
+
+ let db_url = matches.value_of("database_url").unwrap();
+ let conn = PgConnection::establish(db_url).unwrap();
+
+ if let Some(matches) = matches.subcommand_matches("init") {
+ let config = pjournal::models::Config {
+ strava_client_id: matches.value_of("strava_client_id").unwrap(),
+ strava_client_secret: matches.value_of("strava_client_secret").unwrap(),
+ rocket_secret_key: matches.value_of("rocket_secret_key").unwrap(),
+ };
+
+ pjournal::db::create_config(&conn, &config);
+ } else if let Some(matches) = matches.subcommand_matches("adduser") {
+ let user = matches.value_of("USERNAME").unwrap();
+ let password = matches.value_of("PASSWORD").unwrap();
+ pjournal::db::adduser(&conn, user, password).unwrap();
+ } else {
+ let config = pjournal::server::Params {
+ base_url: base_url.to_string(),
+ };
+ pjournal::server::start(db_url, config);
+ }
}