-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy path__init__.py
107 lines (87 loc) · 4.02 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright 2017 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.
import os
import sys
import logging
from botocore.history import get_global_history_recorder
from botocore.exceptions import ProfileNotFound
from awscli.compat import sqlite3
from awscli.customizations.commands import BasicCommand
from awscli.customizations.history.constants import HISTORY_FILENAME_ENV_VAR
from awscli.customizations.history.constants import DEFAULT_HISTORY_FILENAME
from awscli.customizations.history.db import DatabaseConnection
from awscli.customizations.history.db import DatabaseRecordWriter
from awscli.customizations.history.db import RecordBuilder
from awscli.customizations.history.db import DatabaseHistoryHandler
from awscli.customizations.history.show import ShowCommand
from awscli.customizations.history.list import ListCommand
LOG = logging.getLogger(__name__)
HISTORY_RECORDER = get_global_history_recorder()
def register_history_mode(event_handlers):
event_handlers.register(
'session-initialized', attach_history_handler)
def register_history_commands(event_handlers):
event_handlers.register(
"building-command-table.main", add_history_commands)
def attach_history_handler(session, parsed_args, **kwargs):
if _should_enable_cli_history(session, parsed_args):
LOG.debug('Enabling CLI history')
history_filename = os.environ.get(
HISTORY_FILENAME_ENV_VAR, DEFAULT_HISTORY_FILENAME)
if not os.path.isdir(os.path.dirname(history_filename)):
os.makedirs(os.path.dirname(history_filename))
connection = DatabaseConnection(history_filename)
writer = DatabaseRecordWriter(connection)
record_builder = RecordBuilder()
db_handler = DatabaseHistoryHandler(writer, record_builder)
HISTORY_RECORDER.add_handler(db_handler)
HISTORY_RECORDER.enable()
def _should_enable_cli_history(session, parsed_args):
if parsed_args.command == 'history':
return False
try:
scoped_config = session.get_scoped_config()
except ProfileNotFound:
# If the profile does not exist, cli history is definitely not
# enabled, but don't let the error get propagated as commands down
# the road may handle this such as the configure set command with
# a --profile flag set.
return False
has_history_enabled = scoped_config.get('cli_history') == 'enabled'
if has_history_enabled and sqlite3 is None:
if has_history_enabled:
sys.stderr.write(
'cli_history is enabled but sqlite3 is unavailable. '
'Unable to collect CLI history.\n'
)
return False
return has_history_enabled
def add_history_commands(command_table, session, **kwargs):
command_table['history'] = HistoryCommand(session)
class HistoryCommand(BasicCommand):
NAME = 'history'
DESCRIPTION = (
'Commands to interact with the history of AWS CLI commands ran '
'over time. To record the history of AWS CLI commands set '
'``cli_history`` to ``enabled`` in the ``~/.aws/config`` file. '
'This can be done by running:\n\n'
'``$ aws configure set cli_history enabled``'
)
SUBCOMMANDS = [
{'name': 'show', 'command_class': ShowCommand},
{'name': 'list', 'command_class': ListCommand}
]
def _run_main(self, parsed_args, parsed_globals):
if parsed_args.subcommand is None:
raise ValueError("usage: aws [options] <command> <subcommand> "
"[parameters]\naws: error: too few arguments")