Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/history/commands.py
1567 views
1
# Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
import os
14
15
from awscli.compat import is_windows
16
from awscli.utils import is_a_tty
17
from awscli.utils import OutputStreamFactory
18
19
from awscli.customizations.commands import BasicCommand
20
from awscli.customizations.history.db import DatabaseConnection
21
from awscli.customizations.history.constants import HISTORY_FILENAME_ENV_VAR
22
from awscli.customizations.history.constants import DEFAULT_HISTORY_FILENAME
23
from awscli.customizations.history.db import DatabaseRecordReader
24
25
26
class HistorySubcommand(BasicCommand):
27
def __init__(self, session, db_reader=None, output_stream_factory=None):
28
super(HistorySubcommand, self).__init__(session)
29
self._db_reader = db_reader
30
self._output_stream_factory = output_stream_factory
31
if output_stream_factory is None:
32
self._output_stream_factory = OutputStreamFactory()
33
34
def _connect_to_history_db(self):
35
if self._db_reader is None:
36
connection = DatabaseConnection(self._get_history_db_filename())
37
self._db_reader = DatabaseRecordReader(connection)
38
39
def _close_history_db(self):
40
self._db_reader.close()
41
42
def _get_history_db_filename(self):
43
filename = os.environ.get(
44
HISTORY_FILENAME_ENV_VAR, DEFAULT_HISTORY_FILENAME)
45
if not os.path.exists(filename):
46
raise RuntimeError(
47
'Could not locate history. Make sure cli_history is set to '
48
'enabled in the ~/.aws/config file'
49
)
50
return filename
51
52
def _should_use_color(self, parsed_globals):
53
if parsed_globals.color == 'on':
54
return True
55
elif parsed_globals.color == 'off':
56
return False
57
return is_a_tty() and not is_windows
58
59
def _get_output_stream(self, preferred_pager=None):
60
if is_a_tty():
61
return self._output_stream_factory.get_pager_stream(
62
preferred_pager)
63
return self._output_stream_factory.get_stdout_stream()
64
65