summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2016-09-29 14:32:08 -0700
committerThom May <thom@chef.io>2016-10-07 11:12:29 +0100
commit9efca630e084e93fb1ac5eb9c9f04b74d81f4ed7 (patch)
tree82e5956c20f95d32e9025152c422e313965eb4d2 /lib
parentad54419ab0f194c1d89ee8f3065a7a1f053ce6f2 (diff)
downloadmixlib-shellout-9efca630e084e93fb1ac5eb9c9f04b74d81f4ed7.tar.gz
Resolve most chefstyle warnings
Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'lib')
-rw-r--r--lib/mixlib/shellout.rb56
-rw-r--r--lib/mixlib/shellout/unix.rb14
-rw-r--r--lib/mixlib/shellout/windows.rb60
-rw-r--r--lib/mixlib/shellout/windows/core_ext.rb128
4 files changed, 129 insertions, 129 deletions
diff --git a/lib/mixlib/shellout.rb b/lib/mixlib/shellout.rb
index 2fdbcfd..d15b645 100644
--- a/lib/mixlib/shellout.rb
+++ b/lib/mixlib/shellout.rb
@@ -16,10 +16,10 @@
# limitations under the License.
#
-require 'etc'
-require 'tmpdir'
-require 'fcntl'
-require 'mixlib/shellout/exceptions'
+require "etc"
+require "tmpdir"
+require "fcntl"
+require "mixlib/shellout/exceptions"
module Mixlib
@@ -29,10 +29,10 @@ module Mixlib
DEFAULT_READ_TIMEOUT = 600
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
- require 'mixlib/shellout/windows'
+ require "mixlib/shellout/windows"
include ShellOut::Windows
else
- require 'mixlib/shellout/unix'
+ require "mixlib/shellout/unix"
include ShellOut::Unix
end
@@ -162,7 +162,7 @@ module Mixlib
# cmd = Mixlib::ShellOut.new("apachectl", "start", :user => 'www', :env => nil, :cwd => '/tmp')
# cmd.run_command # etc.
def initialize(*command_args)
- @stdout, @stderr, @process_status = '', '', ''
+ @stdout, @stderr, @process_status = "", "", ""
@live_stdout = @live_stderr = nil
@input = nil
@log_level = :debug
@@ -284,15 +284,15 @@ module Mixlib
# is highly encouraged.
# === Raises
# ShellCommandFailed always
- def invalid!(msg=nil)
+ def invalid!(msg = nil)
msg ||= "Command produced unexpected results"
raise ShellCommandFailed, msg + "\n" + format_for_exception
end
def inspect
"<#{self.class.name}##{object_id}: command: '#@command' process_status: #{@status.inspect} " +
- "stdout: '#{stdout.strip}' stderr: '#{stderr.strip}' child_pid: #{@child_pid.inspect} " +
- "environment: #{@environment.inspect} timeout: #{timeout} user: #@user group: #@group working_dir: #@cwd >"
+ "stdout: '#{stdout.strip}' stderr: '#{stderr.strip}' child_pid: #{@child_pid.inspect} " +
+ "environment: #{@environment.inspect} timeout: #{timeout} user: #@user group: #@group working_dir: #@cwd >"
end
private
@@ -300,44 +300,44 @@ module Mixlib
def parse_options(opts)
opts.each do |option, setting|
case option.to_s
- when 'cwd'
+ when "cwd"
self.cwd = setting
- when 'domain'
+ when "domain"
self.domain = setting
- when 'password'
+ when "password"
self.password = setting
- when 'user'
+ when "user"
self.user = setting
self.with_logon = setting
- when 'group'
+ when "group"
self.group = setting
- when 'umask'
+ when "umask"
self.umask = setting
- when 'timeout'
+ when "timeout"
self.timeout = setting
- when 'returns'
+ when "returns"
self.valid_exit_codes = Array(setting)
- when 'live_stream'
+ when "live_stream"
self.live_stdout = self.live_stderr = setting
- when 'live_stdout'
+ when "live_stdout"
self.live_stdout = setting
- when 'live_stderr'
+ when "live_stderr"
self.live_stderr = setting
- when 'input'
+ when "input"
self.input = setting
- when 'logger'
+ when "logger"
self.logger = setting
- when 'log_level'
+ when "log_level"
self.log_level = setting
- when 'log_tag'
+ when "log_tag"
self.log_tag = setting
- when 'environment', 'env'
+ when "environment", "env"
if setting
- self.environment = Hash[setting.map{|(k,v)| [k.to_s,v]}]
+ self.environment = Hash[setting.map { |(k, v)| [k.to_s, v] }]
else
self.environment = {}
end
- when 'login'
+ when "login"
self.login = setting
else
raise InvalidCommandOption, "option '#{option.inspect}' is not a valid option for #{self.class.name}"
diff --git a/lib/mixlib/shellout/unix.rb b/lib/mixlib/shellout/unix.rb
index 734a597..238273c 100644
--- a/lib/mixlib/shellout/unix.rb
+++ b/lib/mixlib/shellout/unix.rb
@@ -39,7 +39,7 @@ module Mixlib
def all_seconderies
ret = []
Etc.endgrent
- while ( g = Etc.getgrent ) do
+ while ( g = Etc.getgrent )
ret << g
end
Etc.endgrent
@@ -52,7 +52,7 @@ module Mixlib
def sgids
return nil unless using_login?
user_name = Etc.getpwuid(uid).name
- all_seconderies.select{|g| g.mem.include?(user_name)}.map{|g|g.gid}
+ all_seconderies.select { |g| g.mem.include?(user_name) }.map { |g| g.gid }
end
# The environment variables that are deduced from simulating logon
@@ -63,7 +63,7 @@ module Mixlib
# According to `man su`, the set fields are:
# $HOME, $SHELL, $USER, $LOGNAME, $PATH, and $IFS
# Values are copied from "shadow" package in Ubuntu 14.10
- {'HOME'=>entry.dir, 'SHELL'=>entry.shell, 'USER'=>entry.name, 'LOGNAME'=>entry.name, 'PATH'=>'/sbin:/bin:/usr/sbin:/usr/bin', 'IFS'=>"\t\n"}
+ { "HOME" => entry.dir, "SHELL" => entry.shell, "USER" => entry.name, "LOGNAME" => entry.name, "PATH" => "/sbin:/bin:/usr/sbin:/usr/bin", "IFS" => "\t\n" }
end
# Merges the two environments for the process
@@ -170,7 +170,7 @@ module Mixlib
def set_environment
# user-set variables should override the login ones
- process_environment.each do |env_var,value|
+ process_environment.each do |env_var, value|
ENV[env_var] = value
end
end
@@ -335,14 +335,14 @@ module Mixlib
set_cwd
begin
- command.kind_of?(Array) ? exec(*command, :close_others=>true) : exec(command, :close_others=>true)
+ command.kind_of?(Array) ? exec(*command, :close_others => true) : exec(command, :close_others => true)
- raise 'forty-two' # Should never get here
+ raise "forty-two" # Should never get here
rescue Exception => e
Marshal.dump(e, process_status_pipe.last)
process_status_pipe.last.flush
end
- process_status_pipe.last.close unless (process_status_pipe.last.closed?)
+ process_status_pipe.last.close unless process_status_pipe.last.closed?
exit!
end
end
diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb
index 5b13dad..0c66c2e 100644
--- a/lib/mixlib/shellout/windows.rb
+++ b/lib/mixlib/shellout/windows.rb
@@ -18,8 +18,8 @@
# limitations under the License.
#
-require 'win32/process'
-require 'mixlib/shellout/windows/core_ext'
+require "win32/process"
+require "mixlib/shellout/windows/core_ext"
module Mixlib
class ShellOut
@@ -63,10 +63,10 @@ module Mixlib
:startup_info => {
:stdout => stdout_write,
:stderr => stderr_write,
- :stdin => stdin_read
+ :stdin => stdin_read,
},
- :environment => inherit_environment.map { |k,v| "#{k}=#{v}" },
- :close_handles => false
+ :environment => inherit_environment.map { |k, v| "#{k}=#{v}" },
+ :close_handles => false,
}
create_process_args[:cwd] = cwd if cwd
# default to local account database if domain is not specified
@@ -90,24 +90,24 @@ module Mixlib
# Wait for the process to finish, consuming output as we go
#
start_wait = Time.now
- while true
+ loop do
wait_status = WaitForSingleObject(process.process_handle, 0)
case wait_status
when WAIT_OBJECT_0
# Get process exit code
- exit_code = [0].pack('l')
+ exit_code = [0].pack("l")
unless GetExitCodeProcess(process.process_handle, exit_code)
raise get_last_error
end
@status = ThingThatLooksSortOfLikeAProcessStatus.new
- @status.exitstatus = exit_code.unpack('l').first
+ @status.exitstatus = exit_code.unpack("l").first
return self
when WAIT_TIMEOUT
# Kill the process
if (Time.now - start_wait) > timeout
begin
- require 'wmi-lite/wmi'
+ require "wmi-lite/wmi"
wmi = WmiLite::Wmi.new
Utils.kill_process_tree(process.process_id, wmi, logger)
Process.kill(:KILL, process.process_id)
@@ -118,13 +118,13 @@ module Mixlib
raise Mixlib::ShellOut::CommandTimeout, [
"command timed out:",
format_for_exception,
- Utils.format_process(process, app_name, command_line, timeout)
+ Utils.format_process(process, app_name, command_line, timeout),
].join("\n")
end
consume_output(open_streams, stdout_read, stderr_read)
else
- raise "Unknown response from WaitForSingleObject(#{process.process_handle}, #{timeout*1000}): #{wait_status}"
+ raise "Unknown response from WaitForSingleObject(#{process.process_handle}, #{timeout * 1000}): #{wait_status}"
end
end
@@ -212,7 +212,7 @@ module Mixlib
# https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4837859
# http://ss64.com/nt/syntax-esc.html
def _run_under_cmd(command)
- [ ENV['COMSPEC'], "cmd /c \"#{command}\"" ]
+ [ ENV["COMSPEC"], "cmd /c \"#{command}\"" ]
end
def _run_directly(command, exe)
@@ -220,7 +220,7 @@ module Mixlib
end
def unquoted_executable_path(command)
- command[0,command.index(/\s/) || command.length]
+ command[0, command.index(/\s/) || command.length]
end
def candidate_executable_for_command(command)
@@ -235,11 +235,11 @@ module Mixlib
def inherit_environment
result = {}
- ENV.each_pair do |k,v|
+ ENV.each_pair do |k, v|
result[k] = v
end
- environment.each_pair do |k,v|
+ environment.each_pair do |k, v|
if v == nil
result.delete(k)
else
@@ -266,15 +266,15 @@ module Mixlib
command.dup.each_char do |c|
case c
when "'", '"'
- if (!quote)
+ if !quote
quote = c
elsif quote == c
quote = nil
end
next
- when '>', '<', '|', '&', "\n"
+ when ">", "<", "|", "&", "\n"
return true unless quote
- when '%'
+ when "%"
return true if env
env = env_first_char = true
next
@@ -282,7 +282,7 @@ module Mixlib
next unless env
if env_first_char
env_first_char = false
- env = false and next if c !~ /[A-Za-z_]/
+ (env = false) && next if c !~ /[A-Za-z_]/
end
env = false if c !~ /[A-Za-z1-9_]/
end
@@ -291,13 +291,13 @@ module Mixlib
end
def self.pathext
- @pathext ||= ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') + [''] : ['']
+ @pathext ||= ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") + [""] : [""]
end
# which() mimicks the Unix which command
# FIXME: it is not working
def self.which(cmd)
- ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
+ ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
exe = find_executable("#{path}/#{cmd}")
return exe if exe
end
@@ -323,13 +323,13 @@ module Mixlib
def self.system_required_processes
[
- 'System Idle Process',
- 'System',
- 'spoolsv.exe',
- 'lsass.exe',
- 'csrss.exe',
- 'smss.exe',
- 'svchost.exe'
+ "System Idle Process",
+ "System",
+ "spoolsv.exe",
+ "lsass.exe",
+ "csrss.exe",
+ "smss.exe",
+ "svchost.exe",
]
end
@@ -358,13 +358,13 @@ module Mixlib
child_pid = instance.wmi_ole_object.processid
logger.debug([
"killing child process #{child_pid}::",
- "#{instance.wmi_ole_object.Name} of parent #{pid}"
+ "#{instance.wmi_ole_object.Name} of parent #{pid}",
].join) if logger
Process.kill(:KILL, instance.wmi_ole_object.processid)
rescue Errno::EIO, SystemCallError
logger.debug([
"Failed to kill child process #{child_pid}::",
- "#{instance.wmi_ole_object.Name} of parent #{pid}"
+ "#{instance.wmi_ole_object.Name} of parent #{pid}",
].join) if logger
end
diff --git a/lib/mixlib/shellout/windows/core_ext.rb b/lib/mixlib/shellout/windows/core_ext.rb
index 2fe2bf2..17faff2 100644
--- a/lib/mixlib/shellout/windows/core_ext.rb
+++ b/lib/mixlib/shellout/windows/core_ext.rb
@@ -17,7 +17,7 @@
# limitations under the License.
#
-require 'win32/process'
+require "win32/process"
# Add new constants for Logon
module Process::Constants
@@ -65,78 +65,78 @@ module Process
class << self
def create(args)
unless args.kind_of?(Hash)
- raise TypeError, 'hash keyword arguments expected'
+ raise TypeError, "hash keyword arguments expected"
end
- valid_keys = %w[
+ valid_keys = %w{
app_name command_line inherit creation_flags cwd environment
startup_info thread_inherit process_inherit close_handles with_logon
domain password
- ]
+ }
- valid_si_keys = %w[
+ valid_si_keys = %w{
startf_flags desktop title x y x_size y_size x_count_chars
y_count_chars fill_attribute sw_flags stdin stdout stderr
- ]
+ }
# Set default values
hash = {
- 'app_name' => nil,
- 'creation_flags' => 0,
- 'close_handles' => true
+ "app_name" => nil,
+ "creation_flags" => 0,
+ "close_handles" => true,
}
# Validate the keys, and convert symbols and case to lowercase strings.
- args.each{ |key, val|
+ args.each do |key, val|
key = key.to_s.downcase
unless valid_keys.include?(key)
raise ArgumentError, "invalid key '#{key}'"
end
hash[key] = val
- }
+ end
si_hash = {}
# If the startup_info key is present, validate its subkeys
- if hash['startup_info']
- hash['startup_info'].each{ |key, val|
+ if hash["startup_info"]
+ hash["startup_info"].each do |key, val|
key = key.to_s.downcase
unless valid_si_keys.include?(key)
raise ArgumentError, "invalid startup_info key '#{key}'"
end
si_hash[key] = val
- }
+ end
end
# The +command_line+ key is mandatory unless the +app_name+ key
# is specified.
- unless hash['command_line']
- if hash['app_name']
- hash['command_line'] = hash['app_name']
- hash['app_name'] = nil
+ unless hash["command_line"]
+ if hash["app_name"]
+ hash["command_line"] = hash["app_name"]
+ hash["app_name"] = nil
else
- raise ArgumentError, 'command_line or app_name must be specified'
+ raise ArgumentError, "command_line or app_name must be specified"
end
end
env = nil
# The env string should be passed as a string of ';' separated paths.
- if hash['environment']
- env = hash['environment']
+ if hash["environment"]
+ env = hash["environment"]
unless env.respond_to?(:join)
- env = hash['environment'].split(File::PATH_SEPARATOR)
+ env = hash["environment"].split(File::PATH_SEPARATOR)
end
- env = env.map{ |e| e + 0.chr }.join('') + 0.chr
- env.to_wide_string! if hash['with_logon']
+ env = env.map { |e| e + 0.chr }.join("") + 0.chr
+ env.to_wide_string! if hash["with_logon"]
end
# Process SECURITY_ATTRIBUTE structure
process_security = nil
- if hash['process_inherit']
+ if hash["process_inherit"]
process_security = SECURITY_ATTRIBUTES.new
process_security[:nLength] = 12
process_security[:bInheritHandle] = 1
@@ -145,7 +145,7 @@ module Process
# Thread SECURITY_ATTRIBUTE structure
thread_security = nil
- if hash['thread_inherit']
+ if hash["thread_inherit"]
thread_security = SECURITY_ATTRIBUTES.new
thread_security[:nLength] = 12
thread_security[:bInheritHandle] = 1
@@ -156,7 +156,7 @@ module Process
# will not work on JRuby because of the way it handles internal file
# descriptors.
#
- ['stdin', 'stdout', 'stderr'].each{ |io|
+ %w{stdin stdout stderr}.each do |io|
if si_hash[io]
if si_hash[io].respond_to?(:fileno)
handle = get_osfhandle(si_hash[io].fileno)
@@ -187,66 +187,66 @@ module Process
raise SystemCallError.new("SetHandleInformation", FFI.errno) unless bool
si_hash[io] = handle
- si_hash['startf_flags'] ||= 0
- si_hash['startf_flags'] |= STARTF_USESTDHANDLES
- hash['inherit'] = true
+ si_hash["startf_flags"] ||= 0
+ si_hash["startf_flags"] |= STARTF_USESTDHANDLES
+ hash["inherit"] = true
end
- }
+ end
procinfo = PROCESS_INFORMATION.new
startinfo = STARTUPINFO.new
unless si_hash.empty?
startinfo[:cb] = startinfo.size
- startinfo[:lpDesktop] = si_hash['desktop'] if si_hash['desktop']
- startinfo[:lpTitle] = si_hash['title'] if si_hash['title']
- startinfo[:dwX] = si_hash['x'] if si_hash['x']
- startinfo[:dwY] = si_hash['y'] if si_hash['y']
- startinfo[:dwXSize] = si_hash['x_size'] if si_hash['x_size']
- startinfo[:dwYSize] = si_hash['y_size'] if si_hash['y_size']
- startinfo[:dwXCountChars] = si_hash['x_count_chars'] if si_hash['x_count_chars']
- startinfo[:dwYCountChars] = si_hash['y_count_chars'] if si_hash['y_count_chars']
- startinfo[:dwFillAttribute] = si_hash['fill_attribute'] if si_hash['fill_attribute']
- startinfo[:dwFlags] = si_hash['startf_flags'] if si_hash['startf_flags']
- startinfo[:wShowWindow] = si_hash['sw_flags'] if si_hash['sw_flags']
+ startinfo[:lpDesktop] = si_hash["desktop"] if si_hash["desktop"]
+ startinfo[:lpTitle] = si_hash["title"] if si_hash["title"]
+ startinfo[:dwX] = si_hash["x"] if si_hash["x"]
+ startinfo[:dwY] = si_hash["y"] if si_hash["y"]
+ startinfo[:dwXSize] = si_hash["x_size"] if si_hash["x_size"]
+ startinfo[:dwYSize] = si_hash["y_size"] if si_hash["y_size"]
+ startinfo[:dwXCountChars] = si_hash["x_count_chars"] if si_hash["x_count_chars"]
+ startinfo[:dwYCountChars] = si_hash["y_count_chars"] if si_hash["y_count_chars"]
+ startinfo[:dwFillAttribute] = si_hash["fill_attribute"] if si_hash["fill_attribute"]
+ startinfo[:dwFlags] = si_hash["startf_flags"] if si_hash["startf_flags"]
+ startinfo[:wShowWindow] = si_hash["sw_flags"] if si_hash["sw_flags"]
startinfo[:cbReserved2] = 0
- startinfo[:hStdInput] = si_hash['stdin'] if si_hash['stdin']
- startinfo[:hStdOutput] = si_hash['stdout'] if si_hash['stdout']
- startinfo[:hStdError] = si_hash['stderr'] if si_hash['stderr']
+ startinfo[:hStdInput] = si_hash["stdin"] if si_hash["stdin"]
+ startinfo[:hStdOutput] = si_hash["stdout"] if si_hash["stdout"]
+ startinfo[:hStdError] = si_hash["stderr"] if si_hash["stderr"]
end
app = nil
cmd = nil
# Convert strings to wide character strings if present
- if hash['app_name']
- app = hash['app_name'].to_wide_string
+ if hash["app_name"]
+ app = hash["app_name"].to_wide_string
end
- if hash['command_line']
- cmd = hash['command_line'].to_wide_string
+ if hash["command_line"]
+ cmd = hash["command_line"].to_wide_string
end
- if hash['cwd']
- cwd = hash['cwd'].to_wide_string
+ if hash["cwd"]
+ cwd = hash["cwd"].to_wide_string
end
- inherit = hash['inherit'] ? 1 : 0
+ inherit = hash["inherit"] ? 1 : 0
- if hash['with_logon']
- logon = hash['with_logon'].to_wide_string
+ if hash["with_logon"]
+ logon = hash["with_logon"].to_wide_string
- if hash['password']
- passwd = hash['password'].to_wide_string
+ if hash["password"]
+ passwd = hash["password"].to_wide_string
else
- raise ArgumentError, 'password must be specified if with_logon is used'
+ raise ArgumentError, "password must be specified if with_logon is used"
end
- if hash['domain']
- domain = hash['domain'].to_wide_string
+ if hash["domain"]
+ domain = hash["domain"].to_wide_string
end
- hash['creation_flags'] |= CREATE_UNICODE_ENVIRONMENT
+ hash["creation_flags"] |= CREATE_UNICODE_ENVIRONMENT
winsta_name = FFI::MemoryPointer.new(:char, 256)
return_size = FFI::MemoryPointer.new(:ulong)
@@ -297,7 +297,7 @@ module Process
process_security, # Process attributes
thread_security, # Thread attributes
inherit, # Inherit handles
- hash['creation_flags'], # Creation Flags
+ hash["creation_flags"], # Creation Flags
env, # Environment
cwd, # Working directory
startinfo, # Startup Info
@@ -318,7 +318,7 @@ module Process
LOGON_WITH_PROFILE, # Logon flags
app, # App name
cmd, # Command line
- hash['creation_flags'], # Creation flags
+ hash["creation_flags"], # Creation flags
env, # Environment
cwd, # Working directory
startinfo, # Startup Info
@@ -336,7 +336,7 @@ module Process
process_security, # Process attributes
thread_security, # Thread attributes
inherit, # Inherit handles?
- hash['creation_flags'], # Creation flags
+ hash["creation_flags"], # Creation flags
env, # Environment
cwd, # Working directory
startinfo, # Startup Info
@@ -350,7 +350,7 @@ module Process
# Automatically close the process and thread handles in the
# PROCESS_INFORMATION struct unless explicitly told not to.
- if hash['close_handles']
+ if hash["close_handles"]
CloseHandle(procinfo[:hProcess])
CloseHandle(procinfo[:hThread])
# Clear these fields so callers don't attempt to close the handle