import click
from planning_system.db import DB


@click.command()
@click.argument("rolename")
@click.argument("username")
@click.pass_obj
def role(config, rolename, username):
    """
    Grant a role.
    """
    with DB(config.uri, config.echo) as db:
        session = db.session()
        name = f"[LCM\{username}]"
        commands = [f"CREATE USER {name} FOR LOGIN {name}",
                    f"ALTER ROLE [{rolename}] ADD MEMBER {name}"]
        for command in commands:
            session.execute(command)
        config.verbose_print("Executing "+command)
        session.commit()
