from typing import Dict
from typing import List
from typing import Tuple
import asyncio
import collections


async def get_versions(project: str) -> Dict[str, List[Tuple[str, ...]]]:
    """
    Get dictionary of appengine versions from Google.
    Key is the service name, value is a line containing information about the version.
    """
    cmd = ["gcloud", "--project", project, "app", "versions", "list"]
    print("Calling command:", " ".join(cmd))
    process = await asyncio.create_subprocess_exec(*cmd, stdout=asyncio.subprocess.PIPE)
    stdout, _ = await process.communicate()
    output = stdout.decode().strip().split("\n")
    versions = collections.defaultdict(list)
    # Remove header line
    for line in output[1:]:
        line = tuple(line.split())
        service = line[0]
        versions[service].append(line)
    return versions