from datetime import datetime


def build_patterns(config, date=datetime.now()):
    """
    Format date strings for patterns. Takes in global config.

    Args:
        config: global settings config
        date: date str or object to override current datetime with

    Returns:
        The global settings config but with updated per cluster pattern sets
    """
    cluster_settings = config['clusters']

    # Set date override
    if isinstance(date, str):
        date = datetime.strptime(date, '%Y-%m-%d')

    # Fill in patterns
    for cluster in cluster_settings:
        for repo, repo_config in cluster['repositories'].items():
            repo_config['patterns'] = list(
                map(
                    lambda x: datetime.strftime(date, x),
                    repo_config['patterns']
                )
            )
            repo_config['patterns'] = list(set(repo_config['patterns']))

    return config