Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ doc/source/tutorial/services.rst
macpkg/dist/aws
macpkg/build
dist/
.venv

# autocomplete index
awscli/data/ac.index
Expand Down
4 changes: 2 additions & 2 deletions awscli/autocomplete/local/indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ModelIndexer:
'ddb': 'High level DynamoDB commands',
}

_NON_SERVICE_COMMANDS = ['configure', 'history', 'cli-dev', 'login', 'logout']
_NON_SERVICE_COMMANDS = ['configure', 'history', 'cli-dev', 'login', 'logout', 'console']

_CREATE_CMD_TABLE = """\
CREATE TABLE IF NOT EXISTS command_table (
Expand Down Expand Up @@ -111,7 +111,7 @@ def _get_service_full_name(self, name, help_command_table):
if name in self._HIGH_LEVEL_SERVICE_FULL_NAMES:
return self._HIGH_LEVEL_SERVICE_FULL_NAMES[name]
service = help_command_table.get(name)
if service:
if service and hasattr(service, 'service_model'):
return service.service_model.metadata['serviceFullName']

def _generate_command_index(
Expand Down
18 changes: 18 additions & 0 deletions awscli/customizations/console/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

from awscli.customizations.console.open import OpenConsoleCommand


def register_console_commands(cli):
cli.register('building-command-table.main', OpenConsoleCommand.add_command)
32 changes: 32 additions & 0 deletions awscli/customizations/console/open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import sys
import webbrowser

from awscli.customizations.commands import BasicCommand


class OpenConsoleCommand(BasicCommand):
NAME = 'console'
DESCRIPTION = (
'Open the AWS Management Console in the default browser.'
)
ARG_TABLE = [
]

def __init__(
self,
session,
token_loader=None,
config_file_writer=None,
):
super().__init__(session)

def _run_main(self, parsed_args, parsed_globals):
console_url = 'https://console.aws.amazon.com/'
try:
webbrowser.open_new_tab(console_url)
except webbrowser.Error:
print(f'Failed to open browser. Please visit: {console_url}', file=sys.stderr)
return 1
return 0
2 changes: 2 additions & 0 deletions awscli/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
from awscli.customizations.sessendemail import register_ses_send_email
from awscli.customizations.sessionmanager import register_ssm_session
from awscli.customizations.sso import register_sso_commands
from awscli.customizations.console import register_console_commands
from awscli.customizations.streamingoutputarg import add_streaming_output_arg
from awscli.customizations.timestampformat import register_timestamp_format
from awscli.customizations.toplevelbool import register_bool_params
Expand Down Expand Up @@ -239,3 +240,4 @@ def awscli_initialize(event_handlers):
register_quicksight_asset_bundle_customizations(event_handlers)
register_ec2_instance_connect_commands(event_handlers)
register_login_cmds(event_handlers)
register_console_commands(event_handlers)
3 changes: 3 additions & 0 deletions tests/unit/customizations/console/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

52 changes: 52 additions & 0 deletions tests/unit/customizations/console/test_open.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import unittest
import webbrowser
from io import StringIO

from awscli.testutils import mock

from awscli.customizations.console.open import OpenConsoleCommand


class TestOpenConsoleCommand(unittest.TestCase):
def setUp(self):
self.session = mock.Mock()
self.command = OpenConsoleCommand(self.session)
self.parsed_args = mock.Mock()
self.parsed_globals = mock.Mock()

@mock.patch('awscli.customizations.console.open.webbrowser.open_new_tab')
def test_opens_console_url_successfully(self, mock_open_browser):
"""Test that the command opens the AWS console URL in browser."""
mock_open_browser.return_value = True

result = self.command._run_main(self.parsed_args, self.parsed_globals)

mock_open_browser.assert_called_once_with(
'https://console.aws.amazon.com/')
self.assertEqual(result, 0)

@mock.patch('awscli.customizations.console.open.webbrowser.open_new_tab')
@mock.patch('sys.stderr', new_callable=StringIO)
def test_handles_browser_error(self, mock_stderr, mock_open_browser):
"""Test that the command handles browser opening errors gracefully."""
mock_open_browser.side_effect = webbrowser.Error(
'Browser not available')

result = self.command._run_main(self.parsed_args, self.parsed_globals)

mock_open_browser.assert_called_once_with(
'https://console.aws.amazon.com/')
self.assertEqual(result, 1)
error_output = mock_stderr.getvalue()
self.assertIn('Failed to open browser', error_output)
self.assertIn('https://console.aws.amazon.com/', error_output)

def test_command_name(self):
"""Test that the command has the correct name."""
self.assertEqual(self.command.NAME, 'console')

def test_command_description(self):
"""Test that the command has a description."""
self.assertIn('AWS Management Console', self.command.DESCRIPTION)
Loading