Skip to content

Index

taskcat

taskcat python module

Template(template_path, project_root='', url='', s3_key_prefix='', template_cache=tcat_template_cache)

Source code in taskcat/_cfn/template.py
def __init__(
    self,
    template_path: Union[str, Path],
    project_root: Union[str, Path] = "",
    url: str = "",
    s3_key_prefix: str = "",
    template_cache: TemplateCache = tcat_template_cache,
):
    self.template_cache = template_cache
    self.template_path: Path = Path(template_path).expanduser().resolve()
    self.template = self.template_cache.get(str(self.template_path))
    with open(template_path, "r", encoding="utf-8") as file_handle:
        self.raw_template = file_handle.read()
    project_root = (
        project_root if project_root else self.template_path.parent.parent
    )
    self.project_root = Path(project_root).expanduser().resolve()
    self.url = url
    self._s3_key_prefix = s3_key_prefix
    self.children: List[Template] = []
    self._find_children()

write()

writes raw_template back to file, and reloads decoded template, useful if the template has been modified

Source code in taskcat/_cfn/template.py
def write(self):
    """writes raw_template back to file, and reloads decoded template, useful if
    the template has been modified"""
    with open(str(self.template_path), "w", encoding="utf-8") as file_handle:
        file_handle.write(self.raw_template)
    self.template = cfnlint.decode.cfn_yaml.load(self.template_path)
    self._find_children()

main(cli_core_class=CliCore, exit_func=exit_with_code)

Main entry point for the TaskCat CLI application.

This function orchestrates the entire CLI workflow including: - Signal handling setup for graceful interruption - Logging configuration - Command-line argument parsing - CLI module initialization and execution - Error handling and reporting

Parameters:

Name Type Description Default
cli_core_class class

CLI core class to use for parsing and execution. Defaults to CliCore. Used for dependency injection in testing.

CliCore
exit_func callable

Function to call for program exit. Defaults to exit_with_code. Used for testing.

exit_with_code

Raises:

Type Description
TaskCatException

For known TaskCat-specific errors

Exception

For unexpected errors during execution

Source code in taskcat/_cli.py
def main(cli_core_class=CliCore, exit_func=exit_with_code):
    """
    Main entry point for the TaskCat CLI application.

    This function orchestrates the entire CLI workflow including:
    - Signal handling setup for graceful interruption
    - Logging configuration
    - Command-line argument parsing
    - CLI module initialization and execution
    - Error handling and reporting

    Args:
        cli_core_class (class, optional): CLI core class to use for parsing and execution.
                                        Defaults to CliCore. Used for dependency injection
                                        in testing.
        exit_func (callable, optional): Function to call for program exit. 
                                      Defaults to exit_with_code. Used for testing.

    Raises:
        TaskCatException: For known TaskCat-specific errors
        Exception: For unexpected errors during execution
    """
    # Set up signal handler for graceful interruption (Ctrl+C)
    signal.signal(signal.SIGINT, _sigint_handler)

    # Configure logging based on command-line arguments
    log_level = _setup_logging(sys.argv)

    # Get command-line arguments, default to help if none provided
    args = sys.argv[1:]
    if not args:
        args.append("-h")

    try:
        # Display welcome banner and version information
        _welcome()

        # Get the currently installed version of TaskCat
        version = get_installed_version()

        # Initialize the CLI core with modules and configuration
        cli = cli_core_class(NAME, _cli_modules, DESCRIPTION, version, GLOBAL_ARGS.ARGS)

        # Parse the command-line arguments
        cli.parse(args)

        # Extract and set the AWS profile if specified
        _default_profile = cli.parsed_args.__dict__.get("_profile")
        if _default_profile:
            GLOBAL_ARGS.profile = _default_profile

        # Execute the parsed command
        cli.run()

    except TaskCatException as e:
        # Handle known TaskCat exceptions with appropriate logging
        LOG.error(str(e), exc_info=_print_tracebacks(log_level))
        exit_func(1)
    except Exception as e:  # pylint: disable=broad-except
        # Handle unexpected exceptions with full error details
        LOG.error(
            "%s %s", e.__class__.__name__, str(e), exc_info=_print_tracebacks(log_level)
        )
        exit_func(1)