Path: blob/develop/awscli/customizations/history/commands.py
1567 views
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.12import os1314from awscli.compat import is_windows15from awscli.utils import is_a_tty16from awscli.utils import OutputStreamFactory1718from awscli.customizations.commands import BasicCommand19from awscli.customizations.history.db import DatabaseConnection20from awscli.customizations.history.constants import HISTORY_FILENAME_ENV_VAR21from awscli.customizations.history.constants import DEFAULT_HISTORY_FILENAME22from awscli.customizations.history.db import DatabaseRecordReader232425class HistorySubcommand(BasicCommand):26def __init__(self, session, db_reader=None, output_stream_factory=None):27super(HistorySubcommand, self).__init__(session)28self._db_reader = db_reader29self._output_stream_factory = output_stream_factory30if output_stream_factory is None:31self._output_stream_factory = OutputStreamFactory()3233def _connect_to_history_db(self):34if self._db_reader is None:35connection = DatabaseConnection(self._get_history_db_filename())36self._db_reader = DatabaseRecordReader(connection)3738def _close_history_db(self):39self._db_reader.close()4041def _get_history_db_filename(self):42filename = os.environ.get(43HISTORY_FILENAME_ENV_VAR, DEFAULT_HISTORY_FILENAME)44if not os.path.exists(filename):45raise RuntimeError(46'Could not locate history. Make sure cli_history is set to '47'enabled in the ~/.aws/config file'48)49return filename5051def _should_use_color(self, parsed_globals):52if parsed_globals.color == 'on':53return True54elif parsed_globals.color == 'off':55return False56return is_a_tty() and not is_windows5758def _get_output_stream(self, preferred_pager=None):59if is_a_tty():60return self._output_stream_factory.get_pager_stream(61preferred_pager)62return self._output_stream_factory.get_stdout_stream()636465