import os
from awscli.customizations.commands import BasicCommand
from awscli.customizations.configure.writer import ConfigFileWriter
from . import PREDEFINED_SECTION_NAMES, profile_to_section
class ConfigureSetCommand(BasicCommand):
NAME = 'set'
DESCRIPTION = BasicCommand.FROM_FILE('configure', 'set',
'_description.rst')
SYNOPSIS = 'aws configure set varname value [--profile profile-name]'
EXAMPLES = BasicCommand.FROM_FILE('configure', 'set', '_examples.rst')
ARG_TABLE = [
{'name': 'varname',
'help_text': 'The name of the config value to set.',
'action': 'store',
'cli_type_name': 'string', 'positional_arg': True},
{'name': 'value',
'help_text': 'The value to set.',
'action': 'store',
'no_paramfile': True,
'cli_type_name': 'string', 'positional_arg': True},
]
_WRITE_TO_CREDS_FILE = ['aws_access_key_id', 'aws_secret_access_key',
'aws_session_token']
def __init__(self, session, config_writer=None):
super(ConfigureSetCommand, self).__init__(session)
if config_writer is None:
config_writer = ConfigFileWriter()
self._config_writer = config_writer
def _get_config_file(self, path):
config_path = self._session.get_config_variable(path)
return os.path.expanduser(config_path)
def _run_main(self, args, parsed_globals):
varname = args.varname
value = args.value
profile = 'default'
if '.' not in varname:
if self._session.profile is not None:
profile = self._session.profile
else:
parts = varname.split('.')
if parts[0] in ('default', 'profile'):
if parts[0] == 'default':
profile = 'default'
remaining = parts[1:]
else:
profile = parts[1]
remaining = parts[2:]
varname = remaining[0]
if len(remaining) == 2:
value = {remaining[1]: value}
elif parts[0] not in PREDEFINED_SECTION_NAMES:
if self._session.profile is not None:
profile = self._session.profile
else:
profile_name = self._session.get_config_variable('profile')
if profile_name is not None:
profile = profile_name
varname = parts[0]
if len(parts) == 2:
value = {parts[1]: value}
elif len(parts) == 2:
profile, varname = parts
config_filename = self._get_config_file('config_file')
if varname in self._WRITE_TO_CREDS_FILE:
section = profile
config_filename = self._get_config_file('credentials_file')
elif profile in PREDEFINED_SECTION_NAMES or profile == 'default':
section = profile
else:
section = profile_to_section(profile)
updated_config = {'__section__': section, varname: value}
self._config_writer.update_config(updated_config, config_filename)
return 0