|
22 | 22 | import sys |
23 | 23 |
|
24 | 24 | from cliff import app |
| 25 | +from cliff import command |
25 | 26 | from cliff import help |
26 | 27 |
|
27 | 28 | import openstackclient |
@@ -64,6 +65,11 @@ class OpenStackShell(app.App): |
64 | 65 | log = logging.getLogger(__name__) |
65 | 66 |
|
66 | 67 | def __init__(self): |
| 68 | + # Patch command.Command to add a default auth_required = True |
| 69 | + command.Command.auth_required = True |
| 70 | + # But not help |
| 71 | + help.HelpCommand.auth_required = False |
| 72 | + |
67 | 73 | super(OpenStackShell, self).__init__( |
68 | 74 | description=__doc__.strip(), |
69 | 75 | version=openstackclient.__version__, |
@@ -383,27 +389,27 @@ def initialize_app(self, argv): |
383 | 389 | # Set up common client session |
384 | 390 | self.restapi = restapi.RESTApi() |
385 | 391 |
|
386 | | - # If the user is not asking for help, make sure they |
387 | | - # have given us auth. |
388 | | - cmd_name = None |
389 | | - if argv: |
390 | | - cmd_info = self.command_manager.find_command(argv) |
391 | | - cmd_factory, cmd_name, sub_argv = cmd_info |
392 | | - if self.interactive_mode or cmd_name != 'help': |
393 | | - self.authenticate_user() |
394 | | - self.restapi.set_auth(self.client_manager.identity.auth_token) |
395 | | - |
396 | 392 | def prepare_to_run_command(self, cmd): |
397 | 393 | """Set up auth and API versions""" |
398 | 394 | self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__) |
399 | | - self.log.debug("api: %s" % cmd.api if hasattr(cmd, 'api') else None) |
| 395 | + |
| 396 | + if cmd.auth_required: |
| 397 | + self.authenticate_user() |
| 398 | + self.restapi.set_auth(self.client_manager.identity.auth_token) |
400 | 399 | return |
401 | 400 |
|
402 | 401 | def clean_up(self, cmd, result, err): |
403 | 402 | self.log.debug('clean_up %s', cmd.__class__.__name__) |
404 | 403 | if err: |
405 | 404 | self.log.debug('got an error: %s', err) |
406 | 405 |
|
| 406 | + def interact(self): |
| 407 | + # NOTE(dtroyer): Maintain the old behaviour for interactive use as |
| 408 | + # this path does not call prepare_to_run_command() |
| 409 | + self.authenticate_user() |
| 410 | + self.restapi.set_auth(self.client_manager.identity.auth_token) |
| 411 | + super(OpenStackShell, self).interact() |
| 412 | + |
407 | 413 |
|
408 | 414 | def main(argv=sys.argv[1:]): |
409 | 415 | try: |
|
0 commit comments