Repository: RedHatInsights/insights-core

Issue:
Add version command/flag to the Insights CLI 
Add `version` command and `--version` flag/arg to print version info from `insights.get_nvr()`. 


Discussion:

Resolving patch:
diff --git a/insights/command_parser.py b/insights/command_parser.py
--- a/insights/command_parser.py
+++ b/insights/command_parser.py
@@ -19,6 +19,7 @@
   info        View info and docs for Insights Core components.
   ocpshell         Interactive evaluation of archives, directories, or individual yaml files.
   run         Run insights-core against host or an archive.
+  version     Show Insights Core version information and exit.
 """
 
 
@@ -36,6 +37,9 @@ def __init__(self):
             description="Insights Core command line execution",
             usage=USAGE)
         parser.add_argument('command', help='Insights Core command to run')
+        parser.add_argument('--version', action='store_true', help='show Insights Core version information and exit')
+        if self._parse_version_arg():
+            self.version()
         args = parser.parse_args(sys.argv[1:2])
         if not hasattr(self, args.command):
             print('Unrecognized command')
@@ -46,6 +50,12 @@ def __init__(self):
         # Use dispatch pattern to execute command
         getattr(self, args.command)()
 
+    def _parse_version_arg(self):
+        """
+        Manually check for version argument/flag in cases when command is not provided.
+        """
+        return '--version' in sys.argv[1:3]
+
     def cat(self):
         from .tools.cat import main as cat_main
         cat_main()
@@ -72,6 +82,14 @@ def run(self):
             sys.path.insert(0, "")
         run(print_summary=True)
 
+    def version(self):
+        """
+        Print version information (NVR) and exit.
+        """
+        from insights import get_nvr
+        print(get_nvr())
+        sys.exit()
+
 
 def fix_arg_dashes():
 


Test patch:
diff --git a/insights/tests/test_command_parser.py b/insights/tests/test_command_parser.py
new file mode 100644
--- /dev/null
+++ b/insights/tests/test_command_parser.py
@@ -0,0 +1,37 @@
+import sys
+from insights import get_nvr
+from insights.command_parser import InsightsCli
+import pytest
+from mock import patch
+
+VERSION_OUT = get_nvr() + '\n'
+
+
+def test_insights_cli_version(capsys):
+    test_args_list = [
+        ["insights", "version"],
+        ["insights", "--version"],
+        ["insights", "cat", "--version"],
+        ["insights", "collect", "--version"],
+        ["insights", "inspect", "--version"],
+        ["insights", "info", "--version"],
+        ["insights", "ocpshell", "--version"],
+        ["insights", "run", "--version"]
+    ]
+    for args in test_args_list:
+        with patch.object(sys, 'argv', args):
+            with pytest.raises(SystemExit):
+                InsightsCli()
+            out, err = capsys.readouterr()
+            assert err == ''
+            assert out == VERSION_OUT
+
+
+def test_insights_cli_version_bad_arg(capsys):
+    test_args = ["insights", "-v"]
+    with patch.object(sys, 'argv', test_args):
+        with pytest.raises(SystemExit):
+            InsightsCli()
+        out, err = capsys.readouterr()
+        assert len(err) > 0
+        assert out == ''