mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-10-04 15:34:01 -07:00
Save the command line arguments into a global context
* Once cli args are parsed, they're constant. So, save the parsed args into the global context for everyone else to use them from now on. * Port cli scripts to use the CLIARGS in the context * Refactor call to parse cli args into the run() method * Fix unittests for changes to the internals of CLI arg parsing * Port callback plugins to use context.CLIARGS * Got rid of the private self._options attribute * Use context.CLIARGS in the individual callback plugins instead. * Also output positional arguments in default and unixy plugins * Code has been simplified since we're now dealing with a dict rather than Optparse.Value
This commit is contained in:
parent
c18da65089
commit
afdbb0d9d5
36 changed files with 1033 additions and 868 deletions
53
lib/ansible/context.py
Normal file
53
lib/ansible/context.py
Normal file
|
@ -0,0 +1,53 @@
|
|||
# Copyright: (c) 2018, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
# Make coding more python3-ish
|
||||
from __future__ import (absolute_import, division, print_function)
|
||||
__metaclass__ = type
|
||||
|
||||
"""
|
||||
Context of the running Ansible.
|
||||
|
||||
In the future we *may* create Context objects to allow running multiple Ansible plays in parallel
|
||||
with different contexts but that is currently out of scope as the Ansible library is just for
|
||||
running the ansible command line tools.
|
||||
|
||||
These APIs are still in flux so do not use them unless you are willing to update them with every Ansible release
|
||||
"""
|
||||
|
||||
from ansible import arguments
|
||||
|
||||
|
||||
# Note: this is not the singleton version. That is only created once the program has actually
|
||||
# parsed the args
|
||||
CLIARGS = arguments.CLIArgs({})
|
||||
|
||||
|
||||
class _Context:
|
||||
"""
|
||||
Not yet ready for Prime Time
|
||||
|
||||
Eventually this may allow for code which needs to run under different contexts (for instance, as
|
||||
if they were run with different command line args or from different current working directories)
|
||||
to exist in the same process. But at the moment, we don't need that so this code has not been
|
||||
tested for suitability.
|
||||
"""
|
||||
def __init__(self):
|
||||
global CLIARGS
|
||||
self._CLIARGS = arguments.CLIArgs(CLIARGS)
|
||||
|
||||
@property
|
||||
def CLIARGS(self):
|
||||
return self._CLIARGS
|
||||
|
||||
@CLIARGS.setter
|
||||
def CLIARGS_set(self, new_cli_args):
|
||||
if not isinstance(new_cli_args, arguments.CLIArgs):
|
||||
raise TypeError('CLIARGS must be of type (ansible.arguments.CLIArgs)')
|
||||
self._CLIARGS = new_cli_args
|
||||
|
||||
|
||||
def _init_global_context(cli_args):
|
||||
"""Initialize the global context objects"""
|
||||
global CLIARGS
|
||||
CLIARGS = arguments.GlobalCLIArgs.from_options(cli_args)
|
Loading…
Add table
Add a link
Reference in a new issue