Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion shodan/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -93,7 +94,7 @@ def main():

@main.command()
@click.option('--fields', help='List of properties to output.', default=None)
@click.argument('input', metavar='<input file>', type=click.Path(exists=True))
@click.argument('input', metavar='<input file>', type=click.Path(exists=True), callback=check_input_file_type)
@click.argument('format', metavar='<output format>', 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:
Expand Down
15 changes: 15 additions & 0 deletions shodan/cli/validation.py
Original file line number Diff line number Diff line change
@@ -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