Repository: softlayer/softlayer-python
Pull request #1226 (2020-02-11 20:41:16)

#1221 Added version checker


Patch:
diff --git a/SoftLayer/CLI/core.py b/SoftLayer/CLI/core.py
index 3a552df7..fe86f714 100644
--- a/SoftLayer/CLI/core.py
+++ b/SoftLayer/CLI/core.py
@@ -14,6 +14,7 @@
 
 import click
 
+import requests
 import SoftLayer
 from SoftLayer.CLI import environment
 from SoftLayer.CLI import exceptions
@@ -31,6 +32,7 @@
     3: logging.DEBUG
 }
 
+PROG_NAME = "slcli (SoftLayer Command-line)"
 VALID_FORMATS = ['table', 'raw', 'json', 'jsonraw']
 DEFAULT_FORMAT = 'raw'
 if sys.stdout.isatty():
@@ -69,6 +71,28 @@ def get_command(self, ctx, name):
             return module
 
 
+def get_latest_version():
+    """Gets the latest version of the Softlayer library."""
+    try:
+        result = requests.get('https://pypi.org/pypi/SoftLayer/json')
+        json_result = result.json()
+        latest = 'v{}'.format(json_result['info']['version'])
+    except Exception:
+        latest = "Unable to get version from pypi."
+    return latest
+
+
+def get_version_message(ctx, param, value):
+    """Gets current and latest release versions message."""
+    if not value or ctx.resilient_parsing:
+        return
+    current = SoftLayer.consts.VERSION
+    latest = get_latest_version()
+    click.secho("Current: {prog} {current}\nLatest:  {prog} {latest}".format(
+        prog=PROG_NAME, current=current, latest=latest))
+    ctx.exit()
+
+
 @click.group(help="SoftLayer Command-line Client",
              epilog="""To use most commands your SoftLayer
 username and api_key need to be configured. The easiest way to do that is to
@@ -103,7 +127,8 @@ def get_command(self, ctx, name):
               is_flag=True,
               required=False,
               help="Use demo data instead of actually making API calls")
-@click.version_option(prog_name="slcli (SoftLayer Command-line)")
+@click.option('--version', is_flag=True, expose_value=False, is_eager=True, callback=get_version_message,
+              help="Show version information.")
 @environment.pass_env
 def cli(env,
         format='table',


Test patch:
diff --git a/tests/CLI/core_tests.py b/tests/CLI/core_tests.py
index 321f78b1..f230a351 100644
--- a/tests/CLI/core_tests.py
+++ b/tests/CLI/core_tests.py
@@ -10,6 +10,7 @@
 import click
 import mock
 
+from requests.models import Response
 import SoftLayer
 from SoftLayer.CLI import core
 from SoftLayer.CLI import environment
@@ -52,6 +53,28 @@ def test_diagnostics(self):
         self.assertIn('"python_version"', result.output)
         self.assertIn('"library_location"', result.output)
 
+    @mock.patch('requests.get')
+    def test_get_latest_version(self, request_get):
+        response = Response()
+        response.status_code = 200
+        response.json = mock.MagicMock(return_value={"info": {"version": "1.1.1"}})
+        request_get.return_value = response
+        version = core.get_latest_version()
+        self.assertIn('1.1.1', version)
+
+    @mock.patch('requests.get')
+    def test_unable_get_latest_version(self, request_get):
+        request_get.side_effect = Exception
+        version = core.get_latest_version()
+        self.assertIn('Unable', version)
+
+    @mock.patch('SoftLayer.CLI.core.get_latest_version')
+    def test_get_version_message(self, get_latest_version_mock):
+        get_latest_version_mock.return_value = '1.1.1'
+        env = environment.Environment()
+        result = self.run_command(['--version'], env=env)
+        self.assert_no_fail(result)
+
 
 class CoreMainTests(testing.TestCase):