78 lines
2.9 KiB
Python
78 lines
2.9 KiB
Python
import os
|
|
import shutil
|
|
|
|
import click
|
|
from pathlib import Path
|
|
import logging
|
|
|
|
from iottb.commands.sniff import sniff
|
|
from iottb.commands.developer import set_key_in_table_to, rm_cfg, rm_dbs, show_cfg, show_everything
|
|
|
|
##################################################
|
|
# Import package modules
|
|
#################################################
|
|
from iottb.utils.logger_config import setup_logging
|
|
from iottb import definitions
|
|
from iottb.models.iottb_config import IottbConfig
|
|
from iottb.commands.testbed import init_db
|
|
from iottb.commands.add_device import add_device
|
|
|
|
############################################################################
|
|
# Module shortcuts for global definitions
|
|
###########################################################################
|
|
APP_NAME = definitions.APP_NAME
|
|
DB_NAME = definitions.DB_NAME
|
|
CFG_FILE_PATH = definitions.CFG_FILE_PATH
|
|
# These are (possibly) redundant when defined in definitions.py
|
|
# keeping them here until refactored and tested
|
|
MAX_VERBOSITY = definitions.MAX_VERBOSITY
|
|
|
|
# Logger stuff
|
|
loglevel = definitions.LOGLEVEL
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@click.group(context_settings=dict(auto_envvar_prefix='IOTTB', show_default=True))
|
|
@click.option('-v', '--verbosity', count=True, type=click.IntRange(0, 3), default=0, is_eager=True,
|
|
help='Set verbosity')
|
|
@click.option('-d', '--debug', is_flag=True, default=False, is_eager=True,
|
|
help='Enable debug mode')
|
|
@click.option('--dry-run', is_flag=True, default=True, is_eager=True)
|
|
@click.option('--cfg-file', type=click.Path(),
|
|
default=Path(click.get_app_dir(APP_NAME)).joinpath('iottb.cfg'),
|
|
envvar='IOTTB_CONF_HOME', help='Path to iottb config file')
|
|
@click.pass_context
|
|
def cli(ctx, verbosity, debug, dry_run, cfg_file):
|
|
setup_logging(verbosity, debug) # Setup logging based on the loaded configuration and other options
|
|
ctx.ensure_object(dict) # Make sure context is ready for use
|
|
logger.info("Starting execution.")
|
|
ctx.obj['CONFIG'] = IottbConfig(cfg_file) # Load configuration directly
|
|
ctx.meta['FULL_PATH_CONFIG_FILE'] = str(cfg_file)
|
|
ctx.meta['DRY_RUN'] = dry_run
|
|
logger.debug(f'Verbosity: {verbosity}')
|
|
ctx.obj['VERBOSITY'] = verbosity
|
|
logger.debug(f'Debug: {debug}')
|
|
ctx.obj['DEBUG'] = debug
|
|
|
|
|
|
##################################################################################
|
|
# Add all subcommands to group here
|
|
#################################################################################
|
|
# TODO: Is there a way to do this without pylint freaking out?
|
|
# noinspection PyTypeChecker
|
|
cli.add_command(init_db)
|
|
cli.add_command(rm_cfg)
|
|
cli.add_command(set_key_in_table_to)
|
|
cli.add_command(rm_dbs)
|
|
# noinspection PyTypeChecker
|
|
cli.add_command(add_device)
|
|
cli.add_command(show_cfg)
|
|
cli.add_command(sniff)
|
|
cli.add_command(show_everything)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
cli()
|
|
for log in Path.cwd().iterdir():
|
|
log.chmod(0o777)
|