fn create_or_update_users(
    conn: &mut DbConnection,
    users_in_db: &[User],
    users_in_config: &[UserInConfig],
    dryrun: bool,
) -> Result<()> {
    let mut summary = vec![vec!["User".to_string(), "Action".to_string()]];
    summary.push(vec!["---".to_string(), "---".to_string()]);

    // Create or update users in database
    for user in users_in_config {
        let user_in_db = users_in_db.iter().find(|&u| u.name == user.name);
        match user_in_db {
            // User in config and in database
            Some(user_in_db) => {
                // Update password if `update_password` is set to true
                if user.update_password.unwrap_or(false) {
                    let sql = user.to_sql_update();

                    if dryrun {
                        info!("{}: {}", Purple.paint("Dry-run"), Purple.paint(sql));
                        summary.push(vec![
                            user.name.to_string(),
                            Green.paint("would update password").to_string(),
                        ]);
                    } else {
                        conn.execute(&sql, &[])?;
                        info!("{}: {}", Green.paint("Success"), Purple.paint(sql));
                        summary.push(vec![user.name.clone(), "password updated".to_string()]);
                    }
                } else {
                    // Do nothing if user is not changed
                    summary.push(vec![
                        user_in_db.name.clone(),
                        "no action (already exists)".to_string(),
                    ]);
                }
            }

            // User in config but not in database
            None => {
                let sql = user.to_sql_create();

                if dryrun {
                    info!("{}: {}", Purple.paint("Dry-run"), sql);
                    summary.push(vec![
                        user.name.clone(),
                        format!("would create (dryrun) {}", sql),
                    ]);
                } else {
                    conn.execute(&sql, &[])?;
                    info!("{}: {}", Green.paint("Success"), sql);
                    summary.push(vec![user.name.clone(), format!("created {}", sql)]);
                }
            }
        }
    }

    // TODO: Support delete users in db that are not in config
    for user in users_in_db {
        if !users_in_config.iter().any(|u| u.name == user.name) {
            // Update summary
            summary.push(vec![
                user.name.clone(),
                "no action (not in config)".to_string(),
            ]);
        }
    }

    // Show summary
    print_summary(summary);

    Ok(())
}
/// Apply users from config to database
///
/// Get list users from database and compare with config users
/// If user is in config but not in database, create it
/// If user is in database but not in config, delete it
/// If user is in both, compare passwords and update if needed
///
/// Show the summary as table of users created, updated, deleted
Apply users from config to database
Get list users from database and compare with config users
If user is in config but not in database, create it
If user is in database but not in config, delete it
If user is in both, compare passwords and update if needed

Show the summary as table of users created, updated, deleted
Apply users from config to database Get list users from database and compare with config users If user is in config but not in database create it If user is in database but not in config delete it If user is in both compare passwords and update if needed Show the summary as table of users created updated deleted
fn create_or_update_users(
    conn: &mut DbConnection,
    users_in_db: &[User],
    users_in_config: &[UserInConfig],
    dryrun: bool,
) -> Result<()> {
    let mut summary = vec![vec!["User".to_string(), "Action".to_string()]];
    summary.push(vec!["---".to_string(), "---".to_string()]);
    // Create or update users in database
    for user in users_in_config {
        let user_in_db = users_in_db.iter().find(|&u| u.name == user.name);
        match user_in_db {
            // User in config and in database
            Some(user_in_db) => {
                // Update password if `update_password` is set to true
                if user.update_password.unwrap_or(false) {
                    let sql = user.to_sql_update();
                    if dryrun {
                        info!("{}: {}", Purple.paint("Dry-run"), Purple.paint(sql));
                        summary.push(vec![
                            user.name.to_string(),
                            Green.paint("would update password").to_string(),
                        ]);
                    } else {
                        conn.execute(&sql, &[])?;
                        info!("{}: {}", Green.paint("Success"), Purple.paint(sql));
                        summary.push(vec![user.name.clone(), "password updated".to_string()]);
                    }
                } else {
                    // Do nothing if user is not changed
                    summary.push(vec![
                        user_in_db.name.clone(),
                        "no action (already exists)".to_string(),
                    ]);
                }
            }
            // User in config but not in database
            None => {
                let sql = user.to_sql_create();
                if dryrun {
                    info!("{}: {}", Purple.paint("Dry-run"), sql);
                    summary.push(vec![
                        user.name.clone(),
                        format!("would create (dryrun) {}", sql),
                    ]);
                } else {
                    conn.execute(&sql, &[])?;
                    info!("{}: {}", Green.paint("Success"), sql);
                    summary.push(vec![user.name.clone(), format!("created {}", sql)]);
                }
            }
        }
    }
    // TODO: Support delete users in db that are not in config
    for user in users_in_db {
        if !users_in_config.iter().any(|u| u.name == user.name) {
            // Update summary
            summary.push(vec![
                user.name.clone(),
                "no action (not in config)".to_string(),
            ]);
        }
    }
    // Show summary
    print_summary(summary);
    Ok(())
}
fn create_or_update_users ( conn : & mut DbConnection , users_in_db : & [ User ] , users_in_config : & [ UserInConfig ] , dryrun : bool , ) -> Result < ( ) > { let mut summary = vec ! [ vec [ " " to_string ( ) " " to_string ( ) ] ] ; summary . push ( vec ! [ " " to_string ( ) " " to_string ( ) ] ) ; // Create or update users in database for user in users_in_config { let user_in_db = users_in_db . iter ( ) . find ( | & u | u . name == user . name ) ; match user_in_db { // User in config and in database Some ( user_in_db ) => { // Update password if `update_password` is set to true if user . update_password . unwrap_or ( false ) { let sql = user . to_sql_update ( ) ; if dryrun { info ! ( " " Purple paint ( " " ) Purple paint ( sql ) ) ; summary . push ( vec ! [ user name to_string ( ) Green paint ( " " ) to_string ( ) ] ) ; } else { conn . execute ( & sql , & [ ] ) ? ; info ! ( " " Green paint ( " " ) Purple paint ( sql ) ) ; summary . push ( vec ! [ user name clone ( ) " " to_string ( ) ] ) ; } } else { // Do nothing if user is not changed summary . push ( vec ! [ user_in_db name clone ( ) " " to_string ( ) ] ) ; } } // User in config but not in database None => { let sql = user . to_sql_create ( ) ; if dryrun { info ! ( " " Purple paint ( " " ) sql ) ; summary . push ( vec ! [ user name clone ( ) format ( " " sql ) ] ) ; } else { conn . execute ( & sql , & [ ] ) ? ; info ! ( " " Green paint ( " " ) sql ) ; summary . push ( vec ! [ user name clone ( ) format ( " " sql ) ] ) ; } } } } // TODO: Support delete users in db that are not in config for user in users_in_db { if ! users_in_config . iter ( ) . any ( | u | u . name == user . name ) { // Update summary summary . push ( vec ! [ user name clone ( ) " " to_string ( ) ] ) ; } } // Show summary print_summary ( summary ) ; Ok ( ( ) ) }
Apply users from config to database
Get list users from database and compare with config users
If user is in config but not in database, create it
If user is in database but not in config, delete it
If user is in both, compare passwords and update if needed
Apply users from config to database Get list users from database and compare with config users If user is in config but not in database create it If user is in database but not in config delete it If user is in both compare passwords and update if needed
conn
&mut DbConnection
users_in_db
&[User]
users_in_config
&[UserInConfig]
dryrun
bool
conn
&mut DbConnection
users_in_db
&[User]
users_in_config
&[UserInConfig]
dryrun
bool