module Chef::Sugar::Shell

Public Instance Methods

dev_null(node) click to toggle source

The platform-specific output path to /dev/null.

@return [String]

# File lib/chef/sugar/shell.rb, line 56
def dev_null(node)
  if defined?(ChefUtils)
    ChefUtils.windows?(node) ? 'NUL' : '/dev/null'
  else
    Chef::Sugar::PlatformFamily.windows?(node) ? 'NUL' : '/dev/null'
  end
end
installed?(cmd) click to toggle source

Boolean method to check if a command line utility is installed.

@param [String] cmd

the command to find

@return [Boolean]

true if the command is found in the path, false otherwise
# File lib/chef/sugar/shell.rb, line 73
def installed?(cmd)
  !which(cmd).nil?
end
installed_at_version?(cmd, expected_version, flag = '--version') click to toggle source

Checks if the given binary is installed and exists at the given version. Also see {version_for}.

@param [String] cmd

the command to check

@param [String] expected_version

the version to check

@param [String] flag

the flag to use to check the version of the binary

@return [Boolean]

true if the command exists and is at the given version, false
otherwise
# File lib/chef/sugar/shell.rb, line 92
def installed_at_version?(cmd, expected_version, flag = '--version')
  return false if !installed?(cmd)

  version = version_for(cmd, flag)
  return false if version.nil?

  if expected_version.is_a?(Regexp)
    !version.match(expected_version).nil?
  else
    version.include?(expected_version)
  end
end
version_for(cmd, flag = '--version') click to toggle source

The version for a given command. This method does NOT check if the command exists! It is assumed the command existence has been checked with which or similar. To simply check if an installed version is acceptable, please see {installed_at_version}.

Assumptions:

1. The command exists.
2. The command outputs version information to +$stdout+ or +$stderr+.
   Did you know that java outputs its version to $stderr?

@param [String] cmd

the command to find the version for

@param [String] flag

the flag to use to get the version

@return [String]

the entire output of the version command (stderr and stdout)
# File lib/chef/sugar/shell.rb, line 125
def version_for(cmd, flag = '--version')
  cmd = Mixlib::ShellOut.new("#{cmd} #{flag}")
  cmd.run_command
  cmd.error!
  [cmd.stdout.strip, cmd.stderr.strip].join("\n")
end
which(cmd) click to toggle source

Finds a command in $PATH

@param [String] cmd

the command to find

@return [String, nil]

# File lib/chef/sugar/shell.rb, line 35
def which(cmd)
  if Pathname.new(cmd).absolute?
    File.executable?(cmd) ? cmd : nil
  else
    paths = ENV['PATH'].split(::File::PATH_SEPARATOR) + %w(/bin /usr/bin /sbin /usr/sbin)

    paths.each do |path|
      possible = File.join(path, cmd)
      return possible if File.executable?(possible)
    end

    nil
  end
end