From 127016d36bea0e2057ad238eb2490d12725edd4a Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Wed, 1 Jun 2022 13:24:24 -0400 Subject: [PATCH] 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` --- awxkit/awxkit/cli/options.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/awxkit/awxkit/cli/options.py b/awxkit/awxkit/cli/options.py index 6253c28d19..fe200f71a1 100644 --- a/awxkit/awxkit/cli/options.py +++ b/awxkit/awxkit/cli/options.py @@ -62,6 +62,17 @@ def pk_or_name(v2, model_name, value, page=None): 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): deprecated = False @@ -132,6 +143,7 @@ class ResourceOptionsParser(object): for k, param in self.options.get(http_method, {}).items(): required = method == 'create' and param.get('required', False) is True help_text = param.get('help_text', '') + args = ['--{}'.format(k)] if method == 'list': if k == 'id': @@ -166,9 +178,6 @@ class ResourceOptionsParser(object): return parsed - def jsonstr(v): - return json.dumps(json_or_yaml(v)) - kwargs = { 'help': help_text, 'required': required, @@ -235,16 +244,20 @@ class ResourceOptionsParser(object): if (self.resource in ('job_templates', 'workflow_job_templates') and k == 'extra_vars') or ( 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_group is None: required_group = self.parser.choices[method].add_argument_group('required arguments') # put the required group first (before the optional args group) self.parser.choices[method]._action_groups.reverse() - required_group.add_argument('--{}'.format(k), **kwargs) + required_group.add_argument(*args, **kwargs) else: - self.parser.choices[method].add_argument('--{}'.format(k), **kwargs) + self.parser.choices[method].add_argument(*args, **kwargs) def handle_custom_actions(self): for _, action in CustomAction.registry.items():