Exemplo n.º 1
0
    def save_harvest(cls, filename: str, content: Any):
        """Save the content into the filename if the file format
        is supported.

        Args:
            filename (str): [Filename. I.e myfile.json]
            content (Any): [The content to be saved.]
        """

        try:
            cls._validate_format(filename)
        except NotSupportedFileFormat as ex:
            return formater(
                ex,
                AlertTagMessage.ERROR,
            )

        save_file_data(
            content,
            filename,
            cls.FILE_TYPE,
        )
        return formater(
            f"File successfully saved to {os.getcwd()}/{filename}",
            AlertTagMessage.DONE,
        )
Exemplo n.º 2
0
def get_record(_id: str) -> dict:
    """In charge of get the specific ID passed from the context
    and return the payload related to it in the history json-file.

    Args:
        id ([str]): [description]

    Returns:
        [tuple[dict|str]]: [
            return a dict if the record was found or a string
            if not
        ]
    """

    try:
        _history = load_file_data(CACHE_FILE_PATH)
    except FileNotFoundError:
        return formater(
            f"ID '{_id}' has not been found. Did you delete the history?",
            AlertTagMessage.ERROR,
        )

    for record in _history:
        if str(_id) == str(record["id"]):
            return record

    return formater(f"No matches found with the ID '{_id}'.",
                    AlertTagMessage.ERROR)
Exemplo n.º 3
0
def clear():
    """Clean history."""

    try:
        os.remove(CACHE_FILE_PATH)
    except FileNotFoundError:
        return formater(
            "Nothing to delete.",
            AlertTagMessage.INFO,
        )
    formater("Cleaned succesfully.", AlertTagMessage.DONE)
Exemplo n.º 4
0
def version():
    """
    Show the project version.
    """

    version = pkg_resources.require("rdap-cli")[0].version
    return formater(f"Rdap CLI version: {version}", AlertTagMessage.INFO)
Exemplo n.º 5
0
def download(filename):
    """Download the history into a file."""

    try:
        _history = load_file_data(CACHE_FILE_PATH)
    except FileNotFoundError:
        return formater("Nothing to download.", AlertTagMessage.INFO)

    Save().save_harvest(filename, _history)
Exemplo n.º 6
0
def generate_table():
    """Just as the function says, generate a table based on the
    get_headers and get_content functions.

    Returns:
        [type]: [return a table]
    """
    try:
        history = load_file_data(CACHE_FILE_PATH)
    except FileNotFoundError:
        return formater("No records available yet. Make a query first.",
                        AlertTagMessage.INFO)

    headers = get_headers(history)
    content = get_content(history)
    return click.echo(
        tabulate(content, headers, tablefmt="fancy_grid", stralign="center"))
Exemplo n.º 7
0
def gather(domain: str, filename: str) -> None:
    """Gather the domain information and prints in the shell.
    Give a valid domain name. In example: 'google.com', 'mydomain.net', etc.
    """

    try:
        domain_validator(domain)
    except (DomainWithSubdomain, DomainWithHttp, DomainValidationError) as ex:
        return formater(ex, AlertTagMessage.ERROR)

    schema = RdapApi(domain).query()

    if filename:
        return Save().save_harvest(filename, schema)

    message = format_domain_output(schema)

    click.echo(message)
Exemplo n.º 8
0
def check(domain) -> None:
    """Check if the domain is available or not."""

    try:
        domain_validator(domain)
    except (
            DomainWithSubdomain,
            DomainWithHttp,
            DomainValidationError,
    ) as ex:
        return formater(ex, AlertTagMessage.ERROR)

    schema = RdapApi(domain).query()
    is_available = get_availability(schema)
    message = f"""
    Domain: {click.style(domain.upper(), MessageColors.WHITE, bold=True)}
    Status: {is_available}
    Rdap Host: {form_hostname(schema.get("query_host", UNDEFINED_DATA))}
    Query host: {schema.get("query_host", UNDEFINED_DATA)}
    """
    click.echo(message)