"""Command to list directories in a task."""
from typing import TextIO
import argparse
import sys
import asyncio

from inductiva import _cli, tasks


def list_files(args: argparse.Namespace, fout: TextIO = sys.stdout):
    task_id = args.id
    task = tasks.Task(task_id)
    directories = asyncio.run(task._list_files())  # pylint: disable=protected-access
    print(directories, file=fout)
    return 0


def register(parser):
    """Register the info tasks command."""
    subparser = parser.add_parser(
        "list-files",
        help="Lists files generated by a task recursively.",
        formatter_class=argparse.RawTextHelpFormatter)

    subparser.description = ("The `inductiva tasks list-files` command lists "
                             "the contents of a task's working directory while "
                             "the task in progress.")

    _cli.utils.add_watch_argument(subparser)
    subparser.add_argument("id", type=str, help="ID of the task to list files.")
    subparser.set_defaults(func=list_files)
