mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-09 12:10:31 -07:00
adds simple implementation of adding and removing android sdk packages
This commit is contained in:
parent
e5761bd7c7
commit
215467f500
2 changed files with 76 additions and 0 deletions
18
plugins/module_utils/sdkmanager.py
Normal file
18
plugins/module_utils/sdkmanager.py
Normal file
|
@ -0,0 +1,18 @@
|
|||
from ansible_collections.community.general.plugins.module_utils import cmd_runner_fmt
|
||||
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner
|
||||
|
||||
_state_map = {
|
||||
"present": "--install",
|
||||
"absent": "--uninstall"
|
||||
}
|
||||
|
||||
|
||||
def sdkmanager_runner(module, **kwargs):
|
||||
return CmdRunner(
|
||||
module,
|
||||
command='sdkmanager',
|
||||
arg_formats=dict(
|
||||
state=cmd_runner_fmt.as_map(_state_map),
|
||||
name=cmd_runner_fmt.as_list()
|
||||
)
|
||||
)
|
58
plugins/modules/android_sdk.py
Normal file
58
plugins/modules/android_sdk.py
Normal file
|
@ -0,0 +1,58 @@
|
|||
import re
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.mh.module_helper import StateModuleHelper
|
||||
from ansible_collections.community.general.plugins.module_utils.sdkmanager import sdkmanager_runner
|
||||
|
||||
|
||||
class AndroidSdk(StateModuleHelper):
|
||||
module = dict(
|
||||
argument_spec=dict(
|
||||
state=dict(type='str', default='present', choices=['present', 'absent', 'latest']),
|
||||
package=dict(type='list', elements='str', aliases=['pkg', 'name'])
|
||||
)
|
||||
)
|
||||
use_old_vardict = False
|
||||
|
||||
def _get_formatted_packages(self):
|
||||
arg_pkgs = self.vars.package
|
||||
packages = []
|
||||
for arg_pkg in arg_pkgs:
|
||||
pkg, version = package_split(arg_pkg)
|
||||
fmt_pkg = format_cmdline_package(pkg, version)
|
||||
packages.append(fmt_pkg)
|
||||
return packages
|
||||
|
||||
def state_present(self):
|
||||
packages = self._get_formatted_packages()
|
||||
with self.sdkmanager('state name') as ctx:
|
||||
ctx.run(name=packages)
|
||||
|
||||
def state_absent(self):
|
||||
packages = self._get_formatted_packages()
|
||||
with self.sdkmanager('state name') as ctx:
|
||||
ctx.run(name=packages)
|
||||
|
||||
def __init_module__(self):
|
||||
self.sdkmanager = sdkmanager_runner(self.module)
|
||||
|
||||
|
||||
def format_cmdline_package(package, version):
|
||||
if version is None:
|
||||
return package
|
||||
else:
|
||||
return "%s;%s" % (package, version)
|
||||
|
||||
|
||||
def package_split(package):
|
||||
parts = re.split(r'=', package, maxsplit=1)
|
||||
if len(parts) > 1:
|
||||
return parts
|
||||
return parts[0], None
|
||||
|
||||
|
||||
def main():
|
||||
AndroidSdk.execute()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Reference in a new issue