mirror of
https://github.com/ZwareBear/awx.git
synced 2026-03-20 07:43:35 -05:00
Allow for multiple --extra_vars or --variables flags in awx-cli
This is particularly useful when you are using the @filepath version of the flag, since otherwise there would be no way to issue the command with multiple vars files. Also, add `-e` as an alias to `--extra_vars`
This commit is contained in:
@@ -62,6 +62,17 @@ def pk_or_name(v2, model_name, value, page=None):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
class JsonDumpsAction(argparse.Action):
|
||||||
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
|
# This Action gets called repeatedly on each instance of the flag that it is
|
||||||
|
# tied to, and unfortunately doesn't come with a good way of noticing we are at
|
||||||
|
# the end. So it's necessary to keep doing json.loads and json.dumps each time.
|
||||||
|
|
||||||
|
json_vars = json.loads(getattr(namespace, self.dest, None) or '{}')
|
||||||
|
json_vars.update(values)
|
||||||
|
setattr(namespace, self.dest, json.dumps(json_vars))
|
||||||
|
|
||||||
|
|
||||||
class ResourceOptionsParser(object):
|
class ResourceOptionsParser(object):
|
||||||
|
|
||||||
deprecated = False
|
deprecated = False
|
||||||
@@ -132,6 +143,7 @@ class ResourceOptionsParser(object):
|
|||||||
for k, param in self.options.get(http_method, {}).items():
|
for k, param in self.options.get(http_method, {}).items():
|
||||||
required = method == 'create' and param.get('required', False) is True
|
required = method == 'create' and param.get('required', False) is True
|
||||||
help_text = param.get('help_text', '')
|
help_text = param.get('help_text', '')
|
||||||
|
args = ['--{}'.format(k)]
|
||||||
|
|
||||||
if method == 'list':
|
if method == 'list':
|
||||||
if k == 'id':
|
if k == 'id':
|
||||||
@@ -166,9 +178,6 @@ class ResourceOptionsParser(object):
|
|||||||
|
|
||||||
return parsed
|
return parsed
|
||||||
|
|
||||||
def jsonstr(v):
|
|
||||||
return json.dumps(json_or_yaml(v))
|
|
||||||
|
|
||||||
kwargs = {
|
kwargs = {
|
||||||
'help': help_text,
|
'help': help_text,
|
||||||
'required': required,
|
'required': required,
|
||||||
@@ -235,16 +244,20 @@ class ResourceOptionsParser(object):
|
|||||||
if (self.resource in ('job_templates', 'workflow_job_templates') and k == 'extra_vars') or (
|
if (self.resource in ('job_templates', 'workflow_job_templates') and k == 'extra_vars') or (
|
||||||
self.resource in ('inventory', 'groups', 'hosts') and k == 'variables'
|
self.resource in ('inventory', 'groups', 'hosts') and k == 'variables'
|
||||||
):
|
):
|
||||||
kwargs['type'] = jsonstr
|
kwargs['type'] = json_or_yaml
|
||||||
|
kwargs['action'] = JsonDumpsAction
|
||||||
|
|
||||||
|
if k == 'extra_vars':
|
||||||
|
args.append('-e')
|
||||||
|
|
||||||
if required:
|
if required:
|
||||||
if required_group is None:
|
if required_group is None:
|
||||||
required_group = self.parser.choices[method].add_argument_group('required arguments')
|
required_group = self.parser.choices[method].add_argument_group('required arguments')
|
||||||
# put the required group first (before the optional args group)
|
# put the required group first (before the optional args group)
|
||||||
self.parser.choices[method]._action_groups.reverse()
|
self.parser.choices[method]._action_groups.reverse()
|
||||||
required_group.add_argument('--{}'.format(k), **kwargs)
|
required_group.add_argument(*args, **kwargs)
|
||||||
else:
|
else:
|
||||||
self.parser.choices[method].add_argument('--{}'.format(k), **kwargs)
|
self.parser.choices[method].add_argument(*args, **kwargs)
|
||||||
|
|
||||||
def handle_custom_actions(self):
|
def handle_custom_actions(self):
|
||||||
for _, action in CustomAction.registry.items():
|
for _, action in CustomAction.registry.items():
|
||||||
|
|||||||
Reference in New Issue
Block a user