Module: Chef::Sugar::Shell

Extended by:
Shell
Included in:
Shell
Defined in:
lib/chef/sugar/shell.rb

Instance Method Summary collapse

Instance Method Details

#dev_null(node) ⇒ String

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



48
49
50
# File 'lib/chef/sugar/shell.rb', line 48

def dev_null(node)
  Chef::Sugar::PlatformFamily.windows?(node) ? 'NUL' : '/dev/null'
end

#installed?(cmd) ⇒ Boolean

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



61
62
63
# File 'lib/chef/sugar/shell.rb', line 61

def installed?(cmd)
  !which(cmd).nil?
end

#installed_at_version?(cmd, version, flag = '--version') ⇒ Boolean

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



80
81
82
83
84
85
86
# File 'lib/chef/sugar/shell.rb', line 80

def installed_at_version?(cmd, version, flag = '--version')
  which(cmd) && if version.is_a?(Regexp)
                  version_for(cmd, flag) =~ version
                else
                  version_for(cmd, flag).include?(version)
                end
end

#version_for(cmd, flag = '--version') ⇒ String

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+.


107
108
109
110
111
112
# File 'lib/chef/sugar/shell.rb', line 107

def version_for(cmd, flag = '--version')
  cmd = Mixlib::ShellOut.new("#{cmd} #{flag}")
  cmd.run_command
  cmd.error!
  cmd.stdout.strip
end

#which(cmd) ⇒ String?

Finds a command in $PATH



32
33
34
35
36
37
38
39
40
41
# File 'lib/chef/sugar/shell.rb', line 32

def which(cmd)
  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