Update homebrew_cask with additional cask features and doc updates (#2682)

Changes:
- Document missing 'path' option and remove redundant brew_path manipulation
- Add 'update_homebrew' option since 'brew cask update' as a synonym for
  'brew update' is available nowadays
- Add additional missing aliases documentation
- Port additional improvements (expanded support for brews path, cask path
  patterns etc.)
- Allow proper `list` type support for 'name' option.
pull/18777/head
Indrajit Raychaudhuri 8 years ago committed by Matt Clay
parent 8702ead0a7
commit 384255cada

@ -35,17 +35,33 @@ options:
description: description:
- name of cask to install/remove - name of cask to install/remove
required: true required: true
aliases: ['pkg', 'package', 'cask']
path:
description:
- "':' separated list of paths to search for 'brew' executable."
required: false
default: '/usr/local/bin'
state: state:
description: description:
- state of the cask - state of the cask
choices: [ 'present', 'absent' ] choices: [ 'present', 'absent' ]
required: false required: false
default: present default: present
update_homebrew:
description:
- update homebrew itself first. Note that C(brew cask update) is
a synonym for C(brew update).
required: false
default: no
choices: [ "yes", "no" ]
aliases: ['update-brew']
version_added: "2.2"
install_options: install_options:
description: description:
- options flags to install a package - options flags to install a package
required: false required: false
default: null default: null
aliases: ['options']
version_added: "2.2" version_added: "2.2"
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -84,6 +100,7 @@ class HomebrewCask(object):
\s # spaces \s # spaces
: # colons : # colons
{sep} # the OS-specific path separator {sep} # the OS-specific path separator
. # dots
- # dashes - # dashes
'''.format(sep=os.path.sep) '''.format(sep=os.path.sep)
@ -91,11 +108,14 @@ class HomebrewCask(object):
\w # alphanumeric characters (i.e., [a-zA-Z0-9_]) \w # alphanumeric characters (i.e., [a-zA-Z0-9_])
\s # spaces \s # spaces
{sep} # the OS-specific path separator {sep} # the OS-specific path separator
. # dots
- # dashes - # dashes
'''.format(sep=os.path.sep) '''.format(sep=os.path.sep)
VALID_CASK_CHARS = r''' VALID_CASK_CHARS = r'''
\w # alphanumeric characters (i.e., [a-zA-Z0-9_]) \w # alphanumeric characters (i.e., [a-zA-Z0-9_])
. # dots
/ # slash (for taps)
- # dashes - # dashes
''' '''
@ -113,6 +133,7 @@ class HomebrewCask(object):
- a string containing only: - a string containing only:
- alphanumeric characters - alphanumeric characters
- dashes - dashes
- dots
- spaces - spaces
- colons - colons
- os.path.sep - os.path.sep
@ -137,6 +158,7 @@ class HomebrewCask(object):
- a string containing only: - a string containing only:
- alphanumeric characters - alphanumeric characters
- dashes - dashes
- dots
- spaces - spaces
- os.path.sep - os.path.sep
''' '''
@ -185,6 +207,7 @@ class HomebrewCask(object):
'''A valid module is an instance of AnsibleModule.''' '''A valid module is an instance of AnsibleModule.'''
return isinstance(module, AnsibleModule) return isinstance(module, AnsibleModule)
# /class validations ------------------------------------------- }}} # /class validations ------------------------------------------- }}}
# class properties --------------------------------------------- {{{ # class properties --------------------------------------------- {{{
@ -266,11 +289,14 @@ class HomebrewCask(object):
return cask return cask
# /class properties -------------------------------------------- }}} # /class properties -------------------------------------------- }}}
def __init__(self, module, path=None, casks=None, state=None, def __init__(self, module, path=path, casks=None, state=None,
install_options=None): update_homebrew=False, install_options=None):
if not install_options:
install_options = list()
self._setup_status_vars() self._setup_status_vars()
self._setup_instance_vars(module=module, path=path, casks=casks, self._setup_instance_vars(module=module, path=path, casks=casks,
state=state, install_options=install_options) state=state, update_homebrew=update_homebrew,
install_options=install_options,)
self._prep() self._prep()
@ -287,13 +313,8 @@ class HomebrewCask(object):
setattr(self, key, val) setattr(self, key, val)
def _prep(self): def _prep(self):
self._prep_path()
self._prep_brew_path() self._prep_brew_path()
def _prep_path(self):
if not self.path:
self.path = ['/usr/local/bin']
def _prep_brew_path(self): def _prep_brew_path(self):
if not self.module: if not self.module:
self.brew_path = None self.brew_path = None
@ -340,8 +361,12 @@ class HomebrewCask(object):
self.message = 'Invalid cask: {0}.'.format(self.current_cask) self.message = 'Invalid cask: {0}.'.format(self.current_cask)
raise HomebrewCaskException(self.message) raise HomebrewCaskException(self.message)
cmd = [self.brew_path, 'cask', 'list'] cmd = [
rc, out, err = self.module.run_command(cmd, path_prefix=self.path[0]) "{brew_path}".format(brew_path=self.brew_path),
"cask",
"list"
]
rc, out, err = self.module.run_command(cmd)
if 'nothing to list' in err: if 'nothing to list' in err:
return False return False
@ -356,6 +381,9 @@ class HomebrewCask(object):
# commands ----------------------------------------------------- {{{ # commands ----------------------------------------------------- {{{
def _run(self): def _run(self):
if self.update_homebrew:
self._update_homebrew()
if self.state == 'installed': if self.state == 'installed':
return self._install_casks() return self._install_casks()
elif self.state == 'absent': elif self.state == 'absent':
@ -369,7 +397,7 @@ class HomebrewCask(object):
rc, out, err = self.module.run_command([ rc, out, err = self.module.run_command([
self.brew_path, self.brew_path,
'update', 'update',
], path_prefix=self.path[0]) ])
if rc == 0: if rc == 0:
if out and isinstance(out, basestring): if out and isinstance(out, basestring):
already_updated = any( already_updated = any(
@ -417,8 +445,7 @@ class HomebrewCask(object):
) )
cmd = [opt for opt in opts if opt] cmd = [opt for opt in opts if opt]
rc, out, err = self.module.run_command(cmd)
rc, out, err = self.module.run_command(cmd, path_prefix=self.path[0])
if self._current_cask_is_installed(): if self._current_cask_is_installed():
self.changed_count += 1 self.changed_count += 1
@ -463,7 +490,7 @@ class HomebrewCask(object):
for opt in (self.brew_path, 'cask', 'uninstall', self.current_cask) for opt in (self.brew_path, 'cask', 'uninstall', self.current_cask)
if opt] if opt]
rc, out, err = self.module.run_command(cmd, path_prefix=self.path[0]) rc, out, err = self.module.run_command(cmd)
if not self._current_cask_is_installed(): if not self._current_cask_is_installed():
self.changed_count += 1 self.changed_count += 1
@ -488,8 +515,16 @@ class HomebrewCask(object):
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec=dict( argument_spec=dict(
name=dict(aliases=["cask"], required=False), name=dict(
path=dict(required=False), aliases=["pkg", "package", "cask"],
required=False,
type='list',
),
path=dict(
default="/usr/local/bin",
required=False,
type='path',
),
state=dict( state=dict(
default="present", default="present",
choices=[ choices=[
@ -497,6 +532,11 @@ def main():
"absent", "removed", "uninstalled", "absent", "removed", "uninstalled",
], ],
), ),
update_homebrew=dict(
default=False,
aliases=["update-brew"],
type='bool',
),
install_options=dict( install_options=dict(
default=None, default=None,
aliases=['options'], aliases=['options'],
@ -511,15 +551,13 @@ def main():
p = module.params p = module.params
if p['name']: if p['name']:
casks = p['name'].split(',') casks = p['name']
else: else:
casks = None casks = None
path = p['path'] path = p['path']
if path: if path:
path = path.split(':') path = path.split(':')
else:
path = ['/usr/local/bin']
state = p['state'] state = p['state']
if state in ('present', 'installed'): if state in ('present', 'installed'):
@ -527,13 +565,14 @@ def main():
if state in ('absent', 'removed', 'uninstalled'): if state in ('absent', 'removed', 'uninstalled'):
state = 'absent' state = 'absent'
update_homebrew = p['update_homebrew']
p['install_options'] = p['install_options'] or [] p['install_options'] = p['install_options'] or []
install_options = ['--{0}'.format(install_option) install_options = ['--{0}'.format(install_option)
for install_option in p['install_options']] for install_option in p['install_options']]
brew_cask = HomebrewCask(module=module, path=path, casks=casks, brew_cask = HomebrewCask(module=module, path=path, casks=casks,
state=state, install_options=install_options) state=state, update_homebrew=update_homebrew,
install_options=install_options)
(failed, changed, message) = brew_cask.run() (failed, changed, message) = brew_cask.run()
if failed: if failed:
module.fail_json(msg=message) module.fail_json(msg=message)

Loading…
Cancel
Save