#!/usr/bin/env python3
"""Executable script for running statick against a single package."""

import argparse
import sys

from statick_tool.args import Args
from statick_tool.statick import Statick


def run(statick: Statick, parsed_args: argparse.Namespace) -> bool:
    """Run statick on a single package."""
    path = parsed_args.path
    issues, success = statick.run(path, parsed_args)
    if issues is None:
        statick.print_no_issues()
        return False
    for tool in issues:
        if issues[tool]:
            success = False
    return success


def main() -> None:
    """Run statick."""
    args = Args("Statick tool")
    args.parser.add_argument("path", help="Path of package or workspace to scan")

    statick = Statick(args.get_user_paths())
    statick.gather_args(args.parser)
    parsed_args = args.get_args()
    statick.set_logging_level(parsed_args)
    statick.get_config(parsed_args)
    statick.get_exceptions(parsed_args)

    if parsed_args.workspace:
        _, success = statick.run_workspace(parsed_args)
    else:
        success = run(statick, parsed_args)

    if parsed_args.check and not success:
        statick.print_exit_status(False)
        sys.exit(1)
    else:
        statick.print_exit_status(True)
        sys.exit(0)


if __name__ == "__main__":
    main()
