Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/history/list.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 json
14
import datetime
15
16
from awscli.compat import default_pager
17
from awscli.customizations.history.commands import HistorySubcommand
18
19
20
class ListCommand(HistorySubcommand):
21
NAME = 'list'
22
DESCRIPTION = (
23
'Shows a list of previously run commands and their command_ids. '
24
'Each row shows only a bare minimum of details including the '
25
'command_id, date, arguments and return code. You can use the '
26
'``history show`` with the command_id to see more details about '
27
'a particular entry.'
28
)
29
_COL_WIDTHS = {
30
'id_a': 38,
31
'timestamp': 24,
32
'args': 50,
33
'rc': 0
34
}
35
36
def _run_main(self, parsed_args, parsed_globals):
37
self._connect_to_history_db()
38
try:
39
raw_records = self._db_reader.iter_all_records()
40
records = RecordAdapter(raw_records)
41
if not records.has_next():
42
raise RuntimeError(
43
'No commands were found in your history. Make sure you have '
44
'enabled history mode by adding "cli_history = enabled" '
45
'to the config file.')
46
47
preferred_pager = self._get_preferred_pager()
48
with self._get_output_stream(preferred_pager) as output_stream:
49
formatter = TextFormatter(self._COL_WIDTHS, output_stream)
50
formatter(records)
51
finally:
52
self._close_history_db()
53
return 0
54
55
def _get_preferred_pager(self):
56
preferred_pager = default_pager
57
if preferred_pager.startswith('less'):
58
preferred_pager = 'less -SR'
59
return preferred_pager
60
61
62
class RecordAdapter(object):
63
"""This class is just to read one ahead to make sure there are records
64
65
If there are no records we can just exit early.
66
"""
67
def __init__(self, records):
68
self._records = records
69
self._next = None
70
self._advance()
71
72
def has_next(self):
73
return self._next is not None
74
75
def _advance(self):
76
try:
77
self._next = next(self._records)
78
except StopIteration:
79
self._next = None
80
81
def __iter__(self):
82
while self.has_next():
83
yield self._next
84
self._advance()
85
86
87
class TextFormatter(object):
88
def __init__(self, col_widths, output_stream):
89
self._col_widths = col_widths
90
self._output_stream = output_stream
91
92
def _format_time(self, timestamp):
93
command_time = datetime.datetime.fromtimestamp(timestamp / 1000)
94
formatted = datetime.datetime.strftime(
95
command_time, '%Y-%m-%d %I:%M:%S %p')
96
return formatted
97
98
def _format_args(self, args, arg_width):
99
json_value = json.loads(args)
100
formatted = ' '.join(json_value[:2])
101
if len(formatted) >= arg_width:
102
formatted = '%s...' % formatted[:arg_width-4]
103
return formatted
104
105
def _format_record(self, record):
106
fmt_string = "{0:<%s}{1:<%s}{2:<%s}{3}\n" % (
107
self._col_widths['id_a'],
108
self._col_widths['timestamp'],
109
self._col_widths['args']
110
)
111
record_line = fmt_string.format(
112
record['id_a'],
113
self._format_time(record['timestamp']),
114
self._format_args(record['args'], self._col_widths['args']),
115
record['rc']
116
)
117
return record_line
118
119
def __call__(self, record_adapter):
120
for record in record_adapter:
121
formatted_record = self._format_record(record)
122
self._output_stream.write(formatted_record.encode('utf-8'))
123
124