Unified command execution and unit tests
- All arguments inits moved to own clases
- Added unified way to execute commands
- Unit test structure and very basic tests
- Command line script to test coverage
Change-Id: I10bc973776595779b563b84548d46367bcd0886f
Related-PROD: PROD-28199
diff --git a/cfg_checker/cli/command.py b/cfg_checker/cli/command.py
new file mode 100644
index 0000000..0a9c2a2
--- /dev/null
+++ b/cfg_checker/cli/command.py
@@ -0,0 +1,59 @@
+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