Module: Chef::Sugar::Shell
Instance Method Summary collapse
-
#dev_null(node) ⇒ String
The platform-specific output path to
/dev/null
. -
#installed?(cmd) ⇒ Boolean
Boolean method to check if a command line utility is installed.
-
#installed_at_version?(cmd, expected_version, flag = '--version') ⇒ Boolean
Checks if the given binary is installed and exists at the given version.
-
#version_for(cmd, flag = '--version') ⇒ String
The version for a given command.
-
#which(cmd) ⇒ String?
Finds a command in $PATH.
Instance Method Details
#dev_null(node) ⇒ String
The platform-specific output path to /dev/null
.
53 54 55 56 57 58 59 |
# File 'lib/chef/sugar/shell.rb', line 53 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) ⇒ Boolean
Boolean method to check if a command line utility is installed.
70 71 72 |
# File 'lib/chef/sugar/shell.rb', line 70 def installed?(cmd) !which(cmd).nil? end |
#installed_at_version?(cmd, expected_version, flag = '--version') ⇒ Boolean
Checks if the given binary is installed and exists at the given version. Also see #version_for.
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/chef/sugar/shell.rb', line 89 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') ⇒ 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+ or +$stderr+.
Did you know that java outputs its version to $stderr?
122 123 124 125 126 127 |
# File 'lib/chef/sugar/shell.rb', line 122 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) ⇒ String?
Finds a command in $PATH
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/chef/sugar/shell.rb', line 33 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 |