Debian Codegraph @

ansible_collections/community/general/plugins/module_utils/_systemd.py

raw
1 # Copyright (c) 2025, Marco Noce <[email protected]>
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time.
6 # Do not use this from other collections or standalone plugins/modules!
7
8 from __future__ import annotations
9
10 import typing as t
11
12 from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt
13
14 if t.TYPE_CHECKING:
15 from ansible.module_utils.basic import AnsibleModule
16
17
18 def systemd_runner(module: AnsibleModule, command, **kwargs) -> CmdRunner:
19 arg_formats = dict(
20 version=cmd_runner_fmt.as_fixed("--version"),
21 list_units=cmd_runner_fmt.as_fixed(["list-units", "--no-pager"]),
22 types=cmd_runner_fmt.as_func(lambda v: [] if not v else ["--type", ",".join(v)]),
23 all=cmd_runner_fmt.as_fixed("--all"),
24 plain=cmd_runner_fmt.as_fixed("--plain"),
25 no_legend=cmd_runner_fmt.as_fixed("--no-legend"),
26 show=cmd_runner_fmt.as_fixed("show"),
27 props=cmd_runner_fmt.as_func(lambda v: [] if not v else ["-p", ",".join(v)]),
28 dashdash=cmd_runner_fmt.as_fixed("--"),
29 unit=cmd_runner_fmt.as_list(),
30 )
31
32 runner = CmdRunner(module, command=command, arg_formats=arg_formats, check_rc=True, **kwargs)
33 return runner