Module: PuppetX::Chocolatey::ChocolateyVersion

Defined in:
lib/puppet_x/chocolatey/chocolatey_version.rb

Overview

Module responsible for retrieving the version of the currently installed Chocolatey executable

Constant Summary collapse

OLD_CHOCO_MESSAGE =

Used to remove additional message on older versions which do not print a single value

'Please run chocolatey /? or chocolatey help - chocolatey v'

Class Method Summary collapse

Class Method Details

.versionString

Retrieves the version of the currently installed Chocolatey package

Returns:

  • (String)

    Semver string of Chocolatey package



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet_x/chocolatey/chocolatey_version.rb', line 14

def self.version
  version = nil
  choco_path = "#{Facter.value('choco_install_path') || PuppetX::Chocolatey::ChocolateyInstall.install_path}\\choco.exe"
  if Puppet::Util::Platform.windows? && File.exist?(choco_path)
    begin
      # call `choco -v`
      # - new choco will output a single value e.g. `0.9.9`
      # - old choco is going to return the default output e.g. `Please run chocolatey /?`
      version = Puppet::Util::Execution.execute("#{choco_path} -v").gsub(OLD_CHOCO_MESSAGE, '')
      # - other messages, such as upgrade warnings or warnings about
      #   installing the licensed extension once the license is installed
      #   may show up when running this comamnd. Remove those as well
      version = version.split(%r{\r\n|\n|\r}).last.strip unless version.nil?
    rescue StandardError => e
      raise StandardError, "Unable to find Choco version: #{e}"
    end
  end

  version
end