|
14 | 14 |
|
15 | 15 | import click |
16 | 16 |
|
| 17 | +import requests |
17 | 18 | import SoftLayer |
18 | 19 | from SoftLayer.CLI import environment |
19 | 20 | from SoftLayer.CLI import exceptions |
|
31 | 32 | 3: logging.DEBUG |
32 | 33 | } |
33 | 34 |
|
| 35 | +PROG_NAME = "slcli (SoftLayer Command-line)" |
34 | 36 | VALID_FORMATS = ['table', 'raw', 'json', 'jsonraw'] |
35 | 37 | DEFAULT_FORMAT = 'raw' |
36 | 38 | if sys.stdout.isatty(): |
@@ -69,6 +71,28 @@ def get_command(self, ctx, name): |
69 | 71 | return module |
70 | 72 |
|
71 | 73 |
|
| 74 | +def get_latest_version(): |
| 75 | + """Gets the latest version of the Softlayer library.""" |
| 76 | + try: |
| 77 | + result = requests.get('https://pypi.org/pypi/SoftLayer/json') |
| 78 | + json_result = result.json() |
| 79 | + latest = 'v{}'.format(json_result['info']['version']) |
| 80 | + except Exception: |
| 81 | + latest = "Unable to get version from pypi." |
| 82 | + return latest |
| 83 | + |
| 84 | + |
| 85 | +def get_version_message(ctx, param, value): |
| 86 | + """Gets current and latest release versions message.""" |
| 87 | + if not value or ctx.resilient_parsing: |
| 88 | + return |
| 89 | + current = SoftLayer.consts.VERSION |
| 90 | + latest = get_latest_version() |
| 91 | + click.secho("Current: {prog} {current}\nLatest: {prog} {latest}".format( |
| 92 | + prog=PROG_NAME, current=current, latest=latest)) |
| 93 | + ctx.exit() |
| 94 | + |
| 95 | + |
72 | 96 | @click.group(help="SoftLayer Command-line Client", |
73 | 97 | epilog="""To use most commands your SoftLayer |
74 | 98 | 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): |
103 | 127 | is_flag=True, |
104 | 128 | required=False, |
105 | 129 | help="Use demo data instead of actually making API calls") |
106 | | -@click.version_option(prog_name="slcli (SoftLayer Command-line)") |
| 130 | +@click.option('--version', is_flag=True, expose_value=False, is_eager=True, callback=get_version_message, |
| 131 | + help="Show version information.") |
107 | 132 | @environment.pass_env |
108 | 133 | def cli(env, |
109 | 134 | format='table', |
|
0 commit comments