Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/cliinputjson.py
1566 views
1
# Copyright 2014 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
15
from awscli.paramfile import get_paramfile, LOCAL_PREFIX_MAP
16
from awscli.argprocess import ParamError
17
from awscli.customizations.arguments import OverrideRequiredArgsArgument
18
19
20
def register_cli_input_json(cli):
21
cli.register('building-argument-table', add_cli_input_json)
22
23
24
def add_cli_input_json(session, argument_table, **kwargs):
25
# This argument cannot support operations with streaming output which
26
# is designated by the argument name `outfile`.
27
if 'outfile' not in argument_table:
28
cli_input_json_argument = CliInputJSONArgument(session)
29
cli_input_json_argument.add_to_arg_table(argument_table)
30
31
32
class CliInputJSONArgument(OverrideRequiredArgsArgument):
33
"""This argument inputs a JSON string as the entire input for a command.
34
35
Ideally, the value to this argument should be a filled out JSON file
36
generated by ``--generate-cli-skeleton``. The items in the JSON string
37
will not clobber other arguments entered into the command line.
38
"""
39
ARG_DATA = {
40
'name': 'cli-input-json',
41
'help_text': 'Performs service operation based on the JSON string '
42
'provided. The JSON string follows the format provided '
43
'by ``--generate-cli-skeleton``. If other arguments are '
44
'provided on the command line, the CLI values will override '
45
'the JSON-provided values. It is not possible to pass '
46
'arbitrary binary values using a JSON-provided value as '
47
'the string will be taken literally.'
48
}
49
50
def __init__(self, session):
51
super(CliInputJSONArgument, self).__init__(session)
52
53
def _register_argument_action(self):
54
self._session.register(
55
'calling-command.*', self.add_to_call_parameters)
56
super(CliInputJSONArgument, self)._register_argument_action()
57
58
def add_to_call_parameters(self, call_parameters, parsed_args,
59
parsed_globals, **kwargs):
60
61
# Check if ``--cli-input-json`` was specified in the command line.
62
input_json = getattr(parsed_args, 'cli_input_json', None)
63
if input_json is not None:
64
# Retrieve the JSON from the file if needed.
65
retrieved_json = get_paramfile(input_json, LOCAL_PREFIX_MAP)
66
# Nothing was retrieved from the file. So assume the argument
67
# is already a JSON string.
68
if retrieved_json is None:
69
retrieved_json = input_json
70
try:
71
# Try to load the JSON string into a python dictionary
72
input_data = json.loads(retrieved_json)
73
except ValueError as e:
74
raise ParamError(
75
self.name, "Invalid JSON: %s\nJSON received: %s"
76
% (e, retrieved_json))
77
# Add the members from the input JSON to the call parameters.
78
self._update_call_parameters(call_parameters, input_data)
79
80
def _update_call_parameters(self, call_parameters, input_data):
81
for input_key in input_data.keys():
82
# Only add the values to ``call_parameters`` if not already
83
# present.
84
if input_key not in call_parameters:
85
call_parameters[input_key] = input_data[input_key]
86
87