diff --git a/shodan/__main__.py b/shodan/__main__.py index 4093b94..1716e73 100644 --- a/shodan/__main__.py +++ b/shodan/__main__.py @@ -37,6 +37,7 @@ import requests import time import json +from shodan.cli.validation import check_input_file_type # The file converters that are used to go from .json.gz to various other formats from shodan.cli.converter import CsvConverter, KmlConverter, GeoJsonConverter, ExcelConverter, ImagesConverter @@ -93,7 +94,7 @@ def main(): @main.command() @click.option('--fields', help='List of properties to output.', default=None) -@click.argument('input', metavar='', type=click.Path(exists=True)) +@click.argument('input', metavar='', type=click.Path(exists=True), callback=check_input_file_type) @click.argument('format', metavar='', type=click.Choice(CONVERTERS.keys())) def convert(fields, input, format): """Convert the given input data file into a different format. The following file formats are supported: diff --git a/shodan/cli/validation.py b/shodan/cli/validation.py new file mode 100644 index 0000000..da53b90 --- /dev/null +++ b/shodan/cli/validation.py @@ -0,0 +1,15 @@ +import click + + +def check_input_file_type(ctx, param, value): + """ + Click callback method used for file type input validation. + :param ctx: Python Click library Context object. + :param param: Python Click Context object params attribute. + :param value: Value passed in for a given command line parameter. + """ + idx = value.find(".") + + if idx == -1 or value[idx:] != ".json.gz": + raise click.BadParameter("Input file type must be '.json.gz'") + return value