Skip to content

Commit

Permalink
[misc] Add 'az version' (#11680)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiasli authored Jan 3, 2020
1 parent 585eb06 commit 4d684d2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/azure-cli-core/azure/cli/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ def _get_version_string(name, version_dict):
return version_string, updates_available


def get_az_version_json():
from azure.cli.core.extension import get_extensions
versions = {'extensions': {}}

for dist in get_installed_cli_distributions():
versions[dist.key] = dist.version
extensions = get_extensions()
if extensions:
for ext in extensions:
versions['extensions'][ext.name] = ext.version or 'Unknown'
return versions


def get_json_object(json_string):
""" Loads a JSON string as an object and converts all keys to snake case """

Expand Down
4 changes: 4 additions & 0 deletions src/azure-cli/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ Release History
* vm monitor metrics tail/list-definitions: support query metric and list definitions for a vm.
* Add new reapply command action for az vm

**Misc.**

* Add preview command `az version show` to show the versions of Azure CLI modules and extensions in JSON format by default or format configured by --output

**Storage**

* `az storage account create`: Remove preview flag for --enable-hierarchical-namespace parameter
Expand Down
5 changes: 5 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,3 +1307,8 @@
type: group
short-summary: Manage resource tags.
"""

helps['version'] = """
type: command
short-summary: Show the versions of Azure CLI modules and extensions in JSON format by default or format configured by --output
"""
3 changes: 3 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,6 @@ def load_command_table(self, _):

with self.command_group('') as g:
g.custom_command('rest', 'rest_call')

with self.command_group('') as g:
g.custom_command('version', 'show_version', is_preview=True)
6 changes: 6 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -2025,6 +2025,12 @@ def rest_call(cmd, method, uri, headers=None, uri_parameters=None,
print(r.text)


def show_version(cmd):
from azure.cli.core.util import get_az_version_json
versions = get_az_version_json()
return versions


class _ResourceUtils(object): # pylint: disable=too-many-instance-attributes
def __init__(self, cli_ctx,
resource_group_name=None, resource_provider_namespace=None,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import unittest
from typing import Dict

from azure.cli.testsdk import ScenarioTest


class VersionTest(ScenarioTest):

def test_version(self):
output = self.cmd('az version').get_output_in_json()
self.assertIn('azure-cli', output)
self.assertIn('azure-cli-command-modules-nspkg', output)
self.assertIn('azure-cli-dev-tools', output)
self.assertIn('azure-cli-nspkg', output)
self.assertIn('azure-cli-telemetry', output)
self.assertIn('azure-cli-testsdk', output)
self.assertIn('extensions', output)
self.assertIsInstance(output['extensions'], Dict)


if __name__ == '__main__':
unittest.main()

0 comments on commit 4d684d2

Please sign in to comment.