| import sys |
| import traceback |
| |
| from cfg_checker.common import logger_cli |
| from cfg_checker.common.exception import CheckerException |
| |
| # TODO: auto-create types for each command |
| package_command_types = ['report'] |
| network_command_types = ['check', 'report'] |
| reclass_command_types = ['list', 'diff'] |
| |
| commands = { |
| 'packages': package_command_types, |
| 'network': network_command_types, |
| 'reclass': reclass_command_types |
| } |
| |
| |
| def execute_command(args, command): |
| # Validate the commands |
| # check command |
| if command not in commands: |
| logger_cli.info("\n# Please, type a command listed above") |
| return 0 |
| elif args.type not in commands[command]: |
| # check type |
| logger_cli.info( |
| "\n# Please, select '{}' command type listed above".format( |
| command |
| ) |
| ) |
| return 0 |
| else: |
| # form function name to call |
| _method_name = "do_" + args.type |
| _target_module = __import__( |
| "cfg_checker.modules."+command, |
| fromlist=[""] |
| ) |
| _method = getattr(_target_module, _method_name) |
| |
| # Execute the command |
| try: |
| _method(args) |
| return 0 |
| except CheckerException as e: |
| logger_cli.error("\nERROR: {}".format( |
| e.message |
| )) |
| |
| exc_type, exc_value, exc_traceback = sys.exc_info() |
| logger_cli.debug("\n{}".format( |
| "".join(traceback.format_exception( |
| exc_type, |
| exc_value, |
| exc_traceback |
| )) |
| )) |
| return 1 |