class EacRubyUtils::Envs::Command
Attributes
extra_options[R]
Public Class Methods
new(env, command, extra_options = {})
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 14 def initialize(env, command, extra_options = {}) @env = env @extra_options = extra_options.with_indifferent_access if command.count == 1 && command.first.is_a?(Array) @command = command.first elsif command.is_a?(Array) @command = command else raise "Invalid argument command: #{command}|#{command.class}" end end
Public Instance Methods
append(args)
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 30 def append(args) duplicate_by_command(@command + args) end
args()
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 26 def args @command end
command(options = {})
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 42 def command(options = {}) c = @command c = c.map { |x| escape(x) }.join(' ') if c.is_a?(Enumerable) append_command_options( @env.command_line( append_chdir(append_concat(append_envvars(c))) ), options ) end
execute(options = {})
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 61 def execute(options = {}) c = command(options) debug_print("BEFORE: #{c}") t1 = Time.now r = ::EacRubyUtils::Envs::Process.new(c).to_h i = Time.now - t1 debug_print("AFTER [#{i}]: #{c}") r end
execute!(options = {})
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 53 def execute!(options = {}) options[:exit_outputs] = status_results.merge(options[:exit_outputs].presence || {}) er = ExecuteResult.new(execute(options), options) return er.result if er.success? raise "execute! command failed: #{self}\n#{er.r.pretty_inspect}" end
prepend(args)
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 34 def prepend(args) duplicate_by_command(args + @command) end
spawn(options = {})
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 71 def spawn(options = {}) c = command(options) debug_print("SPAWN: #{c}") ::EacRubyUtils::Envs::Spawn.new(c) end
system(options = {})
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 83 def system(options = {}) c = command(options) debug_print(c) Kernel.system(c) end
system!(options = {})
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 77 def system!(options = {}) return if system(options) raise "system! command failed: #{self}" end
to_s()
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 38 def to_s "#{@command} [ENV: #{@env}]" end
Protected Instance Methods
duplicate(command, extra_options)
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 91 def duplicate(command, extra_options) self.class.new(@env, command, extra_options) end
Private Instance Methods
append_command_options(command, options)
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 117 def append_command_options(command, options) command = options[:input].command + ' | ' + command if options[:input] if options[:input_file] command = "cat #{Shellwords.escape(options[:input_file])}" \ " | #{command}" end command += ' > ' + Shellwords.escape(options[:output_file]) if options[:output_file] command end
debug?()
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 107 def debug? ENV['DEBUG'].to_s.strip != '' end
debug_print(message)
click to toggle source
Print a message if debugging is enabled.
# File lib/eac_ruby_utils/envs/command.rb, line 112 def debug_print(message) message = message.to_s puts message.if_respond(:light_red, message) if debug? end
duplicate_by_command(new_command)
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 99 def duplicate_by_command(new_command) duplicate(new_command, @extra_options) end
duplicate_by_extra_options(set_extra_options)
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 103 def duplicate_by_extra_options(set_extra_options) duplicate(@command, @extra_options.merge(set_extra_options)) end
escape(arg)
click to toggle source
# File lib/eac_ruby_utils/envs/command.rb, line 127 def escape(arg) arg = arg.to_s m = /^\@ESC_(.+)$/.match(arg) m ? m[1] : Shellwords.escape(arg) end