""" Dependabro CLI module. """ import logging import os.path import sys import click from . import config, utils # Configure logging logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" ) @click.command() @click.argument( "target_directory", type=click.Path(exists=True, file_okay=False, dir_okay=True) ) @click.option("--refresh-cache", is_flag=True, help="Force refresh the modules cache") @click.option("--dry-run", is_flag=True, help="Dry run mode") def main(target_directory, dry_run, refresh_cache): """ Main function for the dependabro CLI. :param target_directory: :param dry_run: :param refresh_cache: :return: """ logging.debug("Dependabro started.") # Check if GITHUB_TOKEN is set if not config.GITHUB_TOKEN: logging.error("Error: GITHUB_TOKEN environment variable is not set.") sys.exit(1) # TODO: cache the latest terraform version like the modules latest_terraform_version = utils.get_latest_terraform_version() module_versions = {} success, modules = utils.get_terraform_modules(config.TERRAFORM_MODULE_LIST_FILE) if success: update_cache = refresh_cache or utils.is_cache_expired( config.MODULE_VERSIONS_CACHE_FILE, config.CACHE_DURATION ) if update_cache: logging.info("Refreshing modules cache.") module_versions = utils.get_all_module_versions(modules) utils.save_module_versions_cache( config.MODULE_VERSIONS_CACHE_FILE, module_versions ) else: success, module_versions = utils.load_module_versions_cache( config.MODULE_VERSIONS_CACHE_FILE ) if not success: logging.error("Error loading module versions cache. Exiting.") sys.exit(1) else: logging.error("Error loading module list. Exiting.") sys.exit(1) if not module_versions: logging.error("No module versions found. Exiting.") sys.exit(1) updates_made = False for root, dirs, files in os.walk(target_directory): # Exclude specified directories dirs[:] = [d for d in dirs if not d.startswith(config.DIRS_EXCLUDE_PREFIXES)] # Filter .tf files excluding specified files tf_files = [ f for f in files if f.endswith(".tf") and not f.startswith(config.FILES_EXCLUDE_PREFIXES) ] for file in tf_files: file_path = str(os.path.join(root, file)) updated = utils.parse_and_update_terraform_file( file_path, module_versions, latest_terraform_version, dry_run ) if updated or success: updates_made = True if not updates_made: logging.info("All modules are up to date.") sys.exit(0)