Detect legacy versions of chocolatey correctly

- This commit makes sure to detect both newer and legacy versions of chocolatey before giving up
This commit is contained in:
Daniel Hobley 2015-10-29 15:36:47 +01:00
parent e7571e6d61
commit 157e0f446d

View file

@ -102,20 +102,30 @@ def chocolatey_version():
'''
if 'chocolatey._version' in __context__:
return __context__['chocolatey._version']
cmd = [_find_chocolatey(__context__, __salt__)]
out = __salt__['cmd.run'](cmd, python_shell=False)
for line in out.splitlines():
line = line.lower()
if line.startswith('chocolatey v'):
__context__['chocolatey._version'] = line[12:]
return __context__['chocolatey._version']
elif line.startswith('version: '):
try:
__context__['chocolatey._version'] = \
line.split(None, 1)[-1].strip("'")
def find_version(legacy=False):
cmd = [_find_chocolatey(__context__, __salt__)]
if legacy:
cmd.append('help')
out = __salt__['cmd.run'](cmd, python_shell=False)
for line in out.splitlines():
line = line.lower()
if line.startswith('chocolatey v'):
__context__['chocolatey._version'] = line[12:]
return __context__['chocolatey._version']
except Exception:
pass
elif line.startswith('version: '):
try:
__context__['chocolatey._version'] = \
line.split(None, 1)[-1].strip("'")
return __context__['chocolatey._version']
except Exception:
pass
# First try to find if we have a newer version of choco
# which doesn't contain the help command,
# else try for a legacy version
find_version(legacy=False)
find_version(legacy=True)
raise CommandExecutionError('Unable to determine Chocolatey version')