@author Lee Hambley
Initialize a new Command object
@param [Array] A list of arguments, the first is considered to be the command name, with optional variadaric args @return [Command] An un-started command object with no exit staus, and nothing in stdin or stdout
# File lib/sshkit/command.rb, line 47 def initialize(*args) raise ArgumentError, "May not pass no arguments to Command.new" if args.empty? @options = default_options.merge(args.extract_options!) @command = args.shift.to_s.strip.to_sym @args = args @options.symbolize_keys! sanitize_command! @stdout, @stderr = String.new, String.new @full_stdout, @full_stderr = String.new, String.new end
# File lib/sshkit/command.rb, line 58 def complete? !exit_status.nil? end
# File lib/sshkit/command.rb, line 149 def environment_hash (SSHKit.config.default_env || {}).merge(options[:env] || {}) end
# File lib/sshkit/command.rb, line 153 def environment_string environment_hash.collect do |key,value| "#{key.to_s.upcase}=#{value}" end.join(' ') end
# File lib/sshkit/command.rb, line 86 def exit_status=(new_exit_status) @finished_at = Time.now @exit_status = new_exit_status if options[:raise_on_non_zero_exit] && exit_status > 0 message = "" message += "#{command} exit status: " + exit_status.to_s + "\n" message += "#{command} stdout: " + (stdout.strip.empty? ? "Nothing written" : stdout.strip) + "\n" stderr_message = [stderr.strip, full_stderr.strip].delete_if(&:empty?).first message += "#{command} stderr: " + (stderr_message || 'Nothing written') + "\n" raise Failed, message end end
# File lib/sshkit/command.rb, line 81 def failure? exit_status.to_i > 0 end
# File lib/sshkit/command.rb, line 179 def group(&block) return yield unless options[:group] "sg #{options[:group]} -c \\\"%s\\\"" % %{#{yield}} # We could also use the so-called heredoc format perhaps: #"newgrp #{options[:group]} <<EOC \\\"%s\\\" EOC" % %Q{#{yield}} end
# File lib/sshkit/command.rb, line 169 def in_background(&block) return yield unless options[:run_in_background] "( nohup %s > /dev/null & )" % yield end
# File lib/sshkit/command.rb, line 101 def runtime return nil unless complete? @finished_at - @started_at end
# File lib/sshkit/command.rb, line 140 def should_map? !command.match /\s/ end
# File lib/sshkit/command.rb, line 67 def started=(new_started) @started_at = Time.now @started = new_started end
# File lib/sshkit/command.rb, line 76 def success? exit_status.nil? ? false : exit_status.to_i == 0 end
# File lib/sshkit/command.rb, line 186 def to_command return command.to_s unless should_map? within do umask do with do user do in_background do group do to_s end end end end end end end
# File lib/sshkit/command.rb, line 106 def to_hash { command: self.to_s, args: args, options: options, exit_status: exit_status, stdout: full_stdout, stderr: full_stderr, started_at: @started_at, finished_at: @finished_at, runtime: runtime, uuid: uuid, started: started?, finished: finished?, successful: successful?, failed: failed? } end
# File lib/sshkit/command.rb, line 203 def to_s [SSHKit.config.command_map[command.to_sym], *Array(args)].join(' ') end
# File lib/sshkit/command.rb, line 174 def umask(&block) return yield unless SSHKit.config.umask "umask #{SSHKit.config.umask} && %s" % yield end
# File lib/sshkit/command.rb, line 164 def user(&block) return yield unless options[:user] "sudo -u #{options[:user]} #{environment_string + " " unless environment_string.empty?}-- sh -c '%s'" % %{#{yield}} end
# File lib/sshkit/command.rb, line 72 def uuid @uuid ||= Digest::SHA1.hexdigest(SecureRandom.random_bytes(10))[0..7] end
# File lib/sshkit/command.rb, line 129 def verbosity if vb = options[:verbosity] case vb.class.name when 'Symbol' then return Logger.const_get(vb.to_s.upcase) when 'Fixnum' then return vb end else Logger::INFO end end
Generated with the Darkfish Rdoc Generator 2.