diff options
author | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2017-06-17 15:08:42 -0400 |
---|---|---|
committer | Kjetil Orbekk <kjetil.orbekk@gmail.com> | 2017-06-17 15:16:27 -0400 |
commit | b1d3703cf2a77689755e1bfbc3ee235338457a19 (patch) | |
tree | 5f085d87c9718d2f1964c4ce43daab9b470f9e18 /src/bin/main.rs | |
parent | 30d40fea83f4d9e06e14f260c23a31020bd39509 (diff) |
feature: Add user command.
* Finish command to create user.
* Change sqlite => rusqlite dependency.
Diffstat (limited to 'src/bin/main.rs')
-rw-r--r-- | src/bin/main.rs | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs index a4d489d..e57ea6a 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -6,7 +6,7 @@ extern crate systemhttp; extern crate env_logger; #[macro_use] extern crate clap; -extern crate sqlite; +extern crate rusqlite; extern crate rpassword; use rpassword::read_password; @@ -15,6 +15,22 @@ use systemhttp::auth; use clap::{App, AppSettings, Arg, SubCommand}; +fn create_user_prompt() -> Option<(String, String)> { + print!("Username: "); + io::stdout().flush(); + let mut user = String::new(); + io::stdin().read_line(&mut user).unwrap(); + let password = rpassword::prompt_password_stdout("Password: ").unwrap(); + let confirmation = + rpassword::prompt_password_stdout("Repeat password: ").unwrap(); + + if password != confirmation { + error!("Passwords don't match"); + return None + } + Some((user, password)) +} + fn main() { let matches = App::new("systemhttpd") .version("0.1") @@ -42,7 +58,7 @@ fn main() { let db_file = matches.value_of("db_file").unwrap(); env_logger::init().unwrap(); - let mut conn = sqlite::Connection::open(db_file) + let mut conn = rusqlite::Connection::open(db_file) .expect(format!("opening sqlite database at {}", db_file).as_str()); systemhttp::db::init(&mut conn); @@ -51,22 +67,14 @@ fn main() { println!("Serving on {}", port); }; - let create_admin_user = || { - println!("Create admin user"); - print!("Username: "); - io::stdout().flush(); - let mut user = String::new(); - io::stdin().read_line(&mut user).unwrap(); - let password = rpassword::prompt_password_stdout("Password: ").unwrap(); - let confirmation = - rpassword::prompt_password_stdout("Repeat password: ").unwrap(); - - if password != confirmation { - println!("\nPasswords don't match"); - return + let mut create_admin_user = || { + info!("Create admin user"); + if let Some((user, password)) = create_user_prompt() { + let enc = auth::encode("test_salt", password.as_str()); + systemhttp::db::insert_user( + &mut conn, user.as_str(), + &enc).expect("create user"); } - println!("\nCreating user {} with password (hashed) {:?}", - user, auth::encode("test_salt", password.as_str())); }; match matches.subcommand_name() { @@ -74,5 +82,4 @@ fn main() { Some("create_admin_user") => create_admin_user(), x => panic!("Don't know about subcommand: {:?}", x) } - } |