Debian Codegraph @

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

raw
1 # Copyright (c) 2023, Alexei Znamensky <[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 _alias_state_map = dict(
19 present="alias",
20 absent="unalias",
21 info="aliases",
22 )
23
24 _state_map = dict(
25 present="install",
26 absent="remove",
27 enabled="enable",
28 disabled="disable",
29 refresh="refresh",
30 )
31
32
33 def snap_runner(module: AnsibleModule, **kwargs) -> CmdRunner:
34 runner = CmdRunner(
35 module,
36 "snap",
37 arg_formats=dict(
38 state_alias=cmd_runner_fmt.as_map(_alias_state_map), # snap_alias only
39 name=cmd_runner_fmt.as_list(),
40 alias=cmd_runner_fmt.as_list(), # snap_alias only
41 state=cmd_runner_fmt.as_map(_state_map),
42 _list=cmd_runner_fmt.as_fixed("list"),
43 _set=cmd_runner_fmt.as_fixed("set"),
44 get=cmd_runner_fmt.as_fixed(["get", "-d"]),
45 classic=cmd_runner_fmt.as_bool("--classic"),
46 channel=cmd_runner_fmt.as_func(lambda v: [] if v == "stable" else ["--channel", f"{v}"]),
47 options=cmd_runner_fmt.as_list(),
48 info=cmd_runner_fmt.as_fixed("info"),
49 revision=cmd_runner_fmt.as_opt_val("--revision"),
50 hold=cmd_runner_fmt.as_bool("--hold"),
51 unhold=cmd_runner_fmt.as_bool("--unhold"),
52 dangerous=cmd_runner_fmt.as_bool("--dangerous"),
53 devmode=cmd_runner_fmt.as_bool("--devmode"),
54 version=cmd_runner_fmt.as_fixed("version"),
55 _connect=cmd_runner_fmt.as_func(lambda v: ["connect", v]),
56 _disconnect=cmd_runner_fmt.as_func(lambda v: ["disconnect", v]),
57 _connections=cmd_runner_fmt.as_fixed("connections"),
58 slot=cmd_runner_fmt.as_list(),
59 ),
60 check_rc=False,
61 **kwargs,
62 )
63 return runner
64
65
66 def get_version(runner: CmdRunner) -> dict[str, list[str]]:
67 with runner("version") as ctx:
68 rc, out, err = ctx.run()
69 return dict(x.split() for x in out.splitlines() if len(x.split()) == 2)