Skip to content

Commit

Permalink
Decouple user agent for version
Browse files Browse the repository at this point in the history
Keep the existing formatting for the cli version.
  • Loading branch information
kdaily committed May 2, 2024
1 parent 341d2d5 commit f55eb6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
33 changes: 17 additions & 16 deletions awscli/clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,28 +441,29 @@ def create_help_command(self):
cli_data.get('synopsis', None),
cli_data.get('help_usage', None))

def _legacy_user_agent(self):
# user_agent_extra on linux will look like "rpm/x86_64.Ubuntu.18"
# on mac and windows like "sources/x86_64"
session_copy = copy.copy(self.session)

user_agent_extra = '%s/%s' % (
_get_distribution_source(),
platform.machine()
def _cli_version(self):
version_string = (
f'{self.session.user_agent_name}/{self.session.user_agent_version}'
f' Python/{platform.python_version()}'
f' {platform.system()}/{platform.release()}'
)
linux_distribution = _get_distribution()
if linux_distribution:
user_agent_extra += ".%s" % linux_distribution
session_copy.user_agent_extra = user_agent_extra

return session_copy.user_agent()
if execution_env := os.environ.get('AWS_EXECUTION_ENV') is not None:
version_string += f' exec-env/{execution_env}'

version_string += f'{_get_distribution_source()}/{platform.machine()}'

if linux_distribution := _get_distribution():
version_string += f'.{linux_distribution}'

return version_string

def create_parser(self, command_table):
# Also add a 'help' command.
command_table['help'] = self.create_help_command()
cli_data = self._get_cli_data()
parser = MainArgParser(
command_table, self._legacy_user_agent(),
command_table, self._cli_version(),
cli_data.get('description', None),
self._get_argument_table(),
prog="aws")
Expand Down Expand Up @@ -490,7 +491,7 @@ def main(self, args=None):
self._handle_top_level_args(parsed_args)
self._emit_session_event(parsed_args)
HISTORY_RECORDER.record(
'CLI_VERSION', self._legacy_user_agent(), 'CLI')
'CLI_VERSION', self._cli_version(), 'CLI')
HISTORY_RECORDER.record('CLI_ARGUMENTS', args, 'CLI')
return command_table[parsed_args.command](remaining, parsed_args)
except BaseException as e:
Expand Down Expand Up @@ -535,7 +536,7 @@ def _set_logging(self, debug):
set_stream_logger(logger_name, logging.DEBUG,
format_string=LOG_FORMAT)
enable_crt_logging()
LOG.debug("CLI version: %s", self._legacy_user_agent())
LOG.debug("CLI version: %s", self._cli_version())
LOG.debug("Arguments entered to CLI: %s", sys.argv[1:])
else:
# In case user set --debug before entering prompt mode and removed
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_clidriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ def __init__(self, emitter=None):
self.credentials = 'fakecredentials'
self.session_vars = {}
self.config_store = self._register_config_store()
self.user_agent_name = 'aws-cli'
self.user_agent_version = '100.100.100'

def _register_config_store(self):
chain_builder = ConfigChainFactory(session=self)
Expand Down

0 comments on commit f55eb6b

Please sign in to comment.