Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/configure/get.py
1567 views
1
# Copyright 2016 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 sys
14
import logging
15
16
from awscli.customizations.commands import BasicCommand
17
18
from . import PREDEFINED_SECTION_NAMES
19
20
LOG = logging.getLogger(__name__)
21
22
23
class ConfigureGetCommand(BasicCommand):
24
NAME = 'get'
25
DESCRIPTION = BasicCommand.FROM_FILE('configure', 'get',
26
'_description.rst')
27
SYNOPSIS = 'aws configure get varname [--profile profile-name]'
28
EXAMPLES = BasicCommand.FROM_FILE('configure', 'get', '_examples.rst')
29
ARG_TABLE = [
30
{'name': 'varname',
31
'help_text': 'The name of the config value to retrieve.',
32
'action': 'store',
33
'cli_type_name': 'string', 'positional_arg': True},
34
]
35
36
def __init__(self, session, stream=None, error_stream=None):
37
super(ConfigureGetCommand, self).__init__(session)
38
if stream is None:
39
stream = sys.stdout
40
if error_stream is None:
41
error_stream = sys.stderr
42
self._stream = stream
43
self._error_stream = error_stream
44
45
def _run_main(self, args, parsed_globals):
46
varname = args.varname
47
48
if '.' not in varname:
49
# get_scoped_config() returns the config variables in the config
50
# file (not the logical_var names), which is what we want.
51
config = self._session.get_scoped_config()
52
value = config.get(varname)
53
else:
54
value = self._get_dotted_config_value(varname)
55
56
LOG.debug(u'Config value retrieved: %s' % value)
57
58
if isinstance(value, str):
59
self._stream.write(value)
60
self._stream.write('\n')
61
return 0
62
elif isinstance(value, dict):
63
# TODO: add support for this. We would need to print it off in
64
# the same format as the config file.
65
self._error_stream.write(
66
'varname (%s) must reference a value, not a section or '
67
'sub-section.' % varname
68
)
69
return 1
70
else:
71
return 1
72
73
def _get_dotted_config_value(self, varname):
74
parts = varname.split('.')
75
num_dots = varname.count('.')
76
77
# Logic to deal with predefined sections like [preview], [plugin] and
78
# etc.
79
if num_dots == 1 and parts[0] in PREDEFINED_SECTION_NAMES:
80
full_config = self._session.full_config
81
section, config_name = varname.split('.')
82
value = full_config.get(section, {}).get(config_name)
83
if value is None:
84
# Try to retrieve it from the profile config.
85
value = full_config['profiles'].get(
86
section, {}).get(config_name)
87
return value
88
89
if parts[0] == 'profile':
90
profile_name = parts[1]
91
config_name = parts[2]
92
remaining = parts[3:]
93
# Check if varname starts with 'default' profile (e.g.
94
# default.emr-dev.emr.instance_profile) If not, go further to check
95
# if varname starts with a known profile name
96
elif parts[0] == 'default' or (
97
parts[0] in self._session.full_config['profiles']):
98
profile_name = parts[0]
99
config_name = parts[1]
100
remaining = parts[2:]
101
else:
102
profile_name = self._session.get_config_variable('profile')
103
if profile_name is None:
104
profile_name = 'default'
105
config_name = parts[0]
106
remaining = parts[1:]
107
108
value = self._session.full_config['profiles'].get(
109
profile_name, {}).get(config_name)
110
if len(remaining) == 1:
111
try:
112
value = value.get(remaining[-1])
113
except AttributeError:
114
value = None
115
return value
116
117