Skip to content

Commit

Permalink
Add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ViViDboarder committed Apr 11, 2019
1 parent cbdd7b8 commit 5785148
Showing 1 changed file with 47 additions and 15 deletions.
62 changes: 47 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
extern crate ldap3;

use std::collections::HashSet;
use std::error::Error;
use std::env;
use std::thread::sleep;
use std::time::Duration;

Expand All @@ -9,33 +11,63 @@ use ldap3::{DerefAliases, LdapConn, Scope, SearchEntry, SearchOptions};
mod bw_admin;
mod config;

/// Container for args parsed from the command line
struct ParsedArgs {
start_loop: bool,
}

impl ParsedArgs {
pub parse() -> ParsedArgs {
let mut parsed_args = ParsedArgs {};
for arg in env::args().collect() {
if arg == "--loop" {
parsed_args.start_loop = true;
}
}

parsed_args.clone()
}
}

fn main() {
let config = config::Config::from_file();
let mut client = bw_admin::Client::new(
config.get_bitwarden_url().clone(),
config.get_bitwarden_admin_token().clone(),
);

match client.users() {
Ok(users) => {
for user in users {
println!("Existing user: {}", user.get_email());
}
}
Err(e) => {
println!("Could not get users");
panic!("{}", e);
}
let parsed_args = ParsedArgs::parse();
if let Err(e) = invite_users(&config, &mut client, parsed_args.start_loop) {
panic!("{}", e);
}
}

// TODO: Use command line args to differentiate if we invite once or start loop
if let Err(e) = invite_from_ldap(&config, &mut client) {
println!("{}", e);
/// Invites new users to Bitwarden from LDAP
fn invite_users(
config: &config::Config,
client: &mut bw_admin::Client,
start_loop: bool,
) -> Result((), Box<Error>> {
let user_emails = get_existing_users(&mut client)?;

if start_loop {
start_sync_loop(&config, &mut client)?;
} else {
invite_from_ldap(&config, &mut client)?;
}

if let Err(e) = start_sync_loop(&config, &mut client) {
println!("{}", e);
Ok(())
}

/// Creates set of email addresses for users that already exist in Bitwarden
fn get_existing_users(client: &mut bw_admin::Client) -> Result<HashSet<String>, Box<Error>> {
let all_users = client.users()?;
let mut user_emails = HashSet::with_capacity(all_users.len());
for user in client.users()? {
user_emails.insert(user.get_email());
}

Ok(user_emails)
}

/// Creates an LDAP connection, authenticating if necessary
Expand Down

0 comments on commit 5785148

Please sign in to comment.