Skip to content

Commit

Permalink
gconftool2: minor refactor (ansible-collections#8711)
Browse files Browse the repository at this point in the history
* gconftool2: minor refactor

* add changelog frag
  • Loading branch information
russoz authored and TobiasZeuch181 committed Oct 2, 2024
1 parent 5d10d40 commit 0abd72a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8711-gconftool2-refactor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- gconftool2 - make use of ``ModuleHelper`` features to simplify code (https://github.com/ansible-collections/community.general/pull/8711).
13 changes: 5 additions & 8 deletions plugins/modules/gconftool2.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ class GConftool(StateModuleHelper):

def __init_module__(self):
self.runner = gconftool2_runner(self.module, check_rc=True)
if self.vars.state != "get":
if not self.vars.direct and self.vars.config_source is not None:
self.module.fail_json(msg='If the "config_source" is specified then "direct" must be "true"')
if not self.vars.direct and self.vars.config_source is not None:
self.do_raise('If the "config_source" is specified then "direct" must be "true"')

self.vars.set('previous_value', self._get(), fact=True)
self.vars.set('value_type', self.vars.value_type)
Expand All @@ -140,7 +139,7 @@ def __init_module__(self):
def _make_process(self, fail_on_err):
def process(rc, out, err):
if err and fail_on_err:
self.ansible.fail_json(msg='gconftool-2 failed with error: %s' % (str(err)))
self.do_raise('gconftool-2 failed with error:\n%s' % err.strip())
out = out.rstrip()
self.vars.value = None if out == "" else out
return self.vars.value
Expand All @@ -152,16 +151,14 @@ def _get(self):
def state_absent(self):
with self.runner("state key", output_process=self._make_process(False)) as ctx:
ctx.run()
if self.verbosity >= 4:
self.vars.run_info = ctx.run_info
self.vars.set('run_info', ctx.run_info, verbosity=4)
self.vars.set('new_value', None, fact=True)
self.vars._value = None

def state_present(self):
with self.runner("direct config_source value_type state key value", output_process=self._make_process(True)) as ctx:
ctx.run()
if self.verbosity >= 4:
self.vars.run_info = ctx.run_info
self.vars.set('run_info', ctx.run_info, verbosity=4)
self.vars.set('new_value', self._get(), fact=True)
self.vars._value = self.vars.new_value

Expand Down

0 comments on commit 0abd72a

Please sign in to comment.