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