Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/configure/set.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 os
14
15
from awscli.customizations.commands import BasicCommand
16
from awscli.customizations.configure.writer import ConfigFileWriter
17
18
from . import PREDEFINED_SECTION_NAMES, profile_to_section
19
20
21
class ConfigureSetCommand(BasicCommand):
22
NAME = 'set'
23
DESCRIPTION = BasicCommand.FROM_FILE('configure', 'set',
24
'_description.rst')
25
SYNOPSIS = 'aws configure set varname value [--profile profile-name]'
26
EXAMPLES = BasicCommand.FROM_FILE('configure', 'set', '_examples.rst')
27
ARG_TABLE = [
28
{'name': 'varname',
29
'help_text': 'The name of the config value to set.',
30
'action': 'store',
31
'cli_type_name': 'string', 'positional_arg': True},
32
{'name': 'value',
33
'help_text': 'The value to set.',
34
'action': 'store',
35
'no_paramfile': True, # To disable the default paramfile behavior
36
'cli_type_name': 'string', 'positional_arg': True},
37
]
38
# Any variables specified in this list will be written to
39
# the ~/.aws/credentials file instead of ~/.aws/config.
40
_WRITE_TO_CREDS_FILE = ['aws_access_key_id', 'aws_secret_access_key',
41
'aws_session_token']
42
43
def __init__(self, session, config_writer=None):
44
super(ConfigureSetCommand, self).__init__(session)
45
if config_writer is None:
46
config_writer = ConfigFileWriter()
47
self._config_writer = config_writer
48
49
def _get_config_file(self, path):
50
config_path = self._session.get_config_variable(path)
51
return os.path.expanduser(config_path)
52
53
def _run_main(self, args, parsed_globals):
54
varname = args.varname
55
value = args.value
56
profile = 'default'
57
# Before handing things off to the config writer,
58
# we need to find out three things:
59
# 1. What section we're writing to (profile).
60
# 2. The name of the config key (varname)
61
# 3. The actual value (value).
62
if '.' not in varname:
63
# unqualified name, scope it to the current
64
# profile (or leave it as the 'default' section if
65
# no profile is set).
66
if self._session.profile is not None:
67
profile = self._session.profile
68
else:
69
# First figure out if it's been scoped to a profile.
70
parts = varname.split('.')
71
if parts[0] in ('default', 'profile'):
72
# Then we know we're scoped to a profile.
73
if parts[0] == 'default':
74
profile = 'default'
75
remaining = parts[1:]
76
else:
77
# [profile, profile_name, ...]
78
profile = parts[1]
79
remaining = parts[2:]
80
varname = remaining[0]
81
if len(remaining) == 2:
82
value = {remaining[1]: value}
83
elif parts[0] not in PREDEFINED_SECTION_NAMES:
84
if self._session.profile is not None:
85
profile = self._session.profile
86
else:
87
profile_name = self._session.get_config_variable('profile')
88
if profile_name is not None:
89
profile = profile_name
90
varname = parts[0]
91
if len(parts) == 2:
92
value = {parts[1]: value}
93
elif len(parts) == 2:
94
# Otherwise it's something like "set preview.service true"
95
# of something in the [plugin] section.
96
profile, varname = parts
97
config_filename = self._get_config_file('config_file')
98
if varname in self._WRITE_TO_CREDS_FILE:
99
# When writing to the creds file, the section is just the profile
100
section = profile
101
config_filename = self._get_config_file('credentials_file')
102
elif profile in PREDEFINED_SECTION_NAMES or profile == 'default':
103
section = profile
104
else:
105
section = profile_to_section(profile)
106
updated_config = {'__section__': section, varname: value}
107
self._config_writer.update_config(updated_config, config_filename)
108
return 0
109
110