Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated deprecated homebrew cask commands #1481

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/1481-deprecated-brew-cask-command.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- homebrew_cask - Homebrew will be deprecating use of ``brew cask`` commands as of version 2.6.0, see https://brew.sh/2020/12/01/homebrew-2.6.0/. Added logic to stop using ``brew cask`` for brew version >= 2.6.0 (https://github.com/ansible-collections/community.general/pull/1481).
98 changes: 65 additions & 33 deletions plugins/modules/packaging/os/homebrew_cask.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
import os
import re
import tempfile
from distutils import version

from ansible.module_utils._text import to_bytes
from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -356,6 +357,18 @@ def current_cask(self, cask):
else:
self._current_cask = cask
return cask

@property
def brew_version(self):
try:
return self._brew_version
except AttributeError:
return None

@brew_version.setter
def brew_version(self, brew_version):
self._brew_version = brew_version

# /class properties -------------------------------------------- }}}

def __init__(self, module, path=path, casks=None, state=None,
Expand Down Expand Up @@ -434,15 +447,12 @@ def _current_cask_is_outdated(self):
if not self.valid_cask(self.current_cask):
return False

cask_is_outdated_command = (
[
self.brew_path,
'cask',
'outdated',
]
+ (['--greedy'] if self.greedy else [])
+ [self.current_cask]
)
if self._brew_cask_command_is_deprecated():
base_opts = [self.brew_path, 'outdated', '--cask']
else:
base_opts = [self.brew_path, 'cask', 'outdated']

cask_is_outdated_command = base_opts + (['--greedy'] if self.greedy else []) + [self.current_cask]

rc, out, err = self.module.run_command(cask_is_outdated_command)

Expand All @@ -454,18 +464,35 @@ def _current_cask_is_installed(self):
self.message = 'Invalid cask: {0}.'.format(self.current_cask)
raise HomebrewCaskException(self.message)

cmd = [
"{brew_path}".format(brew_path=self.brew_path),
"cask",
"list",
self.current_cask
]
if self._brew_cask_command_is_deprecated():
base_opts = [self.brew_path, "list", "--cask"]
else:
base_opts = [self.brew_path, "cask", "list"]

cmd = base_opts + [self.current_cask]
rc, out, err = self.module.run_command(cmd)

if rc == 0:
return True
else:
return False

def _get_brew_version(self):
if self.brew_version:
return self.brew_version

cmd = [self.brew_path, '--version']

rc, out, err = self.module.run_command(cmd, check_rc=True)

# get version string from first line of "brew --version" output
version = out.split('\n')[0].split(' ')[1]
self.brew_version = version
return self.brew_version

def _brew_cask_command_is_deprecated(self):
# The `brew cask` replacements were fully available in 2.6.0 (https://brew.sh/2020/12/01/homebrew-2.6.0/)
return version.LooseVersion(self._get_brew_version()) >= version.LooseVersion('2.6.0')
# /checks ------------------------------------------------------ }}}

# commands ----------------------------------------------------- {{{
Expand Down Expand Up @@ -537,11 +564,10 @@ def _upgrade_all(self):
self.message = 'Casks would be upgraded.'
raise HomebrewCaskException(self.message)

opts = (
[self.brew_path, 'cask', 'upgrade']
)

cmd = [opt for opt in opts if opt]
if self._brew_cask_command_is_deprecated():
cmd = [self.brew_path, 'upgrade', '--cask']
else:
cmd = [self.brew_path, 'cask', 'upgrade']

rc, out, err = '', '', ''

Expand Down Expand Up @@ -586,10 +612,12 @@ def _install_current_cask(self):
)
raise HomebrewCaskException(self.message)

opts = (
[self.brew_path, 'cask', 'install', self.current_cask]
+ self.install_options
)
if self._brew_cask_command_is_deprecated():
base_opts = [self.brew_path, 'install', '--cask']
else:
base_opts = [self.brew_path, 'cask', 'install']

opts = base_opts + [self.current_cask] + self.install_options

cmd = [opt for opt in opts if opt]

Expand Down Expand Up @@ -650,11 +678,13 @@ def _upgrade_current_cask(self):
)
raise HomebrewCaskException(self.message)

opts = (
[self.brew_path, 'cask', command]
+ self.install_options
+ [self.current_cask]
)
if self._brew_cask_command_is_deprecated():
base_opts = [self.brew_path, command, '--cask']
else:
base_opts = [self.brew_path, 'cask', command]

opts = base_opts + self.install_options + [self.current_cask]

cmd = [opt for opt in opts if opt]

rc, out, err = '', '', ''
Expand Down Expand Up @@ -703,10 +733,12 @@ def _uninstall_current_cask(self):
)
raise HomebrewCaskException(self.message)

opts = (
[self.brew_path, 'cask', 'uninstall', self.current_cask]
+ self.install_options
)
if self._brew_cask_command_is_deprecated():
base_opts = [self.brew_path, 'uninstall', '--cask']
else:
base_opts = [self.brew_path, 'cask', 'uninstall']

opts = base_opts + [self.current_cask] + self.install_options

cmd = [opt for opt in opts if opt]

Expand Down