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
- Argument parsers moved to corresponding commands
- Automatic parsers and command mapping

Change-Id: Id099d14702d9590729583dfd9574bd57022efac5
Related-PROD: PROD-28199
diff --git a/cfg_checker/cli/command.py b/cfg_checker/cli/command.py
index 0a9c2a2..e6d9cd9 100644
--- a/cfg_checker/cli/command.py
+++ b/cfg_checker/cli/command.py
@@ -1,19 +1,33 @@
+import pkgutil
 import sys
 import traceback
 
-from cfg_checker.common import logger_cli
+from cfg_checker.common import config, logger, logger_cli
 from cfg_checker.common.exception import CheckerException
+from cfg_checker.helpers.args_utils import MyParser
 
-# TODO: auto-create types for each command
-package_command_types = ['report']
-network_command_types = ['check', 'report']
-reclass_command_types = ['list', 'diff']
+main_pkg_name = __name__.split('.')[0]
+mods_package_name = "modules"
+mods_import_path = main_pkg_name + '.' + mods_package_name
+mods_prefix = mods_import_path + '.'
 
-commands = {
-    'packages': package_command_types,
-    'network': network_command_types,
-    'reclass': reclass_command_types
-}
+commands = {}
+parsers = {}
+helps = {}
+# Pure dynamic magic, loading all 'do_*' methods from available modules
+_m = __import__(mods_import_path, fromlist=[main_pkg_name])
+for _imp, modName, isMod in pkgutil.iter_modules(_m.__path__, mods_prefix):
+    # iterate all packages, add to dict
+    if isMod:
+        # load module
+        _p = _imp.find_module(modName).load_module(modName)
+        # create a shortname
+        mod_name = modName.split('.')[-1]
+        # A package! Create it and add commands
+        commands[mod_name] = \
+            [_n[3:] for _n in dir(_p) if _n.startswith("do_")]
+        parsers[mod_name] = getattr(_p, 'init_parser')
+        helps[mod_name] = getattr(_p, 'command_help')
 
 
 def execute_command(args, command):
@@ -34,7 +48,7 @@
         # form function name to call
         _method_name = "do_" + args.type
         _target_module = __import__(
-            "cfg_checker.modules."+command,
+            mods_prefix + command,
             fromlist=[""]
         )
         _method = getattr(_target_module, _method_name)
@@ -57,3 +71,23 @@
             ))
         ))
         return 1
+
+
+def cli_command(_title, _name):
+    my_parser = MyParser(_title)
+    parsers[_name](my_parser)
+
+    # parse arguments
+    try:
+        args = my_parser.parse_args()
+    except TypeError:
+        logger_cli.info("\n# Please, check arguments")
+        sys.exit(0)
+
+    # force use of sudo
+    config.ssh_uses_sudo = True
+
+    # Execute the command
+    result = execute_command(args, _name)
+    logger.debug(result)
+    sys.exit(result)