mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 12:21:26 -07:00
Add state option to git_config module (#50578)
* Add state option to git_config module State present/absent option works like --set/--unset option for 'git config'. * Change git_config to avoid useless parameter passed to git command When unsetting value, command was : git config --unset foo ''. Now command is : git config --unset foo. * Add some integration tests to git_config module * Add missing aliases file * Change set up method Using git command seems to cause troubles on some OS : changing config by changing '.gitconfig' file. * Remove some distros from tests pool Git is not installed or is out of date on these distros. * Fix aliases to skip tests on centos6 * Refactor tests of the git_config module * Add use case when state=absent and value is defined
This commit is contained in:
parent
984777b3d0
commit
2f85d62989
15 changed files with 257 additions and 5 deletions
|
@ -51,6 +51,14 @@ options:
|
|||
also specify the repo parameter. It defaults to system only when
|
||||
not using I(list_all)=yes.
|
||||
choices: [ "local", "global", "system" ]
|
||||
state:
|
||||
description:
|
||||
- "Indicates the setting should be set/unset.
|
||||
This parameter has higher precedence than I(value) parameter:
|
||||
when I(state)=absent and I(value) is defined, I(value) is discarded."
|
||||
choices: [ 'present', 'absent' ]
|
||||
default: 'present'
|
||||
version_added: '2.8'
|
||||
value:
|
||||
description:
|
||||
- When specifying the name of a single setting, supply a value to
|
||||
|
@ -69,6 +77,12 @@ EXAMPLES = '''
|
|||
scope: global
|
||||
value: status
|
||||
|
||||
# Unset some settings in ~/.gitconfig
|
||||
- git_config:
|
||||
name: alias.ci
|
||||
scope: global
|
||||
state: absent
|
||||
|
||||
# Or system-wide:
|
||||
- git_config:
|
||||
name: alias.remotev
|
||||
|
@ -149,9 +163,10 @@ def main():
|
|||
name=dict(type='str'),
|
||||
repo=dict(type='path'),
|
||||
scope=dict(required=False, type='str', choices=['local', 'global', 'system']),
|
||||
state=dict(required=False, type='str', default='present', choices=['present', 'absent']),
|
||||
value=dict(required=False)
|
||||
),
|
||||
mutually_exclusive=[['list_all', 'name'], ['list_all', 'value']],
|
||||
mutually_exclusive=[['list_all', 'name'], ['list_all', 'value'], ['list_all', 'state']],
|
||||
required_if=[('scope', 'local', ['repo'])],
|
||||
required_one_of=[['list_all', 'name']],
|
||||
supports_check_mode=True,
|
||||
|
@ -175,6 +190,12 @@ def main():
|
|||
else:
|
||||
scope = 'system'
|
||||
|
||||
if params['state'] == 'absent':
|
||||
unset = 'unset'
|
||||
params['value'] = None
|
||||
else:
|
||||
unset = None
|
||||
|
||||
if params['value']:
|
||||
new_value = params['value']
|
||||
else:
|
||||
|
@ -212,16 +233,22 @@ def main():
|
|||
k, v = value.split('=', 1)
|
||||
config_values[k] = v
|
||||
module.exit_json(changed=False, msg='', config_values=config_values)
|
||||
elif not new_value:
|
||||
elif not new_value and not unset:
|
||||
module.exit_json(changed=False, msg='', config_value=out.rstrip())
|
||||
elif unset and not out:
|
||||
module.exit_json(changed=False, msg='no setting to unset')
|
||||
else:
|
||||
old_value = out.rstrip()
|
||||
if old_value == new_value:
|
||||
module.exit_json(changed=False, msg="")
|
||||
|
||||
if not module.check_mode:
|
||||
new_value_quoted = shlex_quote(new_value)
|
||||
cmd = ' '.join(args + [new_value_quoted])
|
||||
if unset:
|
||||
args.insert(len(args) - 1, "--" + unset)
|
||||
cmd = ' '.join(args)
|
||||
else:
|
||||
new_value_quoted = shlex_quote(new_value)
|
||||
cmd = ' '.join(args + [new_value_quoted])
|
||||
(rc, out, err) = module.run_command(cmd, cwd=dir)
|
||||
if err:
|
||||
module.fail_json(rc=rc, msg=err, cmd=cmd)
|
||||
|
@ -232,7 +259,7 @@ def main():
|
|||
before_header=' '.join(args),
|
||||
before=old_value + "\n",
|
||||
after_header=' '.join(args),
|
||||
after=new_value + "\n"
|
||||
after=(new_value or '') + "\n"
|
||||
),
|
||||
changed=True
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue