#!/usr/bin/python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-

# Authors:
#   Andy P. Whitcroft
#   Christian Ehrhardt

"""Dumps a list of currently running tests in Autopkgtest"""

__example__ = """
Display first listed test running on amd64 hardware:
  $ running-autopkgtests | grep amd64 | head -n1
  R     0:01:40 systemd-upstream               -          focal    amd64    upstream-systemd-ci/systemd-ci  - ['CFLAGS=-O0', 'DEB_BUILD_PROFILES=noudeb', 'TEST_UPSTREAM=1', 'CONFFLAGS_UPSTREAM=--werror -Dslow-tests=true', 'UPSTREAM_PULL_REQUEST=23153', 'GITHUB_STATUSES_URL=https://api.github.com/repos/systemd/systemd/statuses/cfb0935923dff8050315b5dd22ce8ab06461ff0e']
"""

import sys
from argparse import ArgumentParser, RawDescriptionHelpFormatter

from ubuntutools.running_autopkgtests import get_queued, get_running


def parse_args():
    description = (
        "Dumps a list of currently running and queued tests in Autopkgtest. "
        "Pass --running to only see running tests, or --queued to only see "
        "queued tests. Passing both will print both, which is the default behavior. "
    )

    parser = ArgumentParser(
        prog="running-autopkgtests",
        description=description,
        epilog=f"example: {__example__}",
        formatter_class=RawDescriptionHelpFormatter,
    )
    parser.add_argument(
        "-r",
        "--running",
        action="store_true",
        help="Print runnning autopkgtests (default: true)",
    )
    parser.add_argument(
        "-q",
        "--queued",
        action="store_true",
        help="Print queued autopkgtests (default: true)",
    )

    options = parser.parse_args()

    # If neither flag was specified, default to both not neither
    if not options.running and not options.queued:
        options.running = True
        options.queued = True

    return options


def main() -> int:
    args = parse_args()
    if args.running:
        print(get_running())
    if args.queued:
        print(get_queued())

    return 0


if __name__ == "__main__":
    sys.exit(main())
