# Path: installer/configurators/common.py
# def update_main_terraform_vars(branch, adminemail, stackprefix, region, tags):
#     """
#         Method parse the parameters send from run.py and these common variables
#         are set in terraform.tfvars and other files needed
#     """
#     jazz_branch = branch
#     cognito_details = [adminemail, passwd_generator()]
#     jazz_account_id = get_account_id()
#     prefix = stackprefix.lower()
#     tag_exempt = (datetime.datetime.today() +
#                   datetime.timedelta(days=1)).strftime("%m/%d/%Y")
# 
#     # ----------------------------------------------------------
#     # Populate Terraform variables in terraform.tfvars
#     # -----------------------------------------------------------
# 
#     # populating BRANCH name
#     replace_tfvars('github_branch', jazz_branch, get_tfvars_file())
# 
#     # populating region
#     replace_tfvars('region', region, get_tfvars_file())
#     os.environ['AWS_DEFAULT_REGION'] = region
# 
#     # Populating Jazz Account ID
#     replace_tfvars('jazz_accountid', jazz_account_id, get_tfvars_file())
# 
#     # Populating Cognito Details
#     replace_tfvars('cognito_pool_username', cognito_details[0],
#                    get_tfvars_file())
#     replace_tfvars('cognito_pool_password', cognito_details[1],
#                    get_tfvars_file())
# 
#     # Generate ACL DB password
#     replace_tfvars('acl_db_password', uuid.uuid4().hex,
#                    get_tfvars_file())
# 
#     # Set the default values of scm_username/passwd to the cognito credentials
#     # If using existing SCM instance, that scenario (e.g. 1) will obviously overwrite these values.
#     # For some reason Gitlab needs the username to conform to this specific regex
#     replace_tfvars('scm_username', re.sub('[^a-zA-Z0-9_-]', '-', cognito_details[0]), get_tfvars_file())
#     replace_tfvars('scm_passwd', cognito_details[1], get_tfvars_file())
# 
#     # Populating Jazz Tag env
#     replace_tfvars('envPrefix', prefix, get_tfvars_file())
#     replace_tfvars('tagsExempt', tag_exempt, get_tfvars_file())
# 
#     # Populating user tags
#     if tags:
#         input_tags = prepare_tags(tags)
#         try:
#             aws_tags, aws_formatted_tags = validate_replication_tags(input_tags)
#             replace_tfvars("aws_tags", str(aws_tags), get_tfvars_file())
#             replace_tfvars("additional_tags", str(aws_formatted_tags), get_tfvars_file(), quoteVal=False)
#         except ValueError as err:
#             print("Invalid Tag!" + str(err))
#             exit(0)


import click
import os
from installer.configurators.common import update_main_terraform_vars
    "--tags",
    "-t",
    help=('Specify AWS tags as key/value pairs, e.g: -t Key=stackenv,Value=production. '
          'You may specify this option multiple times to add multiple key-value pairs, '
          'e.g: -t Key=stackenv,Value=production -t Key=admin,Value=dinky@nope.com'),
    multiple=True
)
@click.option(
    "--stackprefix",
    "-p",
    help='Specify a string prefix name for your stack (limited to 13 chars)',
    # TODO
    # type=click.IntRange(0, 13),
    # type=stackprefix_type,
    prompt=True
)
@click.option(
    "--adminemail",
    "-e",
    help='Specify an admin email address (will be used for Jazz admin login)',
    prompt=True
)
@click.option('--aws_access_key', prompt=True, envvar='AWS_ACCESS_KEY_ID')
@click.option('--aws_secret_key', prompt=True, envvar='AWS_SECRET_ACCESS_KEY')
def install(branch, adminemail, stackprefix, region, tags, aws_access_key, aws_secret_key):
    """Installs the Jazz Stack."""
    os.environ['AWS_ACCESS_KEY_ID'] = aws_access_key
    os.environ['AWS_SECRET_ACCESS_KEY'] = aws_secret_key
    click.secho('\n\nConfiguring install', fg='blue')
    click.secho('\nLogging to `install.log`', fg='green')

# Path: installer/configurators/common.py
# def update_main_terraform_vars(branch, adminemail, stackprefix, region, tags):
#     """
#         Method parse the parameters send from run.py and these common variables
#         are set in terraform.tfvars and other files needed
#     """
#     jazz_branch = branch
#     cognito_details = [adminemail, passwd_generator()]
#     jazz_account_id = get_account_id()
#     prefix = stackprefix.lower()
#     tag_exempt = (datetime.datetime.today() +
#                   datetime.timedelta(days=1)).strftime("%m/%d/%Y")
# 
#     # ----------------------------------------------------------
#     # Populate Terraform variables in terraform.tfvars
#     # -----------------------------------------------------------
# 
#     # populating BRANCH name
#     replace_tfvars('github_branch', jazz_branch, get_tfvars_file())
# 
#     # populating region
#     replace_tfvars('region', region, get_tfvars_file())
#     os.environ['AWS_DEFAULT_REGION'] = region
# 
#     # Populating Jazz Account ID
#     replace_tfvars('jazz_accountid', jazz_account_id, get_tfvars_file())
# 
#     # Populating Cognito Details
#     replace_tfvars('cognito_pool_username', cognito_details[0],
#                    get_tfvars_file())
#     replace_tfvars('cognito_pool_password', cognito_details[1],
#                    get_tfvars_file())
# 
#     # Generate ACL DB password
#     replace_tfvars('acl_db_password', uuid.uuid4().hex,
#                    get_tfvars_file())
# 
#     # Set the default values of scm_username/passwd to the cognito credentials
#     # If using existing SCM instance, that scenario (e.g. 1) will obviously overwrite these values.
#     # For some reason Gitlab needs the username to conform to this specific regex
#     replace_tfvars('scm_username', re.sub('[^a-zA-Z0-9_-]', '-', cognito_details[0]), get_tfvars_file())
#     replace_tfvars('scm_passwd', cognito_details[1], get_tfvars_file())
# 
#     # Populating Jazz Tag env
#     replace_tfvars('envPrefix', prefix, get_tfvars_file())
#     replace_tfvars('tagsExempt', tag_exempt, get_tfvars_file())
# 
#     # Populating user tags
#     if tags:
#         input_tags = prepare_tags(tags)
#         try:
#             aws_tags, aws_formatted_tags = validate_replication_tags(input_tags)
#             replace_tfvars("aws_tags", str(aws_tags), get_tfvars_file())
#             replace_tfvars("additional_tags", str(aws_formatted_tags), get_tfvars_file(), quoteVal=False)
#         except ValueError as err:
#             print("Invalid Tag!" + str(err))
#             exit(0)


# Path: installer/cli/installcli.py
import click
import os
from installer.configurators.common import update_main_terraform_vars

    "--tags",
    "-t",
    help=('Specify AWS tags as key/value pairs, e.g: -t Key=stackenv,Value=production. '
          'You may specify this option multiple times to add multiple key-value pairs, '
          'e.g: -t Key=stackenv,Value=production -t Key=admin,Value=dinky@nope.com'),
    multiple=True
)
@click.option(
    "--stackprefix",
    "-p",
    help='Specify a string prefix name for your stack (limited to 13 chars)',
    # TODO
    # type=click.IntRange(0, 13),
    # type=stackprefix_type,
    prompt=True
)
@click.option(
    "--adminemail",
    "-e",
    help='Specify an admin email address (will be used for Jazz admin login)',
    prompt=True
)
@click.option('--aws_access_key', prompt=True, envvar='AWS_ACCESS_KEY_ID')
@click.option('--aws_secret_key', prompt=True, envvar='AWS_SECRET_ACCESS_KEY')
def install(branch, adminemail, stackprefix, region, tags, aws_access_key, aws_secret_key):
    """Installs the Jazz Stack."""
    os.environ['AWS_ACCESS_KEY_ID'] = aws_access_key
    os.environ['AWS_SECRET_ACCESS_KEY'] = aws_secret_key
    click.secho('\n\nConfiguring install', fg='blue')
    click.secho('\nLogging to `install.log`', fg='green')

    update_main_terraform_vars(branch, adminemail, stackprefix, region, tags)