mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-30 14:21:26 -07:00
Migrate command line parsing to argparse (#50610)
* Start of migration to argparse * various fixes and improvements * Linting fixes * Test fixes * Fix vault_password_files * Add PrependAction for argparse * A bunch of additional tweak/fixes * Fix ansible-config tests * Fix man page generation * linting fix * More adhoc pattern fixes * Add changelog fragment * Add support for argcomplete * Enable argcomplete global completion * Rename PrependAction to PrependListAction to better describe what it does * Add documentation for installing and configuring argcomplete * Address rebase issues * Fix display encoding for vault * Fix line length * Address rebase issues * Handle rebase issues * Use mutually exclusive group instead of handling manually * Fix rebase issues * Address rebase issue * Update version added for argcomplete support * -e must be given a value * ci_complete
This commit is contained in:
parent
7ee6c136fd
commit
db6cc60352
28 changed files with 930 additions and 914 deletions
|
@ -7,11 +7,11 @@ __metaclass__ = type
|
|||
import os
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
from ansible import context
|
||||
from ansible.cli import CLI
|
||||
from ansible.cli.arguments import option_helpers as opt_help
|
||||
from ansible.config.manager import ConfigManager, Setting, find_ini_config_file
|
||||
from ansible.errors import AnsibleError, AnsibleOptionsError
|
||||
from ansible.module_utils._text import to_native, to_text
|
||||
|
@ -26,8 +26,6 @@ display = Display()
|
|||
class ConfigCLI(CLI):
|
||||
""" Config command line class """
|
||||
|
||||
VALID_ACTIONS = frozenset(("view", "dump", "list")) # TODO: edit, update, search
|
||||
|
||||
def __init__(self, args, callback=None):
|
||||
|
||||
self.config_file = None
|
||||
|
@ -37,35 +35,43 @@ class ConfigCLI(CLI):
|
|||
def init_parser(self):
|
||||
|
||||
super(ConfigCLI, self).init_parser(
|
||||
usage="usage: %%prog [%s] [--help] [options] [ansible.cfg]" % "|".join(sorted(self.VALID_ACTIONS)),
|
||||
epilog="\nSee '%s <command> --help' for more information on a specific command.\n\n" % os.path.basename(sys.argv[0]),
|
||||
desc="View, edit, and manage ansible configuration.",
|
||||
)
|
||||
self.parser.add_option('-c', '--config', dest='config_file',
|
||||
help="path to configuration file, defaults to first file found in precedence.")
|
||||
|
||||
self.set_action()
|
||||
common = opt_help.argparse.ArgumentParser(add_help=False)
|
||||
opt_help.add_verbosity_options(common)
|
||||
common.add_argument('-c', '--config', dest='config_file',
|
||||
help="path to configuration file, defaults to first file found in precedence.")
|
||||
|
||||
# options specific to self.actions
|
||||
if self.action == "list":
|
||||
self.parser.set_usage("usage: %prog list [options] ")
|
||||
subparsers = self.parser.add_subparsers(dest='action')
|
||||
subparsers.required = True
|
||||
|
||||
elif self.action == "dump":
|
||||
self.parser.add_option('--only-changed', dest='only_changed', action='store_true',
|
||||
help="Only show configurations that have changed from the default")
|
||||
list_parser = subparsers.add_parser('list', help='Print all config options', parents=[common])
|
||||
list_parser.set_defaults(func=self.execute_list)
|
||||
|
||||
elif self.action == "update":
|
||||
self.parser.add_option('-s', '--setting', dest='setting', help="config setting, the section defaults to 'defaults'")
|
||||
self.parser.set_usage("usage: %prog update [options] [-c ansible.cfg] -s '[section.]setting=value'")
|
||||
dump_parser = subparsers.add_parser('dump', help='Dump configuration', parents=[common])
|
||||
dump_parser.set_defaults(func=self.execute_dump)
|
||||
dump_parser.add_argument('--only-changed', dest='only_changed', action='store_true',
|
||||
help="Only show configurations that have changed from the default")
|
||||
|
||||
elif self.action == "search":
|
||||
self.parser.set_usage("usage: %prog update [options] [-c ansible.cfg] <search term>")
|
||||
view_parser = subparsers.add_parser('view', help='View configuration file', parents=[common])
|
||||
view_parser.set_defaults(func=self.execute_view)
|
||||
|
||||
def post_process_args(self, options, args):
|
||||
options, args = super(ConfigCLI, self).post_process_args(options, args)
|
||||
# update_parser = subparsers.add_parser('update', help='Update configuration option')
|
||||
# update_parser.set_defaults(func=self.execute_update)
|
||||
# update_parser.add_argument('-s', '--setting', dest='setting',
|
||||
# help="config setting, the section defaults to 'defaults'",
|
||||
# metavar='[section.]setting=value')
|
||||
|
||||
# search_parser = subparsers.add_parser('search', help='Search configuration')
|
||||
# search_parser.set_defaults(func=self.execute_search)
|
||||
# search_parser.add_argument('args', help='Search term', metavar='<search term>')
|
||||
|
||||
def post_process_args(self, options):
|
||||
options = super(ConfigCLI, self).post_process_args(options)
|
||||
display.verbosity = options.verbosity
|
||||
|
||||
return options, args
|
||||
return options
|
||||
|
||||
def run(self):
|
||||
|
||||
|
@ -87,15 +93,15 @@ class ConfigCLI(CLI):
|
|||
|
||||
os.environ['ANSIBLE_CONFIG'] = to_native(self.config_file)
|
||||
except Exception:
|
||||
if self.action in ['view']:
|
||||
if context.CLIARGS['action'] in ['view']:
|
||||
raise
|
||||
elif self.action in ['edit', 'update']:
|
||||
elif context.CLIARGS['action'] in ['edit', 'update']:
|
||||
display.warning("File does not exist, used empty file: %s" % self.config_file)
|
||||
|
||||
elif self.action == 'view':
|
||||
elif context.CLIARGS['action'] == 'view':
|
||||
raise AnsibleError('Invalid or no config file was supplied')
|
||||
|
||||
self.execute()
|
||||
context.CLIARGS['func']()
|
||||
|
||||
def execute_update(self):
|
||||
'''
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue