summaryrefslogtreecommitdiff
path: root/src/bin/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/main.rs')
-rw-r--r--src/bin/main.rs43
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)
}
-
}