Skip to content

Commit d32185c

Browse files
author
Dean Troyer
committed
Add 'command list' command
* Add method to CommandManager to retrieve command names by group * Add ListCommands To list command groups loaded by cliff Change-Id: I37fe2471aa2fafa8aa223159452d52b1981021d6
1 parent 5b6c24f commit d32185c

5 files changed

Lines changed: 81 additions & 0 deletions

File tree

openstackclient/common/commandmanager.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""Modify cliff.CommandManager"""
1717

1818
import logging
19+
import pkg_resources
1920

2021
import cliff.commandmanager
2122

@@ -46,3 +47,17 @@ def add_command_group(self, group=None):
4647
def get_command_groups(self):
4748
"""Returns a list of the loaded command groups"""
4849
return self.group_list
50+
51+
def get_command_names(self, group=None):
52+
"""Returns a list of commands loaded for the specified group"""
53+
group_list = []
54+
if group is not None:
55+
for ep in pkg_resources.iter_entry_points(group):
56+
cmd_name = (
57+
ep.name.replace('_', ' ')
58+
if self.convert_underscores
59+
else ep.name
60+
)
61+
group_list.append(cmd_name)
62+
return group_list
63+
return self.commands.keys()

openstackclient/common/module.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,25 @@
1919
import six
2020
import sys
2121

22+
from cliff import lister
2223
from cliff import show
2324

2425

26+
class ListCommand(lister.Lister):
27+
"""List recognized commands by group"""
28+
29+
auth_required = False
30+
log = logging.getLogger(__name__ + '.ListCommand')
31+
32+
def take_action(self, parsed_args):
33+
self.log.debug('take_action(%s)', parsed_args)
34+
cm = self.app.command_manager
35+
groups = cm.get_command_groups()
36+
37+
columns = ('Command Group', 'Commands')
38+
return (columns, ((c, cm.get_command_names(group=c)) for c in groups))
39+
40+
2541
class ListModule(show.ShowOne):
2642
"""List module versions"""
2743

openstackclient/tests/common/test_commandmanager.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,20 @@ def test_get_command_groups(self):
8686

8787
gl = mgr.get_command_groups()
8888
self.assertEqual(['test', 'greek'], gl)
89+
90+
def test_get_command_names(self):
91+
mock_cmd_one = mock.Mock()
92+
mock_cmd_one.name = 'one'
93+
mock_cmd_two = mock.Mock()
94+
mock_cmd_two.name = 'cmd two'
95+
mock_pkg_resources = mock.Mock(
96+
return_value=[mock_cmd_one, mock_cmd_two],
97+
)
98+
with mock.patch(
99+
'pkg_resources.iter_entry_points',
100+
mock_pkg_resources,
101+
) as iter_entry_points:
102+
mgr = commandmanager.CommandManager('test')
103+
assert iter_entry_points.called_once_with('test')
104+
cmds = mgr.get_command_names('test')
105+
self.assertEqual(['one', 'cmd two'], cmds)

openstackclient/tests/common/test_module.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,38 @@
4242
}
4343

4444

45+
class TestCommandList(utils.TestCommand):
46+
47+
def setUp(self):
48+
super(TestCommandList, self).setUp()
49+
50+
self.app.command_manager = mock.Mock()
51+
self.app.command_manager.get_command_groups.return_value = ['test']
52+
self.app.command_manager.get_command_names.return_value = [
53+
'one',
54+
'cmd two',
55+
]
56+
57+
# Get the command object to test
58+
self.cmd = osc_module.ListCommand(self.app, None)
59+
60+
def test_command_list_no_options(self):
61+
arglist = []
62+
verifylist = []
63+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
64+
65+
# DisplayCommandBase.take_action() returns two tuples
66+
columns, data = self.cmd.take_action(parsed_args)
67+
68+
collist = ('Command Group', 'Commands')
69+
self.assertEqual(collist, columns)
70+
datalist = ((
71+
'test',
72+
['one', 'cmd two'],
73+
), )
74+
self.assertEqual(datalist, tuple(data))
75+
76+
4577
@mock.patch.dict(
4678
'openstackclient.common.module.sys.modules',
4779
values=MODULES,

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ console_scripts =
2828
openstack = openstackclient.shell:main
2929

3030
openstack.cli =
31+
command_list = openstackclient.common.module:ListCommand
3132
module_list = openstackclient.common.module:ListModule
3233

3334
openstack.cli.base =

0 commit comments

Comments
 (0)