Skip to content

Commit

Permalink
add checks for validity of ip and token, closes #11
Browse files Browse the repository at this point in the history
  • Loading branch information
rytilahti committed Jul 14, 2017
1 parent 5a0f8ff commit c31647b
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions mirobo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import ast
import sys
import json
import ipaddress
from typing import Any

if sys.version_info < (3, 4):
Expand All @@ -18,9 +19,24 @@
pass_dev = click.make_pass_decorator(mirobo.Vacuum)


def validate_ip(ctx, param, value):
try:
ipaddress.ip_address(value)
return value
except ValueError as ex:
raise click.BadParameter("Invalid IP: %s" % ex)


def validate_token(ctx, param, value):
token_len = len(value)
if token_len != 32:
raise click.BadParameter("Token length != 32 chars: %s" % token_len)
return value


@click.group(invoke_without_command=True)
@click.option('--ip', envvar="MIROBO_IP")
@click.option('--token', envvar="MIROBO_TOKEN")
@click.option('--ip', envvar="MIROBO_IP", callback=validate_ip)
@click.option('--token', envvar="MIROBO_TOKEN", callback=validate_token)
@click.option('-d', '--debug', default=False, count=True)
@click.option('--id-file', type=click.Path(dir_okay=False, writable=True),
default='/tmp/python-mirobo.seq')
Expand Down

0 comments on commit c31647b

Please sign in to comment.